GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

NewArticle::validateDateInput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AloiaCms\Console;
4
5
use Carbon\Carbon;
6
use AloiaCms\Models\Article;
7
use Illuminate\Console\Command;
8
9
class NewArticle extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'aloiacms:new:article {--slug=} {--post_date=} {--file_type=md}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Create a new article entry with the additional requirements';
24
25
    protected $tasks = [];
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return mixed
31
     * @throws \Exception
32
     */
33 4
    public function handle()
34
    {
35 4
        $this->validateInput();
36
37 2
        $this->createNewPost();
38
39 2
        $this->info("Created new post entry for {$this->option('slug')}");
40 2
    }
41
42
    /**
43
     * Generate a post entry and set the post as a draft status
44
     *
45
     * @return void
46
     */
47 2
    private function createNewPost(): void
48
    {
49 2
        Article::find($this->option('slug'))
50 2
            ->setExtension($this->option('file_type'))
51 2
            ->setMatter([
52 2
                "post_date" => $this->option('post_date') ?? date('Y-m-d'),
53 2
                "description" => "",
54
                "is_published" => false,
55
                "is_scheduled" => false
56
            ])
57 2
            ->save();
58 2
    }
59
60
    /**
61
     * Validate the command input
62
     * @throws \Exception
63
     */
64 4
    private function validateInput()
65
    {
66 4
        if (! $this->option('slug')) {
67 1
            throw new \Exception("You need to submit a slug using --slug");
68
        }
69
70 3
        if ($this->option('post_date')) {
71 2
            $this->validateDateInput();
72
        }
73 2
    }
74
75
    /**
76
     * Validate the date input to be the correct format
77
     * @throws \Exception
78
     */
79 2
    private function validateDateInput()
80
    {
81
        try {
82 2
            Carbon::createFromFormat('Y-m-d', $this->option('post_date'));
83 1
        } catch (\Exception $exception) {
84 1
            throw new \Exception("You need to submit the date with the following format: Y-m-d");
85
        }
86 1
    }
87
}
88