Passed
Push — master ( 5d0db5...ea9eab )
by Jonathan
11:13
created

UccelloModule::getAssignedUserAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\AssignedUser;
9
use Illuminate\Support\Facades\Auth;
10
11
trait UccelloModule
12
{
13
    /**
14
     * The "booting" method of the model.
15
     *
16
     * @return void
17
     */
18
    protected static function boot()
19
    {
20
        parent::boot();
21
22
        if(static::isFilteredByUser())
23
        {
24
            static::addGlobalScope(new AssignedUser);
25
        }
26
    }
27
28
    protected static function isFilteredByUser()
29
    {
30
        $user = Auth::user();
31
32
        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...
33
        {
34
            $module = static::getModuleFromClass(static::class);
35
36
            if($module && $module->data && property_exists($module->data, 'private') &&  $module->data->private)
37
            {
38
                return true;
39
            }
40
        }
41
42
        return false;
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();
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
        {
78
            $uuid = $entity->getKey();
79
        }
80
81
        return $uuid;
82
    }
83
84
    /**
85
     * Returns Assigned User
86
     *
87
     * @return string|null
88
     */
89
    public function getAssignedUserAttribute(): ?string
90
    {
91
        return $this->assigned_user_id;
92
    }
93
}