Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| CreatesNewMarkdownPostFile | |
100.00% |
19 / 19 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| toArray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Actions; |
| 6 | |
| 7 | use Hyde\Framework\Exceptions\FileConflictException; |
| 8 | use Hyde\Facades\Filesystem; |
| 9 | use Hyde\Hyde; |
| 10 | use Hyde\Pages\MarkdownPost; |
| 11 | use Illuminate\Support\Carbon; |
| 12 | |
| 13 | /** |
| 14 | * Offloads logic for the make:post command. |
| 15 | * |
| 16 | * This class is executed when creating a new Markdown Post |
| 17 | * using the Hyde command, and converts and formats the |
| 18 | * data input by the user, and then saves the file. |
| 19 | * |
| 20 | * @see \Hyde\Console\Commands\MakePostCommand |
| 21 | */ |
| 22 | class CreatesNewMarkdownPostFile |
| 23 | { |
| 24 | protected string $title; |
| 25 | protected string $description; |
| 26 | protected string $category; |
| 27 | protected string $author; |
| 28 | protected string $date; |
| 29 | protected string $identifier; |
| 30 | protected ?string $customContent; |
| 31 | |
| 32 | /** |
| 33 | * Construct the class. |
| 34 | * |
| 35 | * @param string $title The Post Title. |
| 36 | * @param string|null $description The Post Meta Description. |
| 37 | * @param string|null $category The Primary Post Category. |
| 38 | * @param string|null $author The Username of the Author. |
| 39 | * @param string|null $date Optionally specify a custom date. |
| 40 | * @param string|null $customContent Optionally specify custom post content. |
| 41 | */ |
| 42 | public function __construct(string $title, ?string $description, ?string $category, ?string $author, ?string $date = null, ?string $customContent = null) |
| 43 | { |
| 44 | $this->title = $title; |
| 45 | $this->description = $description ?? 'A short description used in previews and SEO'; |
| 46 | $this->category = $category ?? 'blog'; |
| 47 | $this->author = $author ?? 'default'; |
| 48 | $this->customContent = $customContent; |
| 49 | |
| 50 | $this->date = Carbon::make($date ?? Carbon::now())->format('Y-m-d H:i'); |
| 51 | $this->identifier = Hyde::makeSlug($title); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Save the class object to a Markdown file. |
| 56 | * |
| 57 | * @param bool $force Should the file be created even if a file with the same path already exists? |
| 58 | * @return string Returns the path to the created file. |
| 59 | * |
| 60 | * @throws FileConflictException if a file with the same identifier already exists and the force flag is not set. |
| 61 | */ |
| 62 | public function save(bool $force = false): string |
| 63 | { |
| 64 | $page = new MarkdownPost($this->identifier, $this->toArray(), $this->customContent ?? '## Write something awesome.'); |
| 65 | |
| 66 | if ($force !== true && Filesystem::exists($page->getSourcePath())) { |
| 67 | throw new FileConflictException($page->getSourcePath()); |
| 68 | } |
| 69 | |
| 70 | return $page->save()->getSourcePath(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the class data as an array. |
| 75 | * |
| 76 | * The identifier property is removed from the array as it can't be set in the front matter. |
| 77 | * |
| 78 | * @return array{title: string, description: string, category: string, author: string, date: string} |
| 79 | */ |
| 80 | public function toArray(): array |
| 81 | { |
| 82 | return [ |
| 83 | 'title' => $this->title, |
| 84 | 'description' => $this->description, |
| 85 | 'category' => $this->category, |
| 86 | 'author' => $this->author, |
| 87 | 'date' => $this->date, |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | public function getIdentifier(): string |
| 92 | { |
| 93 | return $this->identifier; |
| 94 | } |
| 95 | } |