1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreates\Projects\Commands; |
4
|
|
|
|
5
|
|
|
use Chriscreates\Projects\Models\Task; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
class CreateTaskCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'projects:create-task'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Laravel Artisan Command to create a new task.'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Task model. |
26
|
|
|
* |
27
|
|
|
* @var object |
28
|
|
|
*/ |
29
|
|
|
private $task; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new command instance. |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function __construct(Task $task) |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
|
40
|
|
|
$this->task = $task; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the console command. |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function handle() |
49
|
|
|
{ |
50
|
|
|
$details = $this->getDetails(); |
51
|
|
|
|
52
|
|
|
$task = $this->task->create($details); |
53
|
|
|
|
54
|
|
|
$this->display($task); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Ask for task details. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
private function getDetails() : array |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'title' => $this->ask('Title'), |
66
|
|
|
'description' => $this->ask('Description'), |
67
|
|
|
'notes' => $this->ask('Notes'), |
68
|
|
|
'complete' => $this->ask('Complete'), |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Display created task. |
74
|
|
|
* |
75
|
|
|
* @param array $task |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
private function display(Task $task) : void |
79
|
|
|
{ |
80
|
|
|
$headers = ['Title', 'Description', 'Notes', 'Complete']; |
81
|
|
|
|
82
|
|
|
$fields = [ |
83
|
|
|
'title' => $task->title, |
84
|
|
|
'description' => $task->description, |
85
|
|
|
'notes' => $task->notes, |
86
|
|
|
'complete' => $task->complete, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$this->info('Task created!'); |
90
|
|
|
$this->table($headers, [$fields]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.