Passed
Push — activity-logs ( 8cf388 )
by Fèvre
06:45
created

UserPresenter::getLastNameAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Xetaravel\Models\Presenters;
3
4
use Xetaravel\Models\ActivityLog;
5
use Xetaravel\Utility\UserUtility;
6
7
trait UserPresenter
8
{
9
    /**
10
     * The default avatar used when there is no avatar for the user.
11
     *
12
     * @var string
13
     */
14
    protected $defaultAvatar = '/images/avatar.png';
15
16
    /**
17
     * The default primary color used when there is no primary color defined.
18
     *
19
     * @var string
20
     */
21
    protected $defaultAvatarPrimaryColor = '#B4AEA4';
22
23
    /**
24
     * Get the account first name.
25
     *
26
     * @return string
27
     */
28
    public function getFirstNameAttribute(): string
29
    {
30
        return $this->parse($this->account, 'first_name');
31
    }
32
33
    /**
34
     * Get the account last name.
35
     *
36
     * @return string
37
     */
38
    public function getLastNameAttribute(): string
39
    {
40
        return $this->parse($this->account, 'last_name');
41
    }
42
43
    /**
44
     * Get whatever the user has rubies or not.
45
     *
46
     * @return boolean
47
     */
48
    public function getHasRubiesAttribute(): bool
49
    {
50
        return $this->rubies_total > 0 ? true : false;
51
    }
52
53
    /**
54
     * Get the account full name. Return the username if the user
55
     * has not set his first name and last name.
56
     *
57
     * @return string
58
     */
59
    public function getFullNameAttribute(): string
60
    {
61
        $fullName = $this->parse($this->account, 'first_name') . ' ' . $this->parse($this->account, 'last_name');
62
63
        if (empty(trim($fullName))) {
64
            return $this->username;
65
        }
66
67
        return $fullName;
68
    }
69
70
    /**
71
     * Get the experiences total formated.
72
     *
73
     * @return integer
74
     */
75
    public function getExperiencesTotalAttribute($experiencesTotal): int
76
    {
77
        return number_format($experiencesTotal, 0, ".", " ");
78
    }
79
80
    /**
81
     * Get the account facebook.
82
     *
83
     * @return string
84
     */
85
    public function getFacebookAttribute(): string
86
    {
87
        return $this->parse($this->account, 'facebook');
88
    }
89
90
    /**
91
     * Get the account twitter.
92
     *
93
     * @return string
94
     */
95
    public function getTwitterAttribute(): string
96
    {
97
        return $this->parse($this->account, 'twitter');
98
    }
99
100
    /**
101
     * Get the account biography.
102
     *
103
     * @return string
104
     */
105
    public function getBiographyAttribute(): string
106
    {
107
        return $this->parse($this->account, 'biography');
108
    }
109
110
    /**
111
     * Get the account signature.
112
     *
113
     * @return string
114
     */
115
    public function getSignatureAttribute(): string
116
    {
117
        return $this->parse($this->account, 'signature');
118
    }
119
120
    /**
121
     * Get the small avatar.
122
     *
123
     * @return string
124
     */
125
    public function getAvatarSmallAttribute(): string
126
    {
127
        return $this->parseMedia('thumbnail.small');
128
    }
129
130
    /**
131
     * Get the medium avatar.
132
     *
133
     * @return string
134
     */
135
    public function getAvatarMediumAttribute(): string
136
    {
137
        return $this->parseMedia('thumbnail.medium');
138
    }
139
140
    /**
141
     * Get the big avatar.
142
     *
143
     * @return string
144
     */
145
    public function getAvatarBigAttribute(): string
146
    {
147
        return $this->parseMedia('thumbnail.big');
148
    }
149
150
    /**
151
     * Get the profile background.
152
     *
153
     * @return string
154
     */
155
    public function getProfileBackgroundAttribute(): string
156
    {
157
        return UserUtility::getProfileBackground();
158
    }
159
160
    /**
161
     * Get the profile url.
162
     *
163
     * @return string
164
     */
165
    public function getProfileUrlAttribute(): string
166
    {
167
        if (!isset($this->slug)) {
168
            return '';
169
        }
170
171
        return route('users.user.show', ['slug' => $this->slug]);
172
    }
173
174
    /**
175
     * Get the primary color detected in the avatar.
176
     *
177
     * @return string
178
     */
179
    public function getAvatarPrimaryColorAttribute(): string
180
    {
181
        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? ( Ignorable by Annotation )

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

181
        if (isset($this->/** @scrutinizer ignore-call */ getMedia('avatar')[0]) && $this->getMedia('avatar')[0]->hasCustomProperty('primaryColor')) {
Loading history...
182
            return $this->getMedia('avatar')[0]->getCustomProperty('primaryColor');
183
        }
184
185
        return $this->defaultAvatarPrimaryColor;
186
    }
187
188
    /**
189
     * We must decrement the post count due to the first post being counted.
190
     *
191
     * @param int $count The actual post count cache.
192
     *
193
     * @return int
194
     */
195
    public function getDiscussPostCountAttribute($count): int
196
    {
197
        return $count - $this->discuss_conversation_count;
198
    }
199
200
    /**
201
     * Get the status of the user : online or offline
202
     *
203
     * @return string
204
     */
205
    public function getOnlineAttribute(): string
206
    {
207
        $online = ActivityLog::expires()->where('user_id', $this->id)->first();
208
209
        return is_null($online) ? false : true;
210
    }
211
212
    /**
213
     * Parse a mdedia and return it if isset or return the default avatar.
214
     *
215
     * @param string $type The type of the media to get.
216
     *
217
     * @return string
218
     */
219
    protected function parseMedia(string $type): string
220
    {
221
        if (isset($this->getMedia('avatar')[0])) {
222
            return $this->getMedia('avatar')[0]->getUrl($type);
223
        }
224
225
        return $this->defaultAvatar;
226
    }
227
228
    /**
229
     * Parse an attribute and return its value or empty if null.
230
     *
231
     * @param Object|null $relation The relation or the user object.
232
     *       Can be `$this` or `$this->account` for exemple
233
     * @param string|null $attribute The attribute to parse.
234
     *
235
     * @return string
236
     */
237
    protected function parse($relation, $attribute): string
238
    {
239
        if ($relation === null || $relation->{$attribute} === null) {
240
            return '';
241
        }
242
243
        return $relation->{$attribute};
244
    }
245
}
246