| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 100.00% | 15 / 15 |  | 100.00% | 2 / 2 | CRAP |  | 100.00% | 1 / 1 | 
| CreatesNewMarkdownPostFile |  | 100.00% | 15 / 15 |  | 100.00% | 2 / 2 | 7 |  | 100.00% | 1 / 1 | 
| __construct |  | 100.00% | 8 / 8 |  | 100.00% | 1 / 1 | 3 | |||
| save |  | 100.00% | 7 / 7 |  | 100.00% | 1 / 1 | 4 | |||
| 1 | <?php | 
| 2 | |
| 3 | namespace Hyde\Framework\Actions; | 
| 4 | |
| 5 | use Hyde\Framework\Exceptions\FileConflictException; | 
| 6 | use Hyde\Framework\Hyde; | 
| 7 | use Illuminate\Support\Str; | 
| 8 | |
| 9 | /** | 
| 10 | * Offloads logic for the make:post command. | 
| 11 | * | 
| 12 | * This class is executed when creating a new Markdown Post | 
| 13 | * using the Hyde command, and converts and formats the | 
| 14 | * data input by the user, and then saves the file. | 
| 15 | * | 
| 16 | * @see \Hyde\Framework\Commands\HydeMakePostCommand | 
| 17 | */ | 
| 18 | class CreatesNewMarkdownPostFile | 
| 19 | { | 
| 20 | /** | 
| 21 | * The Post Title. | 
| 22 | * | 
| 23 | * @var string | 
| 24 | */ | 
| 25 | public string $title; | 
| 26 | |
| 27 | /** | 
| 28 | * The Post Meta Description. | 
| 29 | * | 
| 30 | * @var string | 
| 31 | */ | 
| 32 | public string $description; | 
| 33 | |
| 34 | /** | 
| 35 | * The Primary Post Category. | 
| 36 | * | 
| 37 | * @var string | 
| 38 | */ | 
| 39 | public string $category; | 
| 40 | |
| 41 | /** | 
| 42 | * The Username of the Author. | 
| 43 | * | 
| 44 | * @var string | 
| 45 | */ | 
| 46 | public string $author; | 
| 47 | |
| 48 | /** | 
| 49 | * The Publishing Date. | 
| 50 | * | 
| 51 | * @var string | 
| 52 | */ | 
| 53 | public string $date; | 
| 54 | |
| 55 | /** | 
| 56 | * The Post Slug. | 
| 57 | * | 
| 58 | * @var string | 
| 59 | */ | 
| 60 | public string $slug; | 
| 61 | |
| 62 | /** | 
| 63 | * Construct the class. | 
| 64 | * | 
| 65 | * @param string $title The Post Title. | 
| 66 | * @param string|null $description The Post Meta Description. | 
| 67 | * @param string|null $category The Primary Post Category. | 
| 68 | * @param string|null $author The Username of the Author. | 
| 69 | * @param string|null $date The Publishing Date. | 
| 70 | * @param string|null $slug The Post Slug. | 
| 71 | */ | 
| 72 | public function __construct( | 
| 73 | string $title, | 
| 74 | ?string $description, | 
| 75 | ?string $category, | 
| 76 | ?string $author, | 
| 77 | ?string $date = null, | 
| 78 | ?string $slug = null | 
| 79 | ) { | 
| 80 | $this->title = $title ?? 'My Awesome Blog Post'; | 
| 81 | $this->description = $description ?? 'A short description used in previews and SEO'; | 
| 82 | $this->category = $category ?? 'blog'; | 
| 83 | $this->author = $author ?? 'Mr. Hyde'; | 
| 84 | if ($date === null) { | 
| 85 | $this->date = date('Y-m-d H:i'); | 
| 86 | } | 
| 87 | if ($slug === null) { | 
| 88 | $this->slug = Str::slug($title); | 
| 89 | } | 
| 90 | } | 
| 91 | |
| 92 | /** | 
| 93 | * Save the class object to a Markdown file. | 
| 94 | * | 
| 95 | * @param bool $force Should the file be created even if a file with the same path already exists? | 
| 96 | * @return string|false Returns the path to the file if successful, or false if the file could not be saved. | 
| 97 | * | 
| 98 | * @throws FileConflictException if a file with the same slug already exists and the force flag is not set. | 
| 99 | */ | 
| 100 | public function save(bool $force = false): string|false | 
| 101 | { | 
| 102 | $path = Hyde::path("_posts/$this->slug.md"); | 
| 103 | |
| 104 | if ($force !== true && file_exists($path)) { | 
| 105 | throw new FileConflictException($path); | 
| 106 | } | 
| 107 | |
| 108 | $arrayWithoutSlug = ((array) $this); | 
| 109 | |
| 110 | unset($arrayWithoutSlug['slug']); | 
| 111 | |
| 112 | $contents = (new ConvertsArrayToFrontMatter)->execute($arrayWithoutSlug). | 
| 113 | "\n## Write something awesome.\n\n"; | 
| 114 | |
| 115 | return file_put_contents($path, $contents) ? $path : false; | 
| 116 | } | 
| 117 | } |