Completed
Pull Request — master (#34)
by Fèvre
06:24 queued 03:17
created

UserPresenter::getAvatarPrimaryColorAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
namespace Xetaravel\Models\Presenters;
3
4
use Xetaravel\Utility\UserUtility;
5
6
trait UserPresenter
7
{
8
    /**
9
     * The default avatar used when there is no avatar for the user.
10
     *
11
     * @var string
12
     */
13
    protected $defaultAvatar = '/images/avatar.png';
14
15
    /**
16
     * The default primary color used when there is no primary color defined.
17
     *
18
     * @var string
19
     */
20
    protected $defaultAvatarPrimaryColor = '#B4AEA4';
21
22
    /**
23
     * Get the account first name.
24
     *
25
     * @return string
26
     */
27
    public function getFirstNameAttribute(): string
28
    {
29
        return $this->parse($this->account, 'first_name');
0 ignored issues
show
Bug introduced by
The property account does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
    }
31
32
    /**
33
     * Get the account last name.
34
     *
35
     * @return string
36
     */
37
    public function getLastNameAttribute(): string
38
    {
39
        return $this->parse($this->account, 'last_name');
40
    }
41
42
    /**
43
     * Get the account full name. Return the username if the user
44
     * has not set his first name and last name.
45
     *
46
     * @return string
47
     */
48
    public function getFullNameAttribute(): string
49
    {
50
        $fullName = $this->parse($this->account, 'first_name') . ' ' . $this->parse($this->account, 'last_name');
51
52
        if (empty(trim($fullName))) {
53
            return $this->username;
0 ignored issues
show
Bug introduced by
The property username does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
        }
55
56
        return $fullName;
57
    }
58
59
    /**
60
     * Get the account facebook.
61
     *
62
     * @return string
63
     */
64
    public function getFacebookAttribute(): string
65
    {
66
        return $this->parse($this->account, 'facebook');
67
    }
68
69
    /**
70
     * Get the account twitter.
71
     *
72
     * @return string
73
     */
74
    public function getTwitterAttribute(): string
75
    {
76
        return $this->parse($this->account, 'twitter');
77
    }
78
79
    /**
80
     * Get the account biography.
81
     *
82
     * @return string
83
     */
84
    public function getBiographyAttribute(): string
85
    {
86
        return $this->parse($this->account, 'biography');
87
    }
88
89
    /**
90
     * Get the account signature.
91
     *
92
     * @return string
93
     */
94
    public function getSignatureAttribute(): string
95
    {
96
        return $this->parse($this->account, 'signature');
97
    }
98
99
    /**
100
     * Get the small avatar.
101
     *
102
     * @return string
103
     */
104
    public function getAvatarSmallAttribute(): string
105
    {
106
        return $this->parseMedia('thumbnail.small');
107
    }
108
109
    /**
110
     * Get the medium avatar.
111
     *
112
     * @return string
113
     */
114
    public function getAvatarMediumAttribute(): string
115
    {
116
        return $this->parseMedia('thumbnail.medium');
117
    }
118
119
    /**
120
     * Get the big avatar.
121
     *
122
     * @return string
123
     */
124
    public function getAvatarBigAttribute(): string
125
    {
126
        return $this->parseMedia('thumbnail.big');
127
    }
128
129
    /**
130
     * Get the profile background.
131
     *
132
     * @return string
133
     */
134
    public function getProfileBackgroundAttribute(): string
135
    {
136
        return UserUtility::getProfileBackground();
137
    }
138
139
    /**
140
     * Get the profile url.
141
     *
142
     * @return string
143
     */
144
    public function getProfileUrlAttribute(): string
145
    {
146
        if (!isset($this->slug)) {
147
            return '';
148
        }
149
150
        return route('users.user.show', ['slug' => $this->slug]);
0 ignored issues
show
Bug introduced by
The property slug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
151
    }
152
153
    /**
154
     * Get the primary color detected in the avatar.
155
     *
156
     * @return string
157
     */
158
    public function getAvatarPrimaryColorAttribute(): string
159
    {
160
        if (isset($this->getMedia('avatar')[0]) && $this->getMedia('avatar')[0]->hasCustomProperty('primaryColor')) {
0 ignored issues
show
Bug introduced by
It seems like getMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
161
            return $this->getMedia('avatar')[0]->getCustomProperty('primaryColor');
0 ignored issues
show
Bug introduced by
It seems like getMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
162
        }
163
164
        return $this->defaultAvatarPrimaryColor;
165
    }
166
167
    /**
168
     * Parse a mdedia and return it if isset or return the default avatar.
169
     *
170
     * @param string $type The type of the media to get.
171
     *
172
     * @return string
173
     */
174
    protected function parseMedia(string $type): string
175
    {
176
        if (isset($this->getMedia('avatar')[0])) {
0 ignored issues
show
Bug introduced by
It seems like getMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
177
            return $this->getMedia('avatar')[0]->getUrl($type);
0 ignored issues
show
Bug introduced by
It seems like getMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
178
        }
179
180
        return $this->defaultAvatar;
181
    }
182
183
    /**
184
     * Parse an attribute and return its value or empty if null.
185
     *
186
     * @param Object|null $relation The relation or the user object.
187
     *       Can be `$this` or `$this->account` for exemple
188
     * @param string|null $attribute The attribute to parse.
189
     *
190
     * @return string
191
     */
192
    protected function parse($relation, $attribute): string
193
    {
194
        if ($relation === null || $relation->{$attribute} === null) {
195
            return '';
196
        }
197
198
        return $relation->{$attribute};
199
    }
200
}
201