Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
MakePageCommand
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 validateOptions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getTitle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getQualifiedPageType
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getSelectedType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTypeSelection
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTypeOption
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Console\Commands;
6
7use Hyde\Framework\Actions\CreatesNewPageSourceFile;
8use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
9use Hyde\Pages\BladePage;
10use Hyde\Pages\DocumentationPage;
11use Hyde\Pages\MarkdownPage;
12use Hyde\Console\Concerns\Command;
13
14use function strtolower;
15use function ucfirst;
16
17/**
18 * Scaffold a new Markdown or Blade page file.
19 */
20class 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>
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}