Completed
Push — master ( d1538b...42b84f )
by Anton
03:22
created

ReactionTypeAdd::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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,
121
            $mass
122
        ));
123
    }
124
125
    private function resolveName(): string
126
    {
127
        return $this->option('name')
128
            ?? $this->ask('How to name reaction type?')
129
            ?? '';
130
    }
131
132
    private function resolveMass(): int
133
    {
134
        $mass = $this->option('mass')
135
            ?? $this->ask('What is the mass of this reaction type?')
136
            ?? ReactionType::MASS_DEFAULT;
137
138
        return intval($mass);
139
    }
140
141
    private function sanitizeName(string $name): string
142
    {
143
        $name = trim($name);
144
        $name = Str::studly($name);
145
146
        return $name;
147
    }
148
149
    private function isReactionTypeNameExists(string $name): bool
150
    {
151
        return ReactionType::query()
0 ignored issues
show
Bug Best Practice introduced by
The expression return Cog\Laravel\Love\...name', $name)->exists() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
152
            ->where('name', $name)
153
            ->exists();
154
    }
155
156
    private function isNameInvalid(string $name): bool
157
    {
158
        return preg_match('#^[A-Z][a-zA-Z0-9_]*$#', $name) === 0;
159
    }
160
}
161