Passed
Push — develop ( 3e2f4c...41258c )
by Ngoding
05:09
created

Attribute::setPasswordAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Models\Concerns\User;
4
5
use Illuminate\Support\Facades\Hash;
6
use Illuminate\Support\HtmlString;
7
8
/**
9
 * @property string $username
10
 * @property string $name
11
 * @property string $email
12
 * @property \Illuminate\Support\Carbon $email_verified_at
13
 * @property string $password
14
 * @property \Illuminate\Support\HtmlString $bio
15
 * @property bool $is_subscribe_to_newsletter
16
 * @property-read string $remember_token
17
 * @property-read string $role
18
 * @property-read string $profile_image
19
 *
20
 * @see \App\Models\User
21
 */
22
trait Attribute
23
{
24
    /**
25
     * Set "password" attribute value.
26
     *
27
     * @param  mixed  $value
28
     * @return void
29
     */
30 18
    public function setPasswordAttribute($value)
31
    {
32 18
        $this->attributes['password'] = Hash::make($value);
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
34 18
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type App\Models\Concerns\User\Attribute which is incompatible with the documented return type void.
Loading history...
35
    }
36
37
    /**
38
     * Return "bio" attribute value.
39
     *
40
     * @param  string|null  $value
41
     * @return \Illuminate\Support\HtmlString
42
     */
43 1
    public function getBioAttribute(?string $value): HtmlString
44
    {
45 1
        return new HtmlString($value ?? '');
46
    }
47
48
    /**
49
     * Return "role" attribute value.
50
     *
51
     * @return string
52
     */
53 1
    public function getRoleAttribute(): string
54
    {
55 1
        $this->load('roles:id,name');
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $this->/** @scrutinizer ignore-call */ 
56
               load('roles:id,name');
Loading history...
56
57 1
        return $this->roles->first()->name;
0 ignored issues
show
Bug introduced by
The property roles does not exist on App\Models\Concerns\User\Attribute. Did you mean role?
Loading history...
58
    }
59
60
    /**
61
     * Return "profile_image" attribute value.
62
     *
63
     * @return string
64
     */
65 1
    public function getProfileImageAttribute(): string
66
    {
67 1
        return gravatar_image($this->email);
68
    }
69
}
70