Total Complexity | 40 |
Total Lines | 180 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Complex classes like UccelloModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UccelloModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait UccelloModule |
||
16 | { |
||
17 | use RelatedlistTrait; |
||
18 | |||
19 | /** |
||
20 | * The "booting" method of the trait. |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | protected static function bootUccelloModule() |
||
25 | { |
||
26 | if (static::isFilteredByUser()) { |
||
27 | static::addGlobalScope(new Scopes\AssignedUser); |
||
28 | } |
||
29 | |||
30 | // Create uuid after save |
||
31 | static::created(function ($model) { |
||
32 | $module = Module::where('model_class', get_class($model))->first(); |
||
33 | if ($module) { |
||
34 | Entity::create([ |
||
35 | 'id' => (string) Str::uuid(), |
||
36 | 'module_id' => $module->id, |
||
37 | 'record_id' => $model->getKey(), |
||
38 | ]); |
||
39 | } |
||
40 | }); |
||
41 | |||
42 | // Delete uuid after forced delete |
||
43 | static::deleted(function ($model) { |
||
44 | if (!empty($model->uuid) && (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting() === true)) { |
||
45 | $entity = Entity::find($model->uuid); |
||
46 | if ($entity) { |
||
47 | $entity->delete(); |
||
48 | } |
||
49 | } |
||
50 | }); |
||
51 | } |
||
52 | |||
53 | public function initializeUccelloModule() |
||
54 | { |
||
55 | $this->appends = array_merge($this->appends, ['recordLabel','uuid']); |
||
|
|||
56 | } |
||
57 | |||
58 | protected static function isFilteredByUser() |
||
59 | { |
||
60 | $isFilteredByUser = false; |
||
61 | |||
62 | $user = Auth::user(); |
||
63 | |||
64 | if ($user && !$user->is_admin) { |
||
65 | $module = static::getModuleFromClass(static::class); |
||
66 | |||
67 | if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
||
68 | $isFilteredByUser = true; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $isFilteredByUser; |
||
73 | } |
||
74 | |||
75 | public function getTableAttribute() |
||
76 | { |
||
77 | return $this->table; |
||
78 | } |
||
79 | |||
80 | public function getModuleAttribute() |
||
81 | { |
||
82 | return static::getModuleFromClass(get_class($this)); |
||
83 | } |
||
84 | |||
85 | protected static function getModuleFromClass($className) |
||
86 | { |
||
87 | $modules = Cache::rememberForever('modules_by_model_class', function () { |
||
88 | $modulesGroupedByModelClass = collect(); |
||
89 | Module::all()->map(function ($item) use ($modulesGroupedByModelClass) { |
||
90 | $modulesGroupedByModelClass[$item->model_class] = $item; |
||
91 | return $modulesGroupedByModelClass; |
||
92 | }); |
||
93 | return $modulesGroupedByModelClass; |
||
94 | }); |
||
95 | return $modules[(string) $className] ?? null; |
||
96 | } |
||
97 | |||
98 | public function getUuidAttribute() |
||
99 | { |
||
100 | $uuid = null; |
||
101 | |||
102 | $entity = Entity::where('module_id', $this->module->getKey()) |
||
103 | ->where('record_id', $this->getKey()) |
||
104 | ->first(); |
||
105 | |||
106 | if ($entity) { |
||
107 | $uuid = $entity->getKey(); |
||
108 | } |
||
109 | |||
110 | return $uuid; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns Assigned User |
||
115 | * |
||
116 | * @return string|null |
||
117 | */ |
||
118 | public function getAssignedUserAttribute(): ?string |
||
121 | } |
||
122 | |||
123 | public function scopeInDomain($query, ?Domain $domain, $withDescendants = false) |
||
124 | { |
||
125 | if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
||
126 | // Activate descendant view if the user is allowed |
||
127 | if (Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
||
128 | $domainsIds = $domain->findDescendants()->pluck('id'); |
||
129 | $query = $query->whereIn('domain_id', $domainsIds); |
||
130 | } else { |
||
131 | $query = $query->where('domain_id', $domain->id); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | return $query; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Returns Assigned User |
||
140 | * @param QueryBuilder $query |
||
141 | * @param Field|int|array $filter |
||
142 | * @return QueryBuilder|null |
||
143 | */ |
||
144 | public function scopeFilterBy($query, $filter) |
||
195 | } |
||
196 | } |
||
197 |