1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moo\FlashCard\Command; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Moo\FlashCard\Entity\Card; |
8
|
|
|
use Moo\FlashCard\Entity\Category; |
9
|
|
|
use Moo\FlashCard\Traits\AskAndValidate; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* CreateCardCommand is a command line class for creating a new card. |
13
|
|
|
* |
14
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class CreateCard extends Command |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
use AskAndValidate; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'flashcard:card {--A|active= : If set, the card is going to be active.}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Create a card'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Card |
36
|
|
|
*/ |
37
|
|
|
protected $entity; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Instance of card model |
42
|
|
|
* |
43
|
|
|
* @return Model |
44
|
|
|
*/ |
45
|
1 |
|
protected function getEntity(): Model |
46
|
|
|
{ |
47
|
1 |
|
if (is_null($this->entity)) { |
48
|
1 |
|
$this->entity = new Card([ |
49
|
1 |
|
'active' => (bool) $this->option('active'), |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $this->entity; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the command line to create a new card. |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
1 |
|
public function handle() |
62
|
|
|
{ |
63
|
|
|
// Get collection of categories - can complete process if no categories found |
64
|
1 |
|
$choices = Category::all()->pluck('title', 'id')->toArray(); |
65
|
1 |
|
if (count($choices) === 0) { |
66
|
|
|
$this->error('Add category first before creating cards.'); |
67
|
|
|
|
68
|
|
|
return 1; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Ask for card title |
72
|
1 |
|
$this->askWithValidation('Please enter card title', 'title'); |
73
|
|
|
|
74
|
|
|
// Ask for card content |
75
|
1 |
|
$this->askWithValidation('Please enter card content', 'content'); |
76
|
|
|
|
77
|
|
|
// Ask for card category & set card category |
78
|
1 |
|
$category = $this->choice('Please select card category', $choices, key($choices)); |
79
|
1 |
|
$this->getEntity()->fill([ |
80
|
1 |
|
'category_id' => array_search($category, $choices, true), |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
// Ask for card meta description |
84
|
1 |
|
$this->askWithValidation('Please enter card SEO meta description', 'meta_description'); |
85
|
|
|
|
86
|
|
|
// Save entity and display message |
87
|
1 |
|
$this->getEntity()->save(); |
88
|
1 |
|
$this->info('Voila... You have created a new card.'); |
89
|
|
|
|
90
|
1 |
|
return 0; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.