Passed
Push — master ( 10105b...1efe78 )
by Caen
03:18 queued 12s
created

MakePageCommand::validateOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 4
c 8
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
use function strtolower;
14
use function ucfirst;
15
16
/**
17
 * Hyde Command to scaffold a new Markdown or Blade page file.
18
 *
19
 * @see \Hyde\Framework\Testing\Feature\Commands\MakePageCommandTest
20
 */
21
class MakePageCommand extends Command
22
{
23
    /** @var string */
24
    protected $signature = 'make:page 
25
		{title? : The name of the page file to create. Will be used to generate the slug}
26
		{--type=markdown : The type of page to create (markdown, blade, or docs)}
27
        {--blade : Create a Blade page}
28
        {--docs : Create a Documentation page}
29
		{--force : Overwrite any existing files}';
30
31
    /** @var string */
32
    protected $description = 'Scaffold a new Markdown, Blade, or documentation page file';
33
34
    /**
35
     * The page title.
36
     */
37
    protected string $title;
38
39
    /**
40
     * The selected page type.
41
     */
42
    protected string $selectedType;
43
44
    /**
45
     * The page class type.
46
     *
47
     * @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...
48
     */
49
    protected string $pageClass;
50
51
    /**
52
     * Can the file be overwritten?
53
     */
54
    protected bool $force;
55
56
    public function handle(): int
57
    {
58
        $this->title('Creating a new page!');
59
60
        $this->validateOptions();
61
62
        $this->line('<info>Creating a new '.ucfirst($this->selectedType).' page with title:</info> '.$this->title."\n");
63
64
        $creator = new CreatesNewPageSourceFile($this->title, $this->pageClass, $this->force);
65
66
        $this->info("Created file {$creator->getOutputPath()}");
67
68
        return Command::SUCCESS;
69
    }
70
71
    protected function validateOptions(): void
72
    {
73
        $this->title = $this->getTitle();
74
75
        $this->selectedType = $this->getSelectedType();
76
        $this->pageClass = $this->getQualifiedPageType();
77
78
        $this->force = $this->option('force') ?? false;
79
    }
80
81
    protected function getTitle(): string
82
    {
83
        return $this->argument('title')
84
            ?? $this->ask('What is the title of the page?')
85
            ?? 'My New Page';
86
    }
87
88
    protected function getQualifiedPageType(): string
89
    {
90
        return match ($this->selectedType) {
91
            'blade' => BladePage::class,
92
            'markdown' => MarkdownPage::class,
93
            'docs', 'documentation' => DocumentationPage::class,
94
            default => throw new UnsupportedPageTypeException($this->selectedType),
95
        };
96
    }
97
98
    protected function getSelectedType(): string
99
    {
100
        return $this->getTypeOption() ?? $this->getTypeSelection();
101
    }
102
103
    protected function getTypeSelection(): string
104
    {
105
        return strtolower($this->option('type'));
106
    }
107
108
    protected function getTypeOption(): ?string
109
    {
110
        if ($this->option('blade')) {
111
            return 'blade';
112
        }
113
114
        if ($this->option('docs')) {
115
            return 'documentation';
116
        }
117
118
        return null;
119
    }
120
}
121