Completed
Push — master ( d42b43...5ac77a )
by ARCANEDEV
8s
created

User::slugify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\User as Authenticatable;
4
use Arcanedev\LaravelAuth\Exceptions\UserConfirmationException;
5
use Arcanedev\LaravelAuth\Services\UserConfirmator;
6
use Arcanedev\LaravelAuth\Traits\Activatable;
7
use Arcanedev\LaravelAuth\Traits\AuthUserTrait;
8
use Arcanesoft\Contracts\Auth\Models\User as UserContract;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
use Illuminate\Support\Str;
11
12
/**
13
 * Class     User
14
 *
15
 * @package  Arcanedev\LaravelAuth\Models
16
 * @author   ARCANEDEV <[email protected]>
17
 *
18
 * @property  int                                       id
19
 * @property  string                                    username
20
 * @property  string                                    first_name
21
 * @property  string                                    last_name
22
 * @property  string                                    full_name
23
 * @property  string                                    email
24
 * @property  string                                    password
25
 * @property  string                                    remember_token
26
 * @property  bool                                      is_admin
27
 * @property  bool                                      is_active
28
 * @property  bool                                      is_confirmed       (Optional)
29
 * @property  string                                    confirmation_code  (Optional)
30
 * @property  \Carbon\Carbon                            confirmed_at       (Optional)
31
 * @property  \Carbon\Carbon                            created_at
32
 * @property  \Carbon\Carbon                            updated_at
33
 * @property  \Carbon\Carbon                            deleted_at
34
 * @property  \Illuminate\Database\Eloquent\Collection  roles
35
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
36
 *
37
 * @method  static  bool                                   insert(array $values)
38
 * @method          \Illuminate\Database\Eloquent\Builder  unconfirmed(string $code)
39
 */
