Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
HydeMakePageCommand
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 validateOptions
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace Hyde\Framework\Commands;
4
5use Hyde\Framework\Actions\CreatesNewPageSourceFile;
6use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
7use Hyde\Framework\Models\BladePage;
8use Hyde\Framework\Models\DocumentationPage;
9use Hyde\Framework\Models\MarkdownPage;
10use LaravelZero\Framework\Commands\Command;
11
12/**
13 * Hyde Command to scaffold a new Markdown or Blade page file.
14 *
15 * @see \Tests\Feature\Commands\HydeMakePageCommandTest
16 */
17class HydeMakePageCommand extends Command
18{
19    /**
20     * The name and signature of the console command.
21     *
22     * @var string
23     */
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        {--force : Overwrite any existing files}';
28
29    /**
30     * The console command description.
31     *
32     * @var string
33     */
34    protected $description = 'Scaffold a new Markdown, Blade, or documentation page file';
35
36    /**
37     * The page title.
38     */
39    public string $title;
40
41    /**
42     * The page type.
43     */
44    public string $type;
45
46    /**
47     * Can the file be overwritten?
48     */
49    public bool $force;
50
51    /**
52     * Execute the console command.
53     */
54    public function handle(): int
55    {
56        $this->title('Creating a new page!');
57
58        $this->title = $this->argument('title')
59            ?? $this->ask('What is the title of the page?')
60            ?? 'My New Page';
61
62        $this->line('<info>Creating page with title:</> '.$this->title."\n");
63
64        $this->validateOptions();
65
66        $this->force = $this->option('force') ?? false;
67
68        $creator = new CreatesNewPageSourceFile($this->title, $this->type, $this->force);
69
70        $this->info("Created file $creator->outputPath");
71
72        return 0;
73    }
74
75    /**
76     * Validate the options passed to the command.
77     *
78     * @return void
79     *
80     * @throws UnsupportedPageTypeException if the page type is invalid.
81     */
82    protected function validateOptions(): void
83    {
84        $type = strtolower($this->option('type') ?? 'markdown');
85
86        // Set the type to the fully qualified class name
87        if ($type === 'markdown') {
88            $this->type = MarkdownPage::class;
89
90            return;
91        }
92        if ($type === 'blade') {
93            $this->type = BladePage::class;
94
95            return;
96        }
97        if ($type === 'docs' || $type === 'documentation') {
98            $this->type = DocumentationPage::class;
99
100            return;
101        }
102
103        throw new UnsupportedPageTypeException("Invalid page type: $type", 400);
104    }
105}