Passed
Push — master ( 561a55...a121e9 )
by Caen
03:36 queued 14s
created

MakePageCommand::validateOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
8
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
9
use Hyde\Pages\BladePage;
10
use Hyde\Pages\DocumentationPage;
11
use Hyde\Pages\MarkdownPage;
12
use LaravelZero\Framework\Commands\Command;
13
14
use function strtolower;
15
use function ucfirst;
16
17
/**
18
 * Hyde Command to scaffold a new Markdown or Blade page file.
19
 */
20
class MakePageCommand extends Command
21
{
22
    /** @var string */
23
    protected $signature = 'make:page
24
        {title? : The name of the page file to create. Will be used to generate the slug}
25
        {--type=markdown : The type of page to create (markdown, blade, or docs)}
26
        {--blade : Create a Blade page}
27
        {--docs : Create a Documentation page}
28
        {--force : Overwrite any existing files}';
29
30
    /** @var string */
31
    protected $description = 'Scaffold a new Markdown, Blade, or documentation page file';
32
33
    /**
34
     * The page title.
35
     */
36
    protected string $title;
37
38
    /**
39
     * The selected page type.
40
     */
41
    protected string $selectedType;
42
43
    /**
44
     * The page class type.
45
     *
46
     * @var class-string<\Hyde\Pages\Concerns\HydePage>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
47
     */
48
    protected string $pageClass;
49
50
    /**
51
     * Can the file be overwritten?
52
     */
53
    protected bool $force;
54
55
    public function handle(): int
56
    {
57
        $this->title('Creating a new page!');
58
59
        $this->validateOptions();
60
61
        $this->line('<info>Creating a new '.ucfirst($this->selectedType).' page with title:</info> '.$this->title."\n");
62
63
        $creator = new CreatesNewPageSourceFile($this->title, $this->pageClass, $this->force);
64
65
        $this->info("Created file {$creator->save()}");
66
67
        return Command::SUCCESS;
68
    }
69
70
    protected function validateOptions(): void
71
    {
72
        $this->title = $this->getTitle();
73
74
        $this->selectedType = $this->getSelectedType();
75
        $this->pageClass = $this->getQualifiedPageType();
76
77
        $this->force = $this->option('force') ?? false;
78
    }
79
80
    protected function getTitle(): string
81
    {
82
        return $this->argument('title')
83
            ?? $this->askForString('What is the title of the page?')
84
            ?? 'My New Page';
85
    }
86
87
    protected function getQualifiedPageType(): string
88
    {
89
        return match ($this->selectedType) {
90
            'blade' => BladePage::class,
91
            'markdown' => MarkdownPage::class,
92
            'docs', 'documentation' => DocumentationPage::class,
93
            default => throw new UnsupportedPageTypeException($this->selectedType),
94
        };
95
    }
96
97
    protected function getSelectedType(): string
98
    {
99
        return $this->getTypeOption() ?? $this->getTypeSelection();
100
    }
101
102
    protected function getTypeSelection(): string
103
    {
104
        return strtolower($this->option('type'));
105
    }
106
107
    protected function getTypeOption(): ?string
108
    {
109
        if ($this->option('blade')) {
110
            return 'blade';
111
        }
112
113
        if ($this->option('docs')) {
114
            return 'documentation';
115
        }
116
117
        return null;
118
    }
119
120
    protected function askForString(string $question, ?string $default = null): ?string
121
    {
122
        return $this->ask($question, $default);
123
    }
124
}
125