Test Failed
Push — feature-laravel-5.4 ( bce2b2...556e60 )
by Kirill
03:14
created

User::guest()   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
     * @return User
50
     */
51
    public static function guest(): User
52
    {
53
        return new User(['id' => 0, 'name' => 'Guest']);
54
    }
55
56
    /**
57
     * @param  string $password
58
     * @return User
59
     */
60
    public function setPasswordAttribute(string $password): User
61
    {
62
        $this->attributes['password'] = bcrypt($password);
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param  string|null $avatar
69
     * @return string
70
     */
71
    public function getAvatarAttribute(string $avatar = null): string
72
    {
73
        if ($avatar === null) {
74
            $avatar = self::DEFAULT_AVATAR_NAME;
75
        }
76
77
        return self::DEFAULT_AVATAR_PATH . $avatar;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function hasAvatar(): bool
84
    {
85
        return (bool) ($this->original['avatar'] ?? false);
86
    }
87
88
    /**
89
     * @return MorphMany
90
     */
91
    public function articles(): MorphMany
92
    {
93
        return $this->morphMany(Article::class, 'user');
94
    }
95
}
96