Passed
Push — master ( 6bbf15...aead9d )
by Jonathan
19:29
created

UccelloModule::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Uccello\Core\Support\Traits;
4
5
use Uccello\Core\Models\Entity;
6
use Uccello\Core\Models\Module;
7
use Uccello\Core\Models\Filter;
8
use Illuminate\Support\Facades\Cache;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Uccello\Core\Support\Scopes;
10
use Illuminate\Support\Facades\Auth;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Illuminate\Support\Facades\Schema;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Uccello\Core\Models\Domain;
13
14
trait UccelloModule
15
{
16
    /**
17
     * The "booting" method of the trait.
18
     *
19
     * @return void
20
     */
21
    protected static function bootUccelloModule()
22
    {
23
        if (static::isFilteredByUser()) {
24
            static::addGlobalScope(new Scopes\AssignedUser);
25
        }
26
    }
27
28
    protected static function isFilteredByUser()
29
    {
30
        $isFilteredByUser = false;
31
32
        $user = Auth::user();
33
34
        if ($user && !$user->is_admin) {
35
            $module = static::getModuleFromClass(static::class);
36
37
            if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) {
38
                $isFilteredByUser = true;
39
            }
40
        }
41
42
        return $isFilteredByUser;
43
    }
44
45
    public function getTableAttribute()
46
    {
47
        return $this->table;
48
    }
49
50
    public function getModuleAttribute()
51
    {
52
        return static::getModuleFromClass(get_class($this));
53
    }
54
55
    protected static function getModuleFromClass($className)
56
    {
57
        $modules = Cache::rememberForever('modules_by_model_class', function () {
58
            $modulesGroupedByModelClass = collect();
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
            $modulesGroupedByModelClass = /** @scrutinizer ignore-call */ collect();
Loading history...
59
            Module::all()->map(function ($item) use ($modulesGroupedByModelClass) {
60
                $modulesGroupedByModelClass[$item->model_class] = $item;
61
                return $modulesGroupedByModelClass;
62
            });
63
            return $modulesGroupedByModelClass;
64
        });
65
        return $modules[(string) $className] ?? null;
66
    }
67
68
    public function getUuidAttribute()
69
    {
70
        $uuid = null;
71
72
        $entity = Entity::where('module_id', $this->module->getKey())
73
                        ->where('record_id', $this->getKey())
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

73
                        ->where('record_id', $this->/** @scrutinizer ignore-call */ getKey())
Loading history...
74
                        ->first();
75
76
        if ($entity) {
77
            $uuid = $entity->getKey();
78
        }
79
80
        return $uuid;
81
    }
82
83
    /**
84
     * Returns Assigned User
85
     *
86
     * @return string|null
87
     */
88
    public function getAssignedUserAttribute(): ?string
89
    {
90
        return $this->assigned_user_id;
91
    }
92
93
    public function scopeInDomain($query, ?Domain $domain, $withDescendants = false)
94
    {
95
        if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) {
96
            // Activate descendant view if the user is allowed
97
            if (Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) {
98
                $domainsIds = $domain->findDescendants()->pluck('id');
99
                $query = $query->whereIn('domain_id', $domainsIds);
100
            } else {
101
                $query = $query->where('domain_id', $domain->id);
102
            }
103
        }
104
105
        return $query;
106
    }
107
108
    /**
109
     * Returns Assigned User
110
     * @param QueryBuilder $query
0 ignored issues
show
Bug introduced by
The type Uccello\Core\Support\Traits\QueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
     * @param Field|int|array $filter
0 ignored issues
show
Bug introduced by
The type Uccello\Core\Support\Traits\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
112
     * @return QueryBuilder|null
113
     */
114
    public function scopeFilterBy($query, $filter)
115
    {
116
        $filterModel = null;
117
118
        if (!empty($filter)) {
119
            // $filter: int id
120
            if (is_numeric($filter)) {
121
                // TODO: Check permissions ?? (domain, user)
122
                $filterModel = Filter::where('id', $filter)
123
                                        ->where('module_id', $this->module->getKey())
124
                                        ->first();
125
            }
126
            // $filter: array data
127
            elseif (is_array($filter)) {
128
                $filterModel = Filter::newFromData($filter);
129
            }
130
            // $filter: Filter model
131
            elseif (substr(strrchr(get_class($filter), "\\"), 1) == 'Filter') {
132
                $filterModel = $filter;
133
            }
134
135
            if ($filterModel) {
136
                // Conditions
137
                if (!empty($filterModel->conditions)) {
138
                    // Search
139
                    if (!empty($filterModel->conditions->search)) {
140
                        foreach ($filterModel->conditions->search as $fieldName => $searchValue) {
141
                            // Get field by name and search by field column
142
                            $field = $this->module->getField($fieldName);
143
                            if (isset($searchValue) && !is_null($field)) {
144
                                $uitype = uitype($field->uitype_id);
145
                                $query = $uitype->addConditionToSearchQuery($query, $field, $searchValue);
146
                            }
147
                        }
148
                    }
149
                }
150
151
                // Order results
152
                if (!empty($filterModel->order)) {
153
                    foreach ($filterModel->order as $fieldName => $value) {
154
                        // Get field by name and search by field column
155
                        $field = $this->module->getField($fieldName);
156
                        if (!is_null($field)) {
157
                            $query = $query->orderBy($field->column, $value);
158
                        }
159
                    }
160
                }
161
            }
162
        }
163
164
        return $query;
165
    }
166
}
167