Passed
Push — develop ( 6dcea6...5ae9a6 )
by Septianata
16:24
created

Attribute   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 53
ccs 13
cts 16
cp 0.8125
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoleAttribute() 0 5 1
A getIsActiveBadgeAttribute() 0 11 2
A setPasswordAttribute() 0 5 1
A getGravatarImageAttribute() 0 3 1
1
<?php
2
3
namespace App\Models\Concerns\User;
4
5
use Illuminate\Support\Facades\Hash;
6
7
/**
8
 * @property string|null $telegram_chat_id
9
 * @property string $username
10
 * @property string $fullname
11
 * @property string $email
12
 * @property string $phone_country
13
 * @property \Propaganistas\LaravelPhone\PhoneNumber $phone
14
 * @property \Illuminate\Support\Carbon $email_verified_at
15
 * @property string $password
16
 * @property string $remember_token
17
 * @property boolean $is_active
18
 * @property string $is_active_badge
19
 * @property-read string $role
20
 * @property string $gravatar_image
21
 *
22
 * @see \App\Models\User
23
 */
24
trait Attribute
25
{
26
    /**
27
     * Set "password" attribute value.
28
     *
29
     * @param  mixed  $value
30
     * @return void
31
     */
32 73
    public function setPasswordAttribute($value)
33
    {
34 73
        $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...
35
36 73
        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...
37
    }
38
39
    /**
40
     * Return "is_active_badge" attribute value.
41
     *
42
     * @return string
43
     */
44 1
    public function getIsActiveBadgeAttribute(): string
45
    {
46 1
        if ($this->is_active) {
47 1
            return sprintf(<<<'html'
48 1
            <div class="badge badge-success">%s</div>
49 1
            html, trans('Active'));
0 ignored issues
show
Bug introduced by
It seems like trans('Active') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
            html, /** @scrutinizer ignore-type */ trans('Active'));
Loading history...
50
        }
51
52
        return sprintf(<<<'html'
53
        <div class="badge badge-danger">%s</div>
54
        html, trans('Not Active'));
55
    }
56
57
    /**
58
     * Return "role" attribute value.
59
     *
60
     * @return string
61
     */
62 1
    public function getRoleAttribute(): string
63
    {
64 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

64
        $this->/** @scrutinizer ignore-call */ 
65
               load('roles:id,name');
Loading history...
65
66 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...
67
    }
68
69
    /**
70
     * Return "gravatar_image" attribute value.
71
     *
72
     * @return string
73
     */
74 19
    public function getGravatarImageAttribute(): string
75
    {
76 19
        return gravatar_image($this->email);
77
    }
78
}
79