Passed
Push — 5.0.0 ( 2a052f...bac408 )
by Fèvre
05:55
created

UserPresenter::getOnlineAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

187
        if (isset($this->/** @scrutinizer ignore-call */ getMedia('avatar')[0]) && $this->getMedia('avatar')[0]->hasCustomProperty('primaryColor')) {
Loading history...
188
            return $this->getMedia('avatar')[0]->getCustomProperty('primaryColor');
189
        }
190
191
        return $this->defaultAvatarPrimaryColor;
192
    }
193
194
    /**
195
     * We must decrement the post count due to the first post being counted.
196
     *
197
     * @param int $count The actual post count cache.
198
     *
199
     * @return int
200
     */
201
    public function getDiscussPostCountAttribute($count): int
202
    {
203
        return $count - $this->discuss_conversation_count;
204
    }
205
206
    /**
207
     * Get the status of the user : online or offline
208
     *
209
     * @return Attribute
210
     */
211
    protected function online(): Attribute
212
    {
213
        $online = Session::expires()->where('user_id', $this->id)->first();
214
215
        return Attribute::make(
216
            get: fn () => !is_null($online)
217
        );
218
    }
219
220
    /**
221
     * Get the max role level of the user.
222
     *
223
     * @return Attribute
224
     */
225
    protected function level(): Attribute
226
    {
227
        return Attribute::make(
228
            get: fn () => ($role = $this->roles->sortByDesc('level')->first()) ? $role->level : 0
229
        );
230
    }
231
232
    /**
233
     * Parse a media and return it if isset or return the default avatar.
234
     *
235
     * @param string $type The type of the media to get.
236
     *
237
     * @return string
238
     */
239
    protected function parseMedia(string $type): string
240
    {
241
        if (isset($this->getMedia('avatar')[0])) {
242
            return $this->getMedia('avatar')[0]->getUrl($type);
243
        }
244
245
        return $this->defaultAvatar;
246
    }
247
248
    /**
249
     * Parse an attribute and return its value or empty if null.
250
     *
251
     * @param Object|null $relation The relation or the user object.
252
     *       Can be `$this` or `$this->account` for exemple
253
     * @param string|null $attribute The attribute to parse.
254
     *
255
     * @return string
256
     */
257
    protected function parse($relation, $attribute): string
258
    {
259
        if ($relation === null || $relation->{$attribute} === null) {
260
            return '';
261
        }
262
263
        return $relation->{$attribute};
264
    }
265
}
266