Completed
Push — master ( f67f4f...77dcfd )
by Curtis
40s queued 12s
created

User::person()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use Exception;
6
use LaravelEnso\Comments\App\Traits\Comments;
7
use LaravelEnso\Discussions\App\Traits\Discussions;
8
use LaravelEnso\Discussions\App\Traits\Replies;
9
use Illuminate\Contracts\Translation\HasLocalePreference;
10
use Illuminate\Foundation\Auth\User as Authenticatable;
11
use Illuminate\Notifications\Notifiable;
12
use Illuminate\Support\Facades\App;
13
use Illuminate\Support\Facades\DB;
14
use LaravelEnso\ActionLogger\App\Traits\ActionLogs;
15
use App\Traits\enso\avatars\HasAvatar;
16
use LaravelEnso\Core\App\Enums\UserGroups;
17
use LaravelEnso\Core\App\Exceptions\UserConflict;
18
use LaravelEnso\Core\App\Services\DefaultPreferences;
19
use LaravelEnso\Core\App\Traits\HasPassword;
20
use App\Traits\enso\DynamicMethods\Relations;
21
use LaravelEnso\Files\App\Traits\Uploads;
22
use LaravelEnso\Helpers\App\Contracts\Activatable;
23
use LaravelEnso\Helpers\App\Traits\ActiveState;
24
use LaravelEnso\Helpers\App\Traits\AvoidsDeletionConflicts;
25
use LaravelEnso\Helpers\App\Traits\CascadesMorphMap;
26
use LaravelEnso\Impersonate\App\Traits\Impersonates;
27
use LaravelEnso\People\App\Traits\IsPerson;
28
use LaravelEnso\Rememberable\App\Traits\Rememberable;
29
use LaravelEnso\Roles\App\Enums\Roles;
30
use LaravelEnso\Tables\App\Traits\TableCache;
31
use LaravelEnso\Teams\App\Models\Team;
32
use LaravelEnso\Calendar\App\Models\Event;
33
use LaravelEnso\Files\App\Models\File;
34
use App\Person;
35
use App\Models\enso\Roles\Role;
36
use App\Models\enso\core\Preference;
37
use App\Models\enso\core\UserGroup;
38
39
class User extends Authenticatable implements Activatable, HasLocalePreference
40
{
41
    use Comments, Discussions, Replies, ActionLogs, ActiveState, AvoidsDeletionConflicts, CascadesMorphMap,
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $email which is not provided by App\Models\User.
Loading history...
Bug introduced by
The trait LaravelEnso\Helpers\App\Traits\ActiveState requires the property $is_active which is not provided by App\Models\User.
Loading history...
introduced by
The trait LaravelEnso\People\App\Traits\IsPerson requires some properties which are not provided by App\Models\User: $person, $email
Loading history...
Bug introduced by
The trait App\Traits\enso\avatars\HasAvatar requires the property $avatar which is not provided by App\Models\User.
Loading history...
introduced by
The trait LaravelEnso\Core\App\Traits\HasPassword requires some properties which are not provided by App\Models\User: $password, $password_updated_at
Loading history...
Bug introduced by
The trait LaravelEnso\Rememberable\App\Traits\Rememberable requires the property $cacheLifetime which is not provided by App\Models\User.
Loading history...
42
    HasAvatar, HasPassword, Impersonates, IsPerson, Notifiable,
43
    Relations, Rememberable, TableCache, Uploads;
44
45
    protected $hidden = ['password', 'remember_token', 'password_updated_at'];
46
47
    protected $fillable = ['person_id', 'group_id', 'role_id', 'email', 'is_active','email_verified_at','password'];
48
49
    protected $casts = [
50
        'is_active' => 'boolean', 'person_id' => 'int', 'group_id' => 'int', 'role_id' => 'int',
51
    ];
52
53
    protected $dates = ['password_updated_at'];
54
55
    public function person()
56
    {
57
        return $this->belongsTo(Person::class);
58
    }
59
60
    public function group()
61
    {
62
        return $this->belongsTo(UserGroup::class);
63
    }
64
65
    public function teams()
66
    {
67
        return $this->belongsToMany(Team::class);
68
    }
69
70
    public function role()
71
    {
72
        return $this->belongsTo(Role::class);
73
    }
74
75
    public function company()
76
    {
77
        return $this->person->company();
78
    }
79
80
    public function events()
81
    {
82
        return $this->belongsToMany(Event::class);
83
    }
84
85
    public function files()
86
    {
87
        return $this->hasMany(File::class, 'created_by');
88
    }
89
90
    public function logins()
91
    {
92
        return $this->hasMany(Login::class);
0 ignored issues
show
Bug introduced by
The type App\Models\Login 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...
93
    }
94
95
    public function preference()
96
    {
97
        return $this->hasOne(Preference::class);
98
    }
99
100
    public function isAdmin()
101
    {
102
        return $this->role_id === App::make(Roles::class)::Admin;
103
    }
104
105
    public function isSupervisor()
106
    {
107
        return $this->role_id === App::make(Roles::class)::Supervisor;
108
    }
109
110
    public function belongsToAdminGroup()
111
    {
112
        return $this->group_id === App::make(UserGroups::class)::Admin;
113
    }
114
115
    public function isPerson(Person $person)
116
    {
117
        return $this->person_id === $person->id;
118
    }
119
120
    public function resetPreferences()
121
    {
122
        $this->storePreferences($this->defaultPreferences());
123
    }
124
125
    public function preferences()
126
    {
127
        $preferences = $this->preference
128
            ? $this->preference->value
129
            : $this->defaultPreferences()->value;
130
131
        unset($this->preference);
132
133
        return $preferences;
134
    }
135
136
    public function preferredLocale()
137
    {
138
        return $this->lang();
139
    }
140
141
    public function lang()
142
    {
143
        return $this->preferences()->global->lang;
144
    }
145
146
    public function storeGlobalPreferences($global)
147
    {
148
        $preferences = $this->preferences();
149
        $preferences->global = $global;
150
151
        $this->storePreferences($preferences);
152
    }
153
154
    public function storeLocalPreferences($route, $value)
155
    {
156
        $preferences = $this->preferences();
157
        $preferences->local->$route = $value;
158
159
        $this->storePreferences($preferences);
160
    }
161
162
    public function erase(bool $person = false)
163
    {
164
        if ($person) {
165
            return DB::transaction(fn () => tap($this)->delete()->person->delete());
166
        }
167
168
        return $this->delete();
169
    }
170
171
    public function delete()
172
    {
173
        if ($this->logins()->exists()) {
174
            throw UserConflict::hasActivity();
175
        }
176
177
        try {
178
            return parent::delete();
179
        } catch (Exception $exception) {
180
            throw UserConflict::hasActivity();
181
        }
182
    }
183
184
    private function storePreferences($preferences)
185
    {
186
        $this->preference()->updateOrCreate(
187
            ['user_id' => $this->id],
188
            ['value' => $preferences]
189
        );
190
    }
191
192
    private function defaultPreferences()
193
    {
194
        return new Preference([
195
            'value' => DefaultPreferences::data(),
196
        ]);
197
    }
198
}
199