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

MakePostCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 36
dl 0
loc 61
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 51 6
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 be used to generate the slug}
20
                            {--force : Should the generated file overwrite existing posts with the same slug?}';
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
        $this->line(
30
            $this->argument('title')
31
                ? '<info>Selected title: '.$this->argument('title')."</info>\n"
32
                : 'Please enter the title of the post, it will be used to generate the slug.'
33
        );
34
35
        $title = $this->argument('title')
36
            ?? $this->ask('What is the title of the post?')
37
            ?? 'My New Post';
38
39
        $this->line('Tip: You can just hit return to use the defaults.');
40
        $description = $this->ask('Write a short post excerpt/description');
41
        $author = $this->ask('What is your (the author\'s) name?');
42
        $category = $this->ask('What is the primary category of the post?');
43
44
        $this->info('Creating a post with the following details:');
45
        $creator = new CreatesNewMarkdownPostFile(
46
            $title,
47
            $description,
48
            $category,
49
            $author
50
        );
51
52
        foreach ($creator->toArray() as $key => $value) {
53
            $this->line(sprintf('%s: %s', ucwords($key), $value));
54
        }
55
        $this->line("Identifier: {$creator->getIdentifier()}");
56
57
        if (! $this->confirm('Do you wish to continue?', true)) {
58
            $this->info('Aborting.');
59
60
            return 130;
61
        }
62
63
        try {
64
            $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

64
            $path = $creator->save(/** @scrutinizer ignore-type */ $this->option('force'));
Loading history...
65
            $this->info("Post created! File is saved to $path");
66
67
            return Command::SUCCESS;
68
        } catch (Exception $exception) {
69
            $this->error('Something went wrong when trying to save the file!');
70
            $this->warn($exception->getMessage());
71
            if ($exception->getCode() === 409) {
72
                $this->comment('If you want to overwrite the file supply the --force flag.');
73
            }
74
75
            return (int) $exception->getCode();
76
        }
77
    }
78
}
79