Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
HydeMakePostCommand
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace Hyde\Framework\Commands;
4
5use Exception;
6use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
7use LaravelZero\Framework\Commands\Command;
8
9/**
10 * Hyde Command to scaffold a new Markdown Post.
11 */
12class HydeMakePostCommand extends Command
13{
14    /**
15     * The signature of the command.
16     *
17     * @var string
18     */
19    protected $signature = 'make:post
20                            {title? : The title for the Post. Will be used to generate the slug}
21                            {--force : Should the generated file overwrite existing posts with the same slug?}';
22
23    /**
24     * The description of the command.
25     *
26     * @var string
27     */
28    protected $description = 'Scaffold a new Markdown blog post file';
29
30    /**
31     * Execute the console command.
32     *
33     * @return int
34     */
35    public function handle(): int
36    {
37        $this->title('Creating a new post!');
38
39        $this->line(
40            $this->argument('title')
41                ? '<info>Selected title: '.$this->argument('title')."</info>\n"
42                : 'Please enter the title of the post, it will be used to generate the slug.'
43        );
44
45        $title = $this->argument('title')
46            ?? $this->ask('What is the title of the post?')
47            ?? 'My New Post';
48
49        $this->line('Tip: You can just hit return to use the defaults.');
50        $description = $this->ask('Write a short post excerpt/description');
51        $author = $this->ask('What is your (the author\'s) name?');
52        $category = $this->ask('What is the primary category of the post?');
53
54        $this->info('Creating a post with the following details:');
55        $creator = new CreatesNewMarkdownPostFile(
56            title: $title,
57            description: $description,
58            category: $category,
59            author: $author
60        );
61
62        $this->line("Title: $creator->title");
63        $this->line("Description: $creator->description");
64        $this->line("Author: $creator->author");
65        $this->line("Category: $creator->category");
66        $this->line("Date: $creator->date");
67        $this->line("Slug: $creator->slug");
68
69        if (! $this->confirm('Do you wish to continue?', true)) {
70            $this->info('Aborting.');
71
72            return 130;
73        }
74
75        try {
76            $path = $creator->save($this->option('force'));
77            $this->info("Post created! File is saved to $path");
78
79            return 0;
80        } catch (Exception $exception) {
81            $this->error('Something went wrong when trying to save the file!');
82            $this->warn($exception->getMessage());
83            if ($exception->getCode() === 409) {
84                $this->comment('If you want to overwrite the file supply the --force flag.');
85            }
86
87            return $exception->getCode();
88        }
89    }
90}