Completed
Push — issue-37 ( 681eed )
by Fèvre
03:10
created

UserPresenter::getHasRubiesAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
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 whatever the user has rubies or not.
44
     *
45
     * @return boolean
46
     */
47
    public function getHasRubiesAttribute(): bool
48
    {
49
        return $this->rubies_total > 0 ? true : false;
0 ignored issues
show
Bug introduced by
The property rubies_total 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...
50
    }
51
52
    /**
53
     * Get the account full name. Return the username if the user
54
     * has not set his first name and last name.
55
     *
56
     * @return string
57
     */
58
    public function getFullNameAttribute(): string
59
    {
60
        $fullName = $this->parse($this->account, 'first_name') . ' ' . $this->parse($this->account, 'last_name');
61
62
        if (empty(trim($fullName))) {
63
            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...
64
        }
65
66
        return $fullName;
67
    }
68
69
    /**
70
     * Get the experiences total formated.
71
     *
72
     * @return integer
73
     */
74
    public function getExperiencesTotalAttribute($experiencesTotal): int
75
    {
76
        return number_format($experiencesTotal, 0, ".", " ");
77
    }
78
79
    /**
80
     * Get the account facebook.
81
     *
82
     * @return string
83
     */
84
    public function getFacebookAttribute(): string
85
    {
86
        return $this->parse($this->account, 'facebook');
87
    }
88
89
    /**
90
     * Get the account twitter.
91
     *
92
     * @return string
93
     */
94
    public function getTwitterAttribute(): string
95
    {
96
        return $this->parse($this->account, 'twitter');
97
    }
98
99
    /**
100
     * Get the account biography.
101
     *
102
     * @return string
103
     */
104
    public function getBiographyAttribute(): string
105
    {
106
        return $this->parse($this->account, 'biography');
107
    }
108
109
    /**
110
     * Get the account signature.
111
     *
112
     * @return string
113
     */
114
    public function getSignatureAttribute(): string
115
    {
116
        return $this->parse($this->account, 'signature');
117
    }
118
119
    /**
120
     * Get the small avatar.
121
     *
122
     * @return string
123
     */
124
    public function getAvatarSmallAttribute(): string
125
    {
126
        return $this->parseMedia('thumbnail.small');
127
    }
128
129
    /**
130
     * Get the medium avatar.
131
     *
132
     * @return string
133
     */
134
    public function getAvatarMediumAttribute(): string
135
    {
136
        return $this->parseMedia('thumbnail.medium');
137
    }
138
139
    /**
140
     * Get the big avatar.
141
     *
142
     * @return string
143
     */
144
    public function getAvatarBigAttribute(): string
145
    {
146
        return $this->parseMedia('thumbnail.big');
147
    }
148
149
    /**
150
     * Get the profile background.
151
     *
152
     * @return string
153
     */
154
    public function getProfileBackgroundAttribute(): string
155
    {
156
        return UserUtility::getProfileBackground();
157
    }
158
159
    /**
160
     * Get the profile url.
161
     *
162
     * @return string
163
     */
164
    public function getProfileUrlAttribute(): string
165
    {
166
        if (!isset($this->slug)) {
167
            return '';
168
        }
169
170
        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...
171
    }
172
173
    /**
174
     * Get the primary color detected in the avatar.
175
     *
176
     * @return string
177
     */
178
    public function getAvatarPrimaryColorAttribute(): string
179
    {
180
        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...
181
            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...
182
        }
183
184
        return $this->defaultAvatarPrimaryColor;
185
    }
186
187
    /**
188
     * We must decrement the post count due to the first post being counted.
189
     *
190
     * @param int $count The actual post count cache.
191
     *
192
     * @return int
193
     */
194
    public function getDiscussPostCountAttribute($count): int
195
    {
196
        return $count - $this->discuss_conversation_count;
0 ignored issues
show
Bug introduced by
The property discuss_conversation_count 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...
197
    }
198
199
    /**
200
     * Parse a mdedia and return it if isset or return the default avatar.
201
     *
202
     * @param string $type The type of the media to get.
203
     *
204
     * @return string
205
     */
206
    protected function parseMedia(string $type): string
207
    {
208
        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...
209
            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...
210
        }
211
212
        return $this->defaultAvatar;
213
    }
214
215
    /**
216
     * Parse an attribute and return its value or empty if null.
217
     *
218
     * @param Object|null $relation The relation or the user object.
219
     *       Can be `$this` or `$this->account` for exemple
220
     * @param string|null $attribute The attribute to parse.
221
     *
222
     * @return string
223
     */
224
    protected function parse($relation, $attribute): string
225
    {
226
        if ($relation === null || $relation->{$attribute} === null) {
227
            return '';
228
        }
229
230
        return $relation->{$attribute};
231
    }
232
}
233