Test Failed
Push — feature-laravel-5.4 ( 475d96...446986 )
by Kirill
07:30
created

User::articles()   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 Illuminate\Database\Eloquent\Relations\MorphToMany;
14
use Illuminate\Notifications\Notifiable;
15
use Illuminate\Foundation\Auth\User as Authenticatable;
16
17
/**
18
 * Class User.
19
 */
20
class User extends Authenticatable
21
{
22
    use Notifiable;
23
24
    public const DEFAULT_AVATAR_PATH = '/static/avatars/';
25
    public const DEFAULT_AVATAR_NAME = 'default/1.png';
26
27
    /**
28
     * @var array
29
     */
30
    protected $fillable = [
31
        'name', 'email', 'password', 'avatar',
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    protected $hidden = [
38
        'password', 'remember_token',
39
    ];
40
41
    /**
42
     * @param  string $password
43
     * @return User
44
     */
45
    public function setPasswordAttribute(string $password): User
46
    {
47
        $this->attributes['password'] = bcrypt($password);
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param  string|null $avatar
54
     * @return string
55
     */
56
    public function getAvatarAttribute(string $avatar = null): string
57
    {
58
        if ($avatar === null) {
59
            $avatar = self::DEFAULT_AVATAR_NAME;
60
        }
61
62
        return self::DEFAULT_AVATAR_PATH . $avatar;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function hasAvatar(): bool
69
    {
70
        return (bool) ($this->original['avatar'] ?? false);
71
    }
72
73
    /**
74
     * @return MorphToMany
75
     */
76
    public function articles(): MorphToMany
77
    {
78
        return $this->morphToMany(Article::class, 'user');
79
    }
80
}
81