Passed
Push — master ( dd30a0...b64fc0 )
by Jonathan
17:19
created

UccelloModule::scopeInDomain()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 13
rs 9.6111
cc 5
nc 3
nop 3
1
<?php
2
3
namespace Uccello\Core\Support\Traits;
4
5
use Uccello\Core\Models\Entity;
6
use Uccello\Core\Models\Module;
7
use Illuminate\Support\Facades\Cache;
8
use Uccello\Core\Support\Scopes;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Schema;
11
use Uccello\Core\Models\Domain;
12
13
trait UccelloModule
14
{
15
    /**
16
     * The "booting" method of the model.
17
     *
18
     * @return void
19
     */
20
    protected static function boot()
21
    {
22
        parent::boot();
23
24
        if (static::isFilteredByUser()) {
25
            static::addGlobalScope(new Scopes\AssignedUser);
26
        }
27
    }
28
29
    protected static function isFilteredByUser()
30
    {
31
        $isFilteredByUser = false;
32
33
        $user = Auth::user();
34
35
        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...
36
            $module = static::getModuleFromClass(static::class);
37
38
            if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) {
39
                $isFilteredByUser = true;
40
            }
41
        }
42
43
        return $isFilteredByUser;
44
    }
45
46
    public function scopeInDomain($query, ?Domain $domain, $withDescendants=false)
47
    {
48
        if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) {
49
            // Activate descendant view if the user is allowed
50
            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

50
            if (Auth::user()->/** @scrutinizer ignore-call */ canSeeDescendantsRecords($domain) && $withDescendants) {
Loading history...
51
                $domainsIds = $domain->findDescendants()->pluck('id');
52
                $query = $query::whereIn('domain_id', $domainsIds);
53
            } else {
54
                $query = $query::where('domain_id', $domain->id);
55
            }
56
        }
57
58
        return $query;
59
    }
60
61
    public function getTableAttribute()
62
    {
63
        return $this->table;
64
    }
65
66
    public function getModuleAttribute()
67
    {
68
        return static::getModuleFromClass(get_class($this));
69
    }
70
71
    protected static function getModuleFromClass($className)
72
    {
73
        $modules = Cache::rememberForever('modules_by_model_class', function () {
74
            $modulesGroupedByModelClass = collect();
75
            Module::all()->map(function ($item) use ($modulesGroupedByModelClass) {
76
                $modulesGroupedByModelClass[$item->model_class] = $item;
77
                return $modulesGroupedByModelClass;
78
            });
79
            return $modulesGroupedByModelClass;
80
        });
81
        return $modules[(string) $className] ?? null;
82
    }
83
84
    public function getUuidAttribute()
85
    {
86
        $uuid = null;
87
88
        $entity = Entity::where('module_id', $this->module->getKey())
89
                        ->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

89
                        ->where('record_id', $this->/** @scrutinizer ignore-call */ getKey())
Loading history...
90
                        ->first();
91
92
        if ($entity) {
93
            $uuid = $entity->getKey();
94
        }
95
96
        return $uuid;
97
    }
98
}