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
|
|
|
|