1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of Laravel Love. |
||
5 | * |
||
6 | * (c) Anton Komarev <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Cog\Laravel\Love\Console\Commands; |
||
15 | |||
16 | use Cog\Laravel\Love\ReactionType\Models\ReactionType; |
||
17 | use Illuminate\Console\Command; |
||
18 | use Illuminate\Support\Str; |
||
19 | use Symfony\Component\Console\Input\InputOption; |
||
20 | |||
21 | final class ReactionTypeAdd extends Command |
||
22 | { |
||
23 | /** |
||
24 | * The console command name. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $name = 'love:reaction-type-add'; |
||
29 | |||
30 | /** |
||
31 | * The console command description. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $description = 'Add Reaction Type to Laravel Love'; |
||
36 | |||
37 | /** |
||
38 | * Execute the console command. |
||
39 | * |
||
40 | * @return int |
||
41 | */ |
||
42 | public function handle(): int |
||
43 | { |
||
44 | if ($this->option('default')) { |
||
45 | $this->createDefaultReactionTypes(); |
||
46 | |||
47 | return 0; |
||
48 | } |
||
49 | |||
50 | $name = $this->resolveName(); |
||
51 | $name = $this->sanitizeName($name); |
||
52 | |||
53 | if ($this->isNameInvalid($name)) { |
||
54 | $this->error(sprintf( |
||
55 | 'Reaction type with name `%s` is invalid.', |
||
56 | $name |
||
57 | )); |
||
58 | |||
59 | return 1; |
||
60 | } |
||
61 | |||
62 | if ($this->isReactionTypeNameExists($name)) { |
||
63 | $this->error(sprintf( |
||
64 | 'Reaction type with name `%s` already exists.', |
||
65 | $name |
||
66 | )); |
||
67 | |||
68 | return 1; |
||
69 | } |
||
70 | |||
71 | $this->createReactionType($name, $this->resolveMass()); |
||
72 | |||
73 | return 0; |
||
74 | } |
||
75 | |||
76 | protected function getOptions(): array |
||
77 | { |
||
78 | return [ |
||
79 | ['default', null, InputOption::VALUE_NONE, 'Create default Like & Dislike reactions'], |
||
80 | ['name', null, InputOption::VALUE_OPTIONAL, 'The name of the reaction'], |
||
81 | ['mass', null, InputOption::VALUE_OPTIONAL, 'The mass of the reaction'], |
||
82 | ]; |
||
83 | } |
||
84 | |||
85 | private function createDefaultReactionTypes(): void |
||
86 | { |
||
87 | $types = [ |
||
88 | [ |
||
89 | 'name' => 'Like', |
||
90 | 'mass' => 1, |
||
91 | ], |
||
92 | [ |
||
93 | 'name' => 'Dislike', |
||
94 | 'mass' => -1, |
||
95 | ], |
||
96 | ]; |
||
97 | |||
98 | foreach ($types as $type) { |
||
99 | if ($this->isReactionTypeNameExists($type['name'])) { |
||
100 | $this->line(sprintf( |
||
101 | 'Reaction type with name `%s` already exists.', |
||
102 | $type['name'] |
||
103 | )); |
||
104 | continue; |
||
105 | } |
||
106 | |||
107 | $this->createReactionType($type['name'], $type['mass']); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | private function createReactionType(string $name, int $mass): void |
||
112 | { |
||
113 | ReactionType::query()->create([ |
||
114 | 'name' => $name, |
||
115 | 'mass' => $mass, |
||
116 | ]); |
||
117 | |||
118 | $this->line(sprintf( |
||
119 | 'Reaction type with name `%s` and mass `%d` was added.', |
||
120 | $name, $mass |
||
121 | )); |
||
122 | } |
||
123 | |||
124 | private function resolveName(): string |
||
125 | { |
||
126 | return $this->option('name') |
||
127 | ?? $this->ask('How to name reaction type?') |
||
128 | ?? ''; |
||
129 | } |
||
130 | |||
131 | private function resolveMass(): int |
||
132 | { |
||
133 | $mass = $this->option('mass') |
||
134 | ?? $this->ask('What is the mass of this reaction type?') |
||
135 | ?? ReactionType::MASS_DEFAULT; |
||
136 | |||
137 | return intval($mass); |
||
138 | } |
||
139 | |||
140 | private function sanitizeName(string $name): string |
||
141 | { |
||
142 | $name = trim($name); |
||
143 | $name = Str::studly($name); |
||
144 | |||
145 | return $name; |
||
146 | } |
||
147 | |||
148 | private function isReactionTypeNameExists(string $name): bool |
||
149 | { |
||
150 | return ReactionType::query() |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
151 | ->where('name', $name) |
||
152 | ->exists(); |
||
153 | } |
||
154 | |||
155 | private function isNameInvalid(string $name): bool |
||
156 | { |
||
157 | return preg_match('#^[A-Z][a-zA-Z0-9_]*$#', $name) === 0; |
||
158 | } |
||
159 | } |
||
160 |