Completed
Push — 2.0 ( afe19a...325c55 )
by Kirill
03:06
created

User::isGuest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 array
32
     */
33
    protected $fillable = [
34
        'name',
35
        'email',
36
        'password',
37
        'avatar',
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected $hidden = [
44
        'password',
45
        'remember_token',
46
    ];
47
48
    /**
49
     * @param  string $password
50
     * @return User
51
     */
52
    public function setPasswordAttribute(string $password): User
53
    {
54
        $this->attributes['password'] = bcrypt($password);
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param  string|null $avatar
61
     * @return string
62
     */
63
    public function getAvatarAttribute(string $avatar = null): string
64
    {
65
        if ($avatar === null) {
66
            $avatar = self::DEFAULT_AVATAR_NAME;
67
        }
68
69
        return self::DEFAULT_AVATAR_PATH . $avatar;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function hasAvatar(): bool
76
    {
77
        return (bool) ($this->original['avatar'] ?? false);
78
    }
79
80
    /**
81
     * @return MorphMany
82
     */
83
    public function articles(): MorphMany
84
    {
85
        return $this->morphMany(Article::class, 'user');
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function isGuest(): bool
92
    {
93
        return 0 === (int)$this->getAuthIdentifier();
94
    }
95
}
96