Passed
Push — master ( 720e46...10105b )
by Caen
03:08 queued 12s
created

CreatesNewMarkdownPostFile::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Actions;
6
7
use Hyde\Framework\Exceptions\FileConflictException;
8
use Hyde\Hyde;
9
use Hyde\Pages\MarkdownPost;
10
use Illuminate\Support\Str;
11
12
/**
13
 * Offloads logic for the make:post command.
14
 *
15
 * This class is executed when creating a new Markdown Post
16
 * using the Hyde command, and converts and formats the
17
 * data input by the user, and then saves the file.
18
 *
19
 * @see \Hyde\Console\Commands\MakePostCommand
20
 */
21
class CreatesNewMarkdownPostFile
22
{
23
    protected string $title;
24
    protected string $description;
25
    protected string $category;
26
    protected string $author;
27
    protected string $date;
28
    protected string $identifier;
29
30
    /**
31
     * Construct the class.
32
     *
33
     * @param  string  $title  The Post Title.
34
     * @param  string|null  $description  The Post Meta Description.
35
     * @param  string|null  $category  The Primary Post Category.
36
     * @param  string|null  $author  The Username of the Author.
37
     * @param  string|null  $date  The Publishing Date.
38
     * @param  string|null  $identifier  The Post Identifier.
39
     */
40
    public function __construct(
41
        string $title,
42
        ?string $description,
43
        ?string $category,
44
        ?string $author,
45
        ?string $date = null,
46
        ?string $identifier = null
47
    ) {
48
        $this->title = $title;
49
        $this->description = $description ?? 'A short description used in previews and SEO';
50
        $this->category = $category ?? 'blog';
51
        $this->author = $author ?? 'default';
52
        if ($date === null) {
53
            $this->date = date('Y-m-d H:i');
54
        }
55
        if ($identifier === null) {
56
            $this->identifier = Str::slug($title);
57
        }
58
    }
59
60
    /**
61
     * Save the class object to a Markdown file.
62
     *
63
     * @param  bool  $force  Should the file be created even if a file with the same path already exists?
64
     * @return string|false Returns the path to the file if successful, or false if the file could not be saved.
65
     *
66
     * @throws FileConflictException if a file with the same identifier already exists and the force flag is not set.
67
     */
68
    public function save(bool $force = false): string|false
69
    {
70
        $path = Hyde::path(MarkdownPost::sourcePath($this->identifier));
71
72
        if ($force !== true && file_exists($path)) {
73
            throw new FileConflictException($path);
74
        }
75
76
        $contents = (new ConvertsArrayToFrontMatter)->execute($this->toArray()).
77
            "\n## Write something awesome.\n\n";
78
79
        return file_put_contents($path, $contents) ? $path : false;
80
    }
81
82
    /**
83
     * Get the class data as an array.
84
     *
85
     * The identifier property is removed from the array as it can't be set in the front matter.
86
     *
87
     * @return array
88
     */
89
    public function toArray(): array
90
    {
91
        return [
92
            'title' => $this->title,
93
            'description' => $this->description,
94
            'category' => $this->category,
95
            'author' => $this->author,
96
            'date' => $this->date,
97
        ];
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getIdentifier(): string
104
    {
105
        return $this->identifier;
106
    }
107
}
108