Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
MakePostCommand
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getTitle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getSelections
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 displaySelections
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 createPostFile
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Console\Commands;
6
7use Exception;
8use Hyde\Console\Concerns\Command;
9use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
10
11use function sprintf;
12use function ucwords;
13
14/**
15 * Scaffold a new Markdown blog post file.
16 */
17class MakePostCommand extends Command
18{
19    /** @var string */
20    protected $signature = 'make:post
21                            {title? : The title for the Post. Will also be used to generate the filename}
22                            {--force : Should the generated file overwrite existing posts with the same filename?}';
23
24    /** @var string */
25    protected $description = 'Scaffold a new Markdown blog post file';
26
27    public function handle(): int
28    {
29        $this->title('Creating a new post!');
30
31        $title = $this->getTitle();
32
33        [$description, $author, $category] = $this->getSelections();
34
35        $creator = new CreatesNewMarkdownPostFile($title, $description, $category, $author);
36
37        $this->displaySelections($creator);
38
39        if (! $this->confirm('Do you wish to continue?', true)) {
40            $this->info('Aborting.');
41
42            return Command::USER_EXIT;
43        }
44
45        return $this->createPostFile($creator);
46    }
47
48    protected function getTitle(): string
49    {
50        $this->line($this->argument('title')
51            ? '<info>Selected title: '.$this->argument('title')."</info>\n"
52            : 'Please enter the title of the post, it will be used to generate the filename.'
53        );
54
55        return $this->argument('title')
56            ?? $this->askForString('What is the title of the post?')
57            ?? 'My New Post';
58    }
59
60    /** @return array<?string> */
61    protected function getSelections(): array
62    {
63        $this->line('Tip: You can just hit return to use the defaults.');
64
65        $description = $this->askForString('Write a short post excerpt/description');
66        $author = $this->askForString('What is your (the author\'s) name?');
67        $category = $this->askForString('What is the primary category of the post?');
68
69        return [$description, $author, $category];
70    }
71
72    protected function displaySelections(CreatesNewMarkdownPostFile $creator): void
73    {
74        $this->info('Creating a post with the following details:');
75
76        foreach ($creator->toArray() as $key => $value) {
77            $this->line(sprintf('%s: %s', ucwords($key), $value));
78        }
79
80        $this->line("Identifier: {$creator->getIdentifier()}");
81    }
82
83    protected function createPostFile(CreatesNewMarkdownPostFile $creator): int
84    {
85        try {
86            $path = $creator->save($this->option('force'));
87            $this->info("Post created! File is saved to $path");
88
89            return Command::SUCCESS;
90        } catch (Exception $exception) {
91            $this->error('Something went wrong when trying to save the file!');
92            $this->warn($exception->getMessage());
93
94            if ($exception->getCode() === 409) {
95                $this->comment('If you want to overwrite the file supply the --force flag.');
96            }
97
98            return (int) $exception->getCode();
99        }
100    }
101}