40
class User extends Authenticatable implements UserContract
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: can, getAuthIdentifier, getAuthIdentifierName, getAuthPassword, getEmailForPasswordReset, getRememberToken, getRememberTokenName, sendPasswordResetNotification, setAttribute, setRememberToken
Loading history...
41
{
42
    /* ------------------------------------------------------------------------------------------------
43
     |  Traits
44
     | ------------------------------------------------------------------------------------------------
45
     */
46
    use AuthUserTrait, Activatable, SoftDeletes;
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Properties
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * The attributes that are mass assignable.
54
     *
55
     * @var array
56
     */
57
    protected $fillable = [
58
        'username', 'first_name', 'last_name', 'email', 'password',
59
    ];
60
61
    /**
62
     * The attributes excluded from the model's JSON form.
63
     *
64
     * @var array
65
     */
66
    protected $hidden   = [
67
        'password', 'remember_token', 'confirmation_code',
68
    ];
69
70
    /**
71
     * The attributes that should be casted to native types.
72
     *
73
     * @var array
74
     */
75
    protected $casts = [
76
        'is_admin'     => 'boolean',
77
        'is_active'    => 'boolean',
78
        'is_confirmed' => 'boolean',
79
    ];
80
81
    /**
82
     * The attributes that should be mutated to dates.
83
     *
84
     * @var array
85
     */
86
    protected $dates = [
87
        'confirmed_at', 'deleted_at'
88
    ];
89
90
    /* ------------------------------------------------------------------------------------------------
91
     |  Constructor
92
     | ------------------------------------------------------------------------------------------------
93
     */
94
    /**
95
     * Create a new Eloquent model instance.
96
     *
97
     * @param  array  $attributes
98
     */
99
    public function __construct(array $attributes = [])
100
    {
101
        $this->setTable(config('laravel-auth.users.table', 'users'));
102
103
        parent::__construct($attributes);
104
    }
105
106 528
    /* ------------------------------------------------------------------------------------------------
107
     |  Scopes
108 528
     | ------------------------------------------------------------------------------------------------
109
     */
110 528
    /**
111 528
     * Scope unconfirmed users by code.
112
     *
113
     * @param  \Illuminate\Database\Eloquent\Builder  $query
114
     * @param  string                                 $code
115
     *
116
     * @return \Illuminate\Database\Eloquent\Builder
117
     */
118
    public function scopeUnconfirmed($query, $code)
119
    {
120
        return $query->where('is_confirmed', false)
121
            ->where('confirmation_code', $code)
122
            ->whereNull('confirmed_at');
123
    }
124
125 32
    /* ------------------------------------------------------------------------------------------------
126
     |  Getters & Setters
127 32
     | ------------------------------------------------------------------------------------------------
128 32
     */
129 32
    /**
130
     * Set the `username` attribute.
131
     *
132
     * @param  string  $username
133
     */
134
    public function setUsernameAttribute($username)
135
    {
136
        $this->attributes['username'] = $this->slugify($username);
137
    }
138
139
    /**
140
     * Get the `full_name` attribute.
141 8
     *
142
     * @return string
143 8
     */
144
    public function getFullNameAttribute()
145
    {
146
        return $this->first_name . ' ' . $this->last_name;
147
    }
148
149
    /**
150
     * Set the `password` attribute.
151 160
     *
152
     * @param  string  $password
153 160
     */
154 160
    public function setPasswordAttribute($password)
155
    {
156
        $this->attributes['password'] = bcrypt($password);
157
    }
158
159
    /* ------------------------------------------------------------------------------------------------
160
     |  CRUD Functions
161
     | ------------------------------------------------------------------------------------------------
162
     */
163
    /**
164
     * Confirm the unconfirmed user account by confirmation code.
165
     *
166
     * @param  string  $code
167
     *
168
     * @return \Arcanesoft\Contracts\Auth\Models\User
169 32
     *
170
     * @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException
171 32
     */
172
    public function findUnconfirmed($code)
173 32
    {
174 8
        $unconfirmedUser = self::unconfirmed($code)->first();
175
176
        if ( ! $unconfirmedUser instanceof self)
177 24
            throw (new UserConfirmationException)->setModel(self::class);
178
179
        return $unconfirmedUser;
180
    }
181
182
    /**
183
     * Confirm the new user account.
184
     *
185
     * @param  \Arcanesoft\Contracts\Auth\Models\User|string  $code
186
     *
187 16
     * @return \Arcanesoft\Contracts\Auth\Models\User
188
     */
189 16
    public function confirm($code)
190 8
    {
191 6
        if ($code instanceof self)
192
            $code = $code->confirmation_code;
193 16
194
        $user = $this->findUnconfirmed($code);
195 16
196
        return (new UserConfirmator)->confirm($user);
197
    }
198
199
    /* ------------------------------------------------------------------------------------------------
200
     |  Check Functions
201
     | ------------------------------------------------------------------------------------------------
202
     */
203
    /**
204
     * Check if user is an administrator.
205
     *
206
     * @return bool
207 40
     */
208
    public function isAdmin()
209 40
    {
210
        return $this->is_admin;
211
    }
212
213
    /**
214
     * Check if user is a moderator.
215
     *
216
     * @return bool
217 24
     */
218
    public function isModerator()
219 24
    {
220
        // Override this method to give more privileges than members.
221
        return false;
222
    }
223
224
    /**
225
     * Check if user is a member.
226
     *
227 16
     * @return bool
228
     */
229 16
    public function isMember()
230
    {
231
        return ! $this->isAdmin();
232
    }
233
234
    /**
235
     * Check if user has a confirmed account.
236
     *
237
     * @return bool
238
     */
239
    public function isConfirmed()
240
    {
241
        return $this->is_confirmed;
242
    }
243
244
    /**
245
     * Check if user is on force deleting.
246
     *
247
     * @return bool
248
     */
249
    public function isForceDeleting()
250
    {
251
        return $this->forceDeleting;
252
    }
253
254
    /* ------------------------------------------------------------------------------------------------
255
     |  Other Functions
256
     | ------------------------------------------------------------------------------------------------
257
     */
258
    /**
259
     * Slugify the value.
260
     *
261
     * @param  string  $value
262
     *
263
     * @return string
264
     */
265
    protected function slugify($value)
266
    {
267
        return Str::slug($value, config('laravel-auth.users.slug-separator', '.'));
268
    }
269
}
270