1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @todo Mt bom, analisar |
5
|
|
|
*/ |
6
|
|
|
namespace Telefonica\Traits; |
7
|
|
|
|
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Illuminate\Database\Eloquent\Collection; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
13
|
|
|
use Casa\Models\Economic\Routine; |
14
|
|
|
|
15
|
|
|
trait HasRoutine |
16
|
|
|
{ |
17
|
|
|
protected $queuedRoutine = []; |
18
|
|
|
|
19
|
|
|
public static function getRoutineClassName(): string |
20
|
|
|
{ |
21
|
|
|
return Routine::class; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public static function bootHasRoutine() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
static::created( |
27
|
|
|
function (Model $routineableModel) { |
28
|
|
|
if (count($routineableModel->queuedRoutine) > 0) { |
29
|
|
|
$routineableModel->attachRoutine($routineableModel->queuedRoutine); |
30
|
|
|
|
31
|
|
|
$routineableModel->queuedRoutine = []; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
static::deleted( |
37
|
|
|
function (Model $deletedModel) { |
38
|
|
|
$routines = $deletedModel->routines()->get(); |
39
|
|
|
|
40
|
|
|
$deletedModel->detachRoutine($routines); |
41
|
|
|
} |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function routines(): MorphToMany |
46
|
|
|
{ |
47
|
|
|
return $this |
|
|
|
|
48
|
|
|
->morphToMany(self::getRoutineClassName(), 'routineable') |
49
|
|
|
->ordered(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $locale |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function routinesTranslated($locale = null): MorphToMany |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$locale = ! is_null($locale) ? $locale : app()->getLocale(); |
58
|
|
|
|
59
|
|
|
return $this |
|
|
|
|
60
|
|
|
->morphToMany(self::getRoutineClassName(), 'routineable') |
61
|
|
|
->select('*') |
62
|
|
|
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated") |
63
|
|
|
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated") |
64
|
|
|
->ordered(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string|array|\ArrayAccess|\\App\Models\Routine $routines |
69
|
|
|
*/ |
70
|
|
|
public function setRoutineAttribute($routines) |
71
|
|
|
{ |
72
|
|
|
if (! $this->exists) { |
|
|
|
|
73
|
|
|
$this->queuedRoutine = $routines; |
74
|
|
|
|
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->attachRoutine($routines); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
83
|
|
|
* @param array|\ArrayAccess|\\App\Models\Routine $routines |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function scopeWithAllRoutine(Builder $query, $routines, string $type = null): Builder |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$routines = static::convertToRoutine($routines, $type); |
90
|
|
|
|
91
|
|
|
collect($routines)->each( |
92
|
|
|
function ($routine) use ($query) { |
93
|
|
|
$query->whereHas( |
94
|
|
|
'routines', function (Builder $query) use ($routine) { |
95
|
|
|
$query->where('routines.id', $routine ? $routine->id : 0); |
96
|
|
|
} |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
return $query; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
106
|
|
|
* @param array|\ArrayAccess|\\App\Models\Routine $routines |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function scopeWithAnyRoutine(Builder $query, $routines, string $type = null): Builder |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$routines = static::convertToRoutine($routines, $type); |
113
|
|
|
|
114
|
|
|
return $query->whereHas( |
115
|
|
|
'routines', function (Builder $query) use ($routines) { |
116
|
|
|
$routineIds = collect($routines)->pluck('id'); |
117
|
|
|
|
118
|
|
|
$query->whereIn('routines.id', $routineIds); |
119
|
|
|
} |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function scopeWithAllRoutineOfAnyType(Builder $query, $routines): Builder |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$routines = static::convertToRoutineOfAnyType($routines); |
126
|
|
|
|
127
|
|
|
collect($routines)->each( |
128
|
|
|
function ($routine) use ($query) { |
129
|
|
|
$query->whereHas( |
130
|
|
|
'routines', function (Builder $query) use ($routine) { |
131
|
|
|
$query->where('routines.id', $routine ? $routine->id : 0); |
132
|
|
|
} |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $query; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function scopeWithAnyRoutineOfAnyType(Builder $query, $routines): Builder |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$routines = static::convertToRoutineOfAnyType($routines); |
143
|
|
|
|
144
|
|
|
return $query->whereHas( |
145
|
|
|
'routines', function (Builder $query) use ($routines) { |
146
|
|
|
$routineIds = collect($routines)->pluck('id'); |
147
|
|
|
|
148
|
|
|
$query->whereIn('routines.id', $routineIds); |
149
|
|
|
} |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function routinesWithType(string $type = null): Collection |
154
|
|
|
{ |
155
|
|
|
return $this->routines->filter( |
|
|
|
|
156
|
|
|
function (Routine $routine) use ($type) { |
157
|
|
|
return $routine->type === $type; |
158
|
|
|
} |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array|\ArrayAccess|\\App\Models\Routine $routines |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function attachRoutine($routines) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$className = static::getRoutineClassName(); |
170
|
|
|
|
171
|
|
|
$routines = collect($className::findOrCreate($routines)); |
172
|
|
|
|
173
|
|
|
$this->routines()->syncWithoutDetaching($routines->pluck('id')->toArray()); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string|\\App\Models\Routine $routine |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
|
|
public function detachRoutine($routine) |
184
|
|
|
{ |
185
|
|
|
return $this->detachRoutine([$routine]); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array|\ArrayAccess $routines |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function syncRoutine($routines) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$className = static::getRoutineClassName(); |
196
|
|
|
|
197
|
|
|
$routines = collect($className::findOrCreate($routines)); |
198
|
|
|
|
199
|
|
|
$this->routines()->sync($routines->pluck('id')->toArray()); |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array|\ArrayAccess $routines |
206
|
|
|
* @param string|null $type |
207
|
|
|
* |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function syncRoutineWithType($routines, string $type = null) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$className = static::getRoutineClassName(); |
213
|
|
|
|
214
|
|
|
$routines = collect($className::findOrCreate($routines, $type)); |
215
|
|
|
|
216
|
|
|
$this->syncRoutineIds($routines->pluck('id')->toArray(), $type); |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
protected static function convertToRoutine($values, $type = null, $locale = null) |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
return collect($values)->map( |
224
|
|
|
function ($value) use ($type, $locale) { |
225
|
|
|
if ($value instanceof Routine) { |
|
|
|
|
226
|
|
|
if (isset($type) && $value->type != $type) { |
227
|
|
|
throw new InvalidArgumentException("Type was set to {$type} but routine is of type {$value->type}"); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $value; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$className = static::getRoutineClassName(); |
234
|
|
|
|
235
|
|
|
return $className::findFromString($value, $type, $locale); |
236
|
|
|
} |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
View Code Duplication |
protected static function convertToRoutineOfAnyType($values, $locale = null) |
|
|
|
|
241
|
|
|
{ |
242
|
|
|
return collect($values)->map( |
243
|
|
|
function ($value) use ($locale) { |
244
|
|
|
if ($value instanceof Routine) { |
|
|
|
|
245
|
|
|
return $value; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$className = static::getRoutineClassName(); |
249
|
|
|
|
250
|
|
|
return $className::findFromStringOfAnyType($value, $locale); |
251
|
|
|
} |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Use in place of eloquent's sync() method so that the routine type may be optionally specified. |
257
|
|
|
* |
258
|
|
|
* @param $ids |
259
|
|
|
* @param string|null $type |
260
|
|
|
* @param bool $detaching |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
protected function syncRoutineIds($ids, string $type = null, $detaching = true) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$isUpdated = false; |
265
|
|
|
|
266
|
|
|
// Get a list of routine_ids for all current routines |
267
|
|
|
$current = $this->routines() |
268
|
|
|
->newPivotStatement() |
269
|
|
|
->where('routineable_id', $this->getKey()) |
|
|
|
|
270
|
|
|
->where('routineable_type', $this->getMorphClass()) |
|
|
|
|
271
|
|
|
->when( |
272
|
|
|
$type !== null, function ($query) use ($type) { |
273
|
|
|
$routineModel = $this->routines()->getRelated(); |
274
|
|
|
|
275
|
|
|
return $query->join( |
276
|
|
|
$routineModel->getTable(), |
277
|
|
|
'routineables.routine_id', |
278
|
|
|
'=', |
279
|
|
|
$routineModel->getTable().'.'.$routineModel->getKeyName() |
280
|
|
|
) |
281
|
|
|
->where('routines.type', $type); |
282
|
|
|
} |
283
|
|
|
) |
284
|
|
|
->pluck('routine_id') |
285
|
|
|
->all(); |
286
|
|
|
|
287
|
|
|
// Compare to the list of ids given to find the routines to remove |
288
|
|
|
$detach = array_diff($current, $ids); |
289
|
|
|
if ($detaching && count($detach) > 0) { |
290
|
|
|
$this->routines()->detach($detach); |
291
|
|
|
$isUpdated = true; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Attach any new ids |
295
|
|
|
$attach = array_diff($ids, $current); |
296
|
|
|
if (count($attach) > 0) { |
297
|
|
|
collect($attach)->each( |
298
|
|
|
function ($id) { |
299
|
|
|
$this->routines()->attach($id, []); |
300
|
|
|
} |
301
|
|
|
); |
302
|
|
|
$isUpdated = true; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// Once we have finished attaching or detaching the records, we will see if we |
306
|
|
|
// have done any attaching or detaching, and if we have we will touch these |
307
|
|
|
// relationships if they are configured to touch on any database updates. |
308
|
|
|
if ($isUpdated) { |
309
|
|
|
$this->routines()->touchIfTouching(); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.