Completed
Push — 2.0 ( 60f4f1...be6014 )
by Kirill
04:10
created

User::setPasswordAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\Models;
12
13
use App\Models\User\GravatarSupport;
14
use Illuminate\Notifications\Notifiable;
15
use Illuminate\Database\Eloquent\Relations\MorphMany;
16
use Service\ImageUploader\Resolvers\GravatarSupports;
17
use Illuminate\Foundation\Auth\User as Authenticatable;
18
19
/**
20
 * Class User.
21
 */
22
class User extends Authenticatable implements GravatarSupports
23
{
24
    use Notifiable;
25
    use GravatarSupport;
26
27
    public const DEFAULT_AVATAR_PATH = '/static/avatars/';
28
    public const DEFAULT_AVATAR_NAME = 'default/1.png';
29
30
    /**
31
     * @var string|null
32
     */
33
    public $token;
34
35
    /**
36
     * @var array
37
     */
38
    protected $fillable = [
39
        'name',
40
        'email',
41
        'password',
42
        'avatar',
43
    ];
44
45
    /**
46
     * @var array
47
     */
48
    protected $hidden = [
49
        'password',
50
        'remember_token',
51
    ];
52
53
    /**
54
     * @param  string $password
55
     * @return User
56
     */
57
    public function setPasswordAttribute(string $password): User
58
    {
59
        $this->attributes['password'] = bcrypt($password);
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param  string|null $avatar
66
     * @return string
67
     */
68
    public function getAvatarAttribute(string $avatar = null): string
69
    {
70
        if ($avatar === null) {
71
            $avatar = self::DEFAULT_AVATAR_NAME;
72
        }
73
74
        return self::DEFAULT_AVATAR_PATH . $avatar;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function hasAvatar(): bool
81
    {
82
        return (bool) ($this->original['avatar'] ?? false);
83
    }
84
85
    /**
86
     * @return MorphMany
87
     */
88
    public function articles(): MorphMany
89
    {
90
        return $this->morphMany(Article::class, 'user');
91
    }
92
}
93