LikeableRecountCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 116
rs 10
c 1
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeModelType() 0 21 4
A handle() 0 10 2
A recountLikesOfAllModelTypes() 0 5 2
A recountLikesOfModelType() 0 11 1
1
<?php
2
3
namespace Turahe\Likeable\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Turahe\Likeable\Contracts\Like as LikeContract;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
use Turahe\Likeable\Exceptions\ModelInvalidException;
11
use Turahe\Likeable\Contracts\Likeable as LikeableContract;
12
use Turahe\Likeable\Contracts\LikeCounter as LikeCounterContract;
13
use Turahe\Likeable\Services\LikeableService as LikeableServiceContract;
14
15
class LikeableRecountCommand extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'likeable:recount {model?} {type?}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Recount likes and dislikes for the models';
30
31
    /**
32
     * Type of likes to be recounted.
33
     *
34
     * @var string|null
35
     */
36
    protected $likeType;
37
38
    /**
39
     * Likeable service.
40
     *
41
     * @var \Turahe\Likeable\Contracts\LikeableService
42
     */
43
    protected $service;
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @param \Illuminate\Contracts\Events\Dispatcher $events
49
     * @return void
50
     *
51
     * @throws \Turahe\Likeable\Exceptions\ModelInvalidException
52
     */
53
    public function handle(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

53
    public function handle(/** @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...
54
    {
55
        $model = $this->argument('model');
56
        $this->likeType = $this->argument('type');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->argument('type') can also be of type string[]. However, the property $likeType is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
57
        $this->service = app(LikeableServiceContract::class);
58
59
        if (empty($model)) {
60
            $this->recountLikesOfAllModelTypes();
61
        } else {
62
            $this->recountLikesOfModelType($model);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type string[]; however, parameter $modelType of Turahe\Likeable\Console\...countLikesOfModelType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

62
            $this->recountLikesOfModelType(/** @scrutinizer ignore-type */ $model);
Loading history...
63
        }
64
    }
65
66
    /**
67
     * Recount likes of all model types.
68
     *
69
     * @return void
70
     *
71
     * @throws \Turahe\Likeable\Exceptions\ModelInvalidException
72
     */
73
    protected function recountLikesOfAllModelTypes()
74
    {
75
        $likeableTypes = app(LikeContract::class)->groupBy('likeable_type')->get();
0 ignored issues
show
Bug introduced by
The method groupBy() does not exist on Turahe\Likeable\Contracts\Like. Since it exists in all sub-types, consider adding an abstract or default implementation to Turahe\Likeable\Contracts\Like. ( Ignorable by Annotation )

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

75
        $likeableTypes = app(LikeContract::class)->/** @scrutinizer ignore-call */ groupBy('likeable_type')->get();
Loading history...
76
        foreach ($likeableTypes as $like) {
77
            $this->recountLikesOfModelType($like->likeable_type);
78
        }
79
    }
80
81
    /**
82
     * Recount likes of model type.
83
     *
84
     * @param string $modelType
85
     * @return void
86
     *
87
     * @throws \Turahe\Likeable\Exceptions\ModelInvalidException
88
     */
89
    protected function recountLikesOfModelType($modelType)
90
    {
91
        $modelType = $this->normalizeModelType($modelType);
92
93
        $counters = $this->service->fetchLikesCounters($modelType, $this->likeType);
94
95
        $this->service->removeLikeCountersOfType($modelType, $this->likeType);
96
97
        DB::table(app(LikeCounterContract::class)->getTable())->insert($counters);
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on Turahe\Likeable\Contracts\LikeCounter. Since it exists in all sub-types, consider adding an abstract or default implementation to Turahe\Likeable\Contracts\LikeCounter. ( Ignorable by Annotation )

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

97
        DB::table(app(LikeCounterContract::class)->/** @scrutinizer ignore-call */ getTable())->insert($counters);
Loading history...
98
99
        $this->info('All ['.$modelType.'] records likes has been recounted.');
100
    }
101
102
    /**
103
     * Normalize likeable model type.
104
     *
105
     * @param string $modelType
106
     * @return string
107
     *
108
     * @throws \Turahe\Likeable\Exceptions\ModelInvalidException
109
     */
110
    protected function normalizeModelType($modelType)
111
    {
112
        $morphMap = Relation::morphMap();
113
114
        if (class_exists($modelType)) {
115
            $model = new $modelType;
116
            $modelType = $model->getMorphClass();
117
        } else {
118
            if (! isset($morphMap[$modelType])) {
119
                throw new ModelInvalidException("[$modelType] class and morph map are not found.");
120
            }
121
122
            $modelClass = $morphMap[$modelType];
123
            $model = new $modelClass;
124
        }
125
126
        if (! $model instanceof LikeableContract) {
127
            throw new ModelInvalidException("[$modelType] not implements Likeable contract.");
128
        }
129
130
        return $modelType;
131
    }
132
}
133