Passed
Push — master ( a8119a...bd111d )
by Caen
03:52 queued 15s
created

MakePostCommand::askForString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Exception;
8
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
9
use LaravelZero\Framework\Commands\Command;
10
use function ucwords;
11
12
/**
13
 * Hyde Command to scaffold a new Markdown Post.
14
 */
15
class MakePostCommand extends Command
16
{
17
    /** @var string */
18
    protected $signature = 'make:post
19
                            {title? : The title for the Post. Will also be used to generate the filename}
20
                            {--force : Should the generated file overwrite existing posts with the same filename?}';
21
22
    /** @var string */
23
    protected $description = 'Scaffold a new Markdown blog post file';
24
25
    public function handle(): int
26
    {
27
        $this->title('Creating a new post!');
28
29
        $title = $this->getTitle();
30
31
        [$description, $author, $category] = $this->getSelections();
32
33
        $creator = new CreatesNewMarkdownPostFile($title, $description, $category, $author);
34
35
        $this->displaySelections($creator);
36
37
        if (! $this->confirm('Do you wish to continue?', true)) {
38
            $this->info('Aborting.');
39
40
            return 130;
41
        }
42
43
        return $this->createPostFile($creator);
44
    }
45
46
    protected function getTitle(): string
47
    {
48
        $this->line($this->argument('title')
49
                ? '<info>Selected title: '.$this->argument('title')."</info>\n"
50
                : 'Please enter the title of the post, it will be used to generate the filename.'
51
        );
52
53
        return $this->argument('title')
54
            ?? $this->askForString('What is the title of the post?')
55
            ?? 'My New Post';
56
    }
57
58
    /** @return array<?string> */
59
    protected function getSelections(): array
60
    {
61
        $this->line('Tip: You can just hit return to use the defaults.');
62
63
        $description = $this->askForString('Write a short post excerpt/description');
64
        $author = $this->askForString('What is your (the author\'s) name?');
65
        $category = $this->askForString('What is the primary category of the post?');
66
67
        return [$description, $author, $category];
68
    }
69
70
    protected function displaySelections(CreatesNewMarkdownPostFile $creator): void
71
    {
72
        $this->info('Creating a post with the following details:');
73
74
        foreach ($creator->toArray() as $key => $value) {
75
            $this->line(sprintf('%s: %s', ucwords($key), $value));
76
        }
77
78
        $this->line("Identifier: {$creator->getIdentifier()}");
79
    }
80
81
    protected function createPostFile(CreatesNewMarkdownPostFile $creator): int
82
    {
83
        try {
84
            $path = $creator->save($this->option('force'));
0 ignored issues
show
Bug introduced by
$this->option('force') of type string is incompatible with the type boolean expected by parameter $force of Hyde\Framework\Actions\C...arkdownPostFile::save(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $path = $creator->save(/** @scrutinizer ignore-type */ $this->option('force'));
Loading history...
85
            $this->info("Post created! File is saved to $path");
86
87
            return Command::SUCCESS;
88
        } catch (Exception $exception) {
89
            $this->error('Something went wrong when trying to save the file!');
90
            $this->warn($exception->getMessage());
91
92
            if ($exception->getCode() === 409) {
93
                $this->comment('If you want to overwrite the file supply the --force flag.');
94
            }
95
96
            return (int) $exception->getCode();
97
        }
98
    }
99
100
    protected function askForString(string $question, ?string $default = null): ?string
101
    {
102
        return is_string($answer = $this->output->ask($question, $default)) ? $answer : null;
103
    }
104
}
105