AdminUser   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 158
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 7
1
<?php
2
3
namespace App\Models;
4
5
use App\Notifications\ResetPassword;
6
use App\Support\Helper;
7
use App\Support\Image\Filters\Fit;
8
use App\Support\Traits\ImageStorage;
9
use Iatstuti\Database\Support\NullableFields;
10
use Illuminate\Foundation\Auth\User as Authenticatable;
11
use Illuminate\Notifications\Notifiable;
12
13
class AdminUser extends Authenticatable
14
{
15
    use NullableFields, ImageStorage, Notifiable;
16
17
    /**
18
     * The avatar size.
19
     */
20
    const AVATAR_SIZE = 120;
21
22
    /**
23
     * The directory stores all admin users' avatars.
24
     */
25
    const AVATAR_DIRECTORY = 'admin-avatar';
26
27
    /**
28
     * The accessors to append to the model's array form.
29
     *
30
     * @var array
31
     */
32
    protected $appends = ['super_admin'];
33
34
    /**
35
     * The attributes that should be hidden for arrays.
36
     *
37
     * @var array
38
     */
39
    protected $hidden = ['password', 'remember_token'];
40
41
    /**
42
     * The attributes that should be saved as null when empty.
43
     *
44
     * @var array
45
     */
46
    protected $nullable = ['avatar'];
47
48
    /**
49
     * Create an admin user.
50
     *
51
     * @param  array  $data
52
     * @return static
53
     */
54
    public static function createUser($data)
55
    {
56
        return static::forceCreate([
57
            'email' => mb_strtolower(trim($data['email'])),
58
            'password' => bcrypt($data['password']),
59
            'username' => trim($data['username']),
60
        ]);
61
    }
62
63
    /**
64
     * Get the `super_admin` attribute.
65
     *
66
     * @return bool
67
     */
68
    public function getSuperAdminAttribute()
69
    {
70
        return $this->id === 1;
71
    }
72
73
    /**
74
     * Determines whether the user is a super admin.
75
     *
76
     * @return bool
77
     */
78
    public function isSuperAdmin()
79
    {
80
        return $this->super_admin;
81
    }
82
83
    /**
84
     * Get the `avatar` attribute.
85
     *
86
     * @param  string|null  $value
87
     * @return string
88
     */
89
    public function getAvatarAttribute($value)
90
    {
91
        if (! is_null($value)) {
92
            return $this->getAssetUrl($value, 'avatar');
93
        }
94
95
        return Helper::gravatar($this->email, static::AVATAR_SIZE);
96
    }
97
98
    /**
99
     * Set the `avatar` attribute.
100
     *
101
     * @param  string|null  $value
102
     */
103
    public function setAvatarAttribute($value)
104
    {
105
        $this->attributes['avatar'] = $value;
106
    }
107
108
    /**
109
     * Use the default avatar.
110
     */
111
    public function useDefaultAvatar()
112
    {
113
        $this->avatar = null;
114
    }
115
116
    /**
117
     * Store the given file as user's avatar.
118
     *
119
     * @param  mixed  $file
120
     * @return bool
121
     */
122
    public function storeAvatarFile($file)
123
    {
124
        if ($path = $this->storeImageFile($file, 'avatar')) {
125
            $this->avatar = $path;
126
127
            return true;
128
        }
129
130
        return false;
131
    }
132
133
    /**
134
     * Get image filter.
135
     *
136
     * @see http://image.intervention.io/api/filter
137
     *
138
     * @param  string|null  $identifier
139
     */
140
    protected function getImageFilter($identifier = null)
141
    {
142
        if ($identifier == 'avatar') {
143
            return (new Fit)->width(static::AVATAR_SIZE);
144
        }
145
    }
146
147
    /**
148
     * Get image output directory.
149
     *
150
     * @param  string|null  $identifier
151
     * @return string
152
     */
153
    protected function getImageDirectory($identifier = null)
154
    {
155
        if ($identifier == 'avatar') {
156
            return static::AVATAR_DIRECTORY;
157
        }
158
    }
159
160
    /**
161
     * Send the password reset notification.
162
     *
163
     * @param  string  $token
164
     * @return void
165
     */
166
    public function sendPasswordResetNotification($token)
167
    {
168
        $this->notify(new ResetPassword($token));
169
    }
170
}
171