Passed
Pull Request — master (#51)
by Anton
02:43
created

ReactionTypeAdd::createReactionType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Contracts\Events\Dispatcher;
19
20
final class ReactionTypeAdd extends Command
21
{
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'love:reaction-type-add
28
                            {--default}
29
                            {name?}
30
                            {weight?}';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Add Reaction Type to Laravel Love';
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @param \Illuminate\Contracts\Events\Dispatcher $events
43
     * @return void
44
     */
45
    public function handle(
46
        Dispatcher $events
0 ignored issues
show
Unused Code introduced by
The parameter $events is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

46
        /** @scrutinizer ignore-unused */ Dispatcher $events

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    ): void {
48
        if ($this->option('default')) {
49
            $this->createDefaultReactionTypes();
50
51
            return;
52
        }
53
54
        $this->createReactionType($this->resolveName(), $this->resolveWeight());
55
    }
56
57
    private function createDefaultReactionTypes(): void
58
    {
59
        $types = [
60
            [
61
                'name' => 'Like',
62
                'weight' => 1,
63
            ],
64
            [
65
                'name' => 'Dislike',
66
                'weight' => -1,
67
            ],
68
        ];
69
70
        foreach ($types as $type) {
71
            if (ReactionType::query()->where('name', $type['name'])->exists()) {
72
                continue;
73
            }
74
75
            $this->createReactionType($type['name'], $type['weight']);
76
        }
77
    }
78
79
    private function createReactionType(string $name, int $weight): void
80
    {
81
        ReactionType::query()->create([
82
            'name' => $name,
83
            'weight' => $weight,
84
        ]);
85
86
        $this->line(sprintf(
87
            "Reaction type with name `%s` and weight `%d` was added.",
88
            $name,
89
            $weight
90
        ));
91
    }
92
93
    private function resolveName(): string
94
    {
95
        $name = $this->argument('name') ?? $this->ask('How to name reaction type?');
96
97
        if (is_null($name)) {
98
            $name = $this->resolveName();
99
        }
100
101
        return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name could return the type null|string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
104
    private function resolveWeight(): int
105
    {
106
        return intval($this->argument('weight') ?? $this->ask('What is the weight of this reaction type?'));
107
    }
108
}
109