Total Complexity | 45 |
Total Lines | 219 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 6 | 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 | 'creator_id' => auth()->id(), |
||
39 | ]); |
||
40 | } |
||
41 | }); |
||
42 | |||
43 | // Delete uuid after forced delete |
||
44 | static::deleted(function ($model) { |
||
45 | if (!empty($model->uuid) && (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting() === true)) { |
||
46 | $entity = Entity::find($model->uuid); |
||
47 | if ($entity) { |
||
48 | $entity->delete(); |
||
49 | } |
||
50 | } |
||
51 | }); |
||
52 | } |
||
53 | |||
54 | public function initializeUccelloModule() |
||
57 | } |
||
58 | |||
59 | protected static function isFilteredByUser() |
||
60 | { |
||
61 | $isFilteredByUser = false; |
||
62 | |||
63 | $user = Auth::user(); |
||
64 | |||
65 | if ($user && !$user->is_admin) { |
||
66 | $module = static::getModuleFromClass(static::class); |
||
67 | |||
68 | if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
||
69 | $isFilteredByUser = true; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | return $isFilteredByUser; |
||
74 | } |
||
75 | |||
76 | public function getTableAttribute() |
||
79 | } |
||
80 | |||
81 | public function getModuleAttribute() |
||
84 | } |
||
85 | |||
86 | protected static function getModuleFromClass($className) |
||
97 | } |
||
98 | |||
99 | public function getUuidAttribute() |
||
100 | { |
||
101 | $uuid = null; |
||
102 | |||
103 | $module = $this->module; |
||
104 | |||
105 | if ($module) { |
||
106 | return Cache::rememberForever('uuid_'.$module->getKey().'_'.$this->getKey(), function () use ($module) { |
||
107 | $entity = Entity::where('module_id', $module->getKey()) |
||
108 | ->where('record_id', $this->getKey()) |
||
109 | ->first(); |
||
110 | |||
111 | $uuid = null; |
||
112 | if ($entity) { |
||
113 | $uuid = $entity->getKey(); |
||
114 | } |
||
115 | |||
116 | return $uuid; |
||
117 | }); |
||
118 | } |
||
119 | |||
120 | return $uuid; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Returns Assigned User |
||
125 | * |
||
126 | * @return string|null |
||
127 | */ |
||
128 | public function getAssignedUserAttribute(): ?string |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns user who created the entity, if defined. |
||
135 | * |
||
136 | * @return \Uccello\Core\Models\User|null |
||
137 | */ |
||
138 | public function getCreatorAttribute() |
||
155 | } |
||
156 | |||
157 | public function scopeInDomain($query, ?Domain $domain, $withDescendants = false) |
||
158 | { |
||
159 | if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
||
160 | // Activate descendant view if the user is allowed |
||
161 | if (Auth::user() && Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
||
162 | $query->whereIn('domain_id', function ($query) use ($domain) { |
||
163 | $query->select('id') |
||
164 | ->from((new Domain)->getTable()) |
||
165 | ->where('path', 'like', $domain->id.'/%') |
||
166 | ->orWhere('path', 'like', '%/'.$domain->id.'/%') |
||
167 | ->get(); |
||
168 | }); |
||
169 | } else { |
||
170 | $query->where('domain_id', $domain->id); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | return $query; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Add filter conditions to a query. |
||
179 | * @param QueryBuilder $query |
||
180 | * @param Filter|int|array $filter |
||
181 | * @return QueryBuilder|null |
||
182 | */ |
||
183 | public function scopeFilterBy($query, $filter) |
||
234 | } |
||
235 | } |
||
236 |