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) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$model = $this->argument('model'); |
56
|
|
|
$this->likeType = $this->argument('type'); |
|
|
|
|
57
|
|
|
$this->service = app(LikeableServiceContract::class); |
58
|
|
|
|
59
|
|
|
if (empty($model)) { |
60
|
|
|
$this->recountLikesOfAllModelTypes(); |
61
|
|
|
} else { |
62
|
|
|
$this->recountLikesOfModelType($model); |
|
|
|
|
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(); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.