Passed
Push — master ( 0f9eb8...340202 )
by Jonathan
24:22
created

UccelloModule::getAssignedUserAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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;
9
use Uccello\Core\Support\Scopes;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\Facades\Schema;
12
use Uccello\Core\Models\Domain;
13
14
trait UccelloModule
15
{
16
    /**
17
     * The "booting" method of the model.
18
     *
19
     * @return void
20
     */
21
    protected static function boot()
22
    {
23
        parent::boot();
24
25
        if (static::isFilteredByUser()) {
26
            static::addGlobalScope(new Scopes\AssignedUser);
27
        }
28
    }
29
30
    protected static function isFilteredByUser()
31
    {
32
        $isFilteredByUser = false;
33
34
        $user = Auth::user();
35
36
        if ($user && !$user->is_admin) {
0 ignored issues
show
Bug introduced by
Accessing is_admin on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
37
            $module = static::getModuleFromClass(static::class);
38
39
            if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) {
40
                $isFilteredByUser = true;
41
            }
42
        }
43
44
        return $isFilteredByUser;
45
    }
46
47
    public function getTableAttribute()
48
    {
49
        return $this->table;
50
    }
51
52
    public function getModuleAttribute()
53
    {
54
        return static::getModuleFromClass(get_class($this));
55
    }
56
57
    protected static function getModuleFromClass($className)
58
    {
59
        $modules = Cache::rememberForever('modules_by_model_class', function () {
60
            $modulesGroupedByModelClass = collect();
61
            Module::all()->map(function ($item) use ($modulesGroupedByModelClass) {
62
                $modulesGroupedByModelClass[$item->model_class] = $item;
63
                return $modulesGroupedByModelClass;
64
            });
65
            return $modulesGroupedByModelClass;
66
        });
67
        return $modules[(string) $className] ?? null;
68
    }
69
70
    public function getUuidAttribute()
71
    {
72
        $uuid = null;
73
74
        $entity = Entity::where('module_id', $this->module->getKey())
75
                        ->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

75
                        ->where('record_id', $this->/** @scrutinizer ignore-call */ getKey())
Loading history...
76
                        ->first();
77
78
        if ($entity) {
79
            $uuid = $entity->getKey();
80
        }
81
82
        return $uuid;
83
    }
84
85
    /**
86
     * Returns Assigned User
87
     *
88
     * @return string|null
89
     */
90
    public function getAssignedUserAttribute(): ?string
91
    {
92
        return $this->assigned_user_id;
93
    }
94
95
    public function scopeInDomain($query, ?Domain $domain, $withDescendants = false)
96
    {
97
        if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) {
98
            // Activate descendant view if the user is allowed
99
            if (Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) {
0 ignored issues
show
Bug introduced by
The method canSeeDescendantsRecords() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

99
            if (Auth::user()->/** @scrutinizer ignore-call */ canSeeDescendantsRecords($domain) && $withDescendants) {
Loading history...
100
                $domainsIds = $domain->findDescendants()->pluck('id');
101
                $query = $query->whereIn('domain_id', $domainsIds);
102
            } else {
103
                $query = $query->where('domain_id', $domain->id);
104
            }
105
        }
106
107
        return $query;
108
    }
109
110
    /**
111
     * Returns Assigned User
112
     * @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...
113
     * @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...
114
     * @return QueryBuilder|null
115
     */
116
    public function scopeFilterBy($query, $filter)
117
    {
118
        $filterModel = null;
119
120
        if (!empty($filter)) {
121
            // $filter: int id
122
            if (is_numeric($filter)) {
123
                // TODO: Check permissions ?? (domain, user)
124
                $filterModel = Filter::where('id', $filter)
125
                                        ->where('module_id', $this->module->getKey())
126
                                        ->first();
127
            }
128
            // $filter: array data
129
            elseif (is_array($filter)) {
130
                $filterModel = Filter::newFromData($filter);
131
            }
132
            // $filter: Filter model
133
            elseif (substr(strrchr(get_class($filter), "\\"), 1) == 'Filter') {
134
                $filterModel = $filter;
135
            }
136
            
137
            if ($filterModel) {
138
                // Conditions
139
                if (!empty($filterModel->conditions)) {
140
                    // Search
141
                    if (!empty($filterModel->conditions->search)) {
0 ignored issues
show
Bug introduced by
The property conditions does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
142
                        foreach ($filterModel->conditions->search as $fieldName => $searchValue) {
143
                            // Get field by name and search by field column
144
                            $field = $this->module->getField($fieldName);
145
                            if (isset($searchValue) && !is_null($field)) {
146
                                $uitype = uitype($field->uitype_id);
147
                                $query = $uitype->addConditionToSearchQuery($query, $field, $searchValue);
148
                            }
149
                        }
150
                    }
151
                }
152
    
153
                // Order results
154
                if (!empty($filterModel->order)) {
155
                    foreach ($filterModel->order as $fieldName => $value) {
0 ignored issues
show
Bug introduced by
The property order does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
156
                        // Get field by name and search by field column
157
                        $field = $this->module->getField($fieldName);
158
                        if (!is_null($field)) {
159
                            $query = $query->orderBy($field->column, $value);
160
                        }
161
                    }
162
                }
163
            }
164
        }
165
166
        return $query;
167
    }
168
}
169