XetaIO /
Xetaravel
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace Xetaravel\Models\Presenters; |
||||
| 6 | |||||
| 7 | use Illuminate\Database\Eloquent\Casts\Attribute; |
||||
| 8 | use Illuminate\Support\Facades\DB; |
||||
| 9 | |||||
| 10 | trait UserPresenter |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * The default avatar used when there is no avatar for the user. |
||||
| 14 | * |
||||
| 15 | * @var string |
||||
| 16 | */ |
||||
| 17 | protected string $defaultAvatar = '/images/avatar.png'; |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * Get the user's username. |
||||
| 21 | * |
||||
| 22 | * @return Attribute |
||||
| 23 | */ |
||||
| 24 | protected function username(): Attribute |
||||
| 25 | { |
||||
| 26 | return Attribute::make( |
||||
| 27 | get: fn ($value) => $this->trashed() ? 'Deleted' : $value |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 28 | ); |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * Get the status of the user : online or offline |
||||
| 33 | * |
||||
| 34 | * @return Attribute |
||||
| 35 | */ |
||||
| 36 | protected function online(): Attribute |
||||
| 37 | { |
||||
| 38 | $sessionLifetime = config('session.lifetime') * 60; |
||||
| 39 | |||||
| 40 | $expirationTime = time() - $sessionLifetime; |
||||
| 41 | |||||
| 42 | $online = DB::table(config('session.table')) |
||||
| 43 | ->where('user_id', $this->id) |
||||
| 44 | ->where('last_activity', '>=', $expirationTime) |
||||
| 45 | ->first(); |
||||
| 46 | |||||
| 47 | return Attribute::make( |
||||
| 48 | get: fn () => !is_null($online) |
||||
| 49 | ); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * Get the max role level of the user. |
||||
| 54 | * |
||||
| 55 | * @return Attribute |
||||
| 56 | */ |
||||
| 57 | protected function level(): Attribute |
||||
| 58 | { |
||||
| 59 | return Attribute::make( |
||||
| 60 | get: fn () => ($role = $this->roles->sortByDesc('level')->first()) ? $role->level : 0 |
||||
| 61 | ); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | /** |
||||
| 65 | * Parse a media and return it if isset or return the default avatar. |
||||
| 66 | * |
||||
| 67 | * @param string $type The type of the media to get. |
||||
| 68 | * |
||||
| 69 | * @return string |
||||
| 70 | */ |
||||
| 71 | protected function parseMedia(string $type): string |
||||
| 72 | { |
||||
| 73 | if (isset($this->getMedia('avatar')[0])) { |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 74 | return $this->getMedia('avatar')[0]->getUrl($type); |
||||
| 75 | } |
||||
| 76 | |||||
| 77 | return $this->defaultAvatar; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Parse an attribute and return its value or empty if null. |
||||
| 82 | * |
||||
| 83 | * @param Object|null $relation The relation or the user object. |
||||
| 84 | * Can be `$this` or `$this->account` for exemple |
||||
| 85 | * @param string|null $attribute The attribute to parse. |
||||
| 86 | * |
||||
| 87 | * @return string |
||||
| 88 | */ |
||||
| 89 | protected function parse(?object $relation, ?string $attribute): string |
||||
| 90 | { |
||||
| 91 | if ($relation === null || $relation->{$attribute} === null) { |
||||
| 92 | return ''; |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | return $relation->{$attribute}; |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Get the profile url. |
||||
| 100 | * |
||||
| 101 | * @return Attribute |
||||
| 102 | */ |
||||
| 103 | protected function showUrl(): Attribute |
||||
| 104 | { |
||||
| 105 | $slug = $this->trashed() ? mb_strtolower($this->username) : $this->slug; |
||||
| 106 | |||||
| 107 | return Attribute::make( |
||||
| 108 | get: fn () => route('user.show', ['slug' => $slug]) |
||||
| 109 | ); |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | /** |
||||
| 113 | * Get the account facebook. |
||||
| 114 | * |
||||
| 115 | * @return Attribute |
||||
| 116 | */ |
||||
| 117 | protected function facebook(): Attribute |
||||
| 118 | { |
||||
| 119 | return Attribute::make( |
||||
| 120 | get: fn () => $this->parse($this->account, 'facebook') |
||||
| 121 | ); |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | /** |
||||
| 125 | * Get the account twitter. |
||||
| 126 | * |
||||
| 127 | * @return Attribute |
||||
| 128 | */ |
||||
| 129 | protected function twitter(): Attribute |
||||
| 130 | { |
||||
| 131 | return Attribute::make( |
||||
| 132 | get: fn () => $this->parse($this->account, 'twitter') |
||||
| 133 | ); |
||||
| 134 | } |
||||
| 135 | |||||
| 136 | /** |
||||
| 137 | * Get the account biography. |
||||
| 138 | * |
||||
| 139 | * @return Attribute |
||||
| 140 | */ |
||||
| 141 | protected function biography(): Attribute |
||||
| 142 | { |
||||
| 143 | return Attribute::make( |
||||
| 144 | get: fn () => $this->parse($this->account, 'biography') |
||||
| 145 | ); |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | /** |
||||
| 149 | * Get the account signature. |
||||
| 150 | * |
||||
| 151 | * @return Attribute |
||||
| 152 | */ |
||||
| 153 | protected function signature(): Attribute |
||||
| 154 | { |
||||
| 155 | return Attribute::make( |
||||
| 156 | get: fn () => $this->parse($this->account, 'signature') |
||||
| 157 | ); |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | /** |
||||
| 161 | * Get the small avatar. |
||||
| 162 | * |
||||
| 163 | * @return Attribute |
||||
| 164 | */ |
||||
| 165 | protected function avatarSmall(): Attribute |
||||
| 166 | { |
||||
| 167 | return Attribute::make( |
||||
| 168 | get: fn () => $this->parseMedia('thumbnail.small') |
||||
| 169 | ); |
||||
| 170 | } |
||||
| 171 | |||||
| 172 | /** |
||||
| 173 | * Get the medium avatar. |
||||
| 174 | * |
||||
| 175 | * @return Attribute |
||||
| 176 | */ |
||||
| 177 | protected function avatarMedium(): Attribute |
||||
| 178 | { |
||||
| 179 | return Attribute::make( |
||||
| 180 | get: fn () => $this->parseMedia('thumbnail.medium') |
||||
| 181 | ); |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * Get the big avatar. |
||||
| 186 | * |
||||
| 187 | * @return Attribute |
||||
| 188 | */ |
||||
| 189 | protected function avatarBig(): Attribute |
||||
| 190 | { |
||||
| 191 | return Attribute::make( |
||||
| 192 | get: fn () => $this->parseMedia('thumbnail.big') |
||||
| 193 | ); |
||||
| 194 | } |
||||
| 195 | |||||
| 196 | /** |
||||
| 197 | * Get the account first name. |
||||
| 198 | * |
||||
| 199 | * @return Attribute |
||||
| 200 | */ |
||||
| 201 | protected function firstName(): Attribute |
||||
| 202 | { |
||||
| 203 | return Attribute::make( |
||||
| 204 | get: fn () => $this->parse($this->account, 'first_name') |
||||
| 205 | ); |
||||
| 206 | } |
||||
| 207 | |||||
| 208 | /** |
||||
| 209 | * Get the account last name. |
||||
| 210 | * |
||||
| 211 | * @return Attribute |
||||
| 212 | */ |
||||
| 213 | protected function lastName(): Attribute |
||||
| 214 | { |
||||
| 215 | return Attribute::make( |
||||
| 216 | get: fn () => $this->parse($this->account, 'last_name') |
||||
| 217 | ); |
||||
| 218 | } |
||||
| 219 | |||||
| 220 | /** |
||||
| 221 | * Get whatever the user has rubies or not. |
||||
| 222 | * |
||||
| 223 | * @return Attribute |
||||
| 224 | */ |
||||
| 225 | protected function hasRubies(): Attribute |
||||
| 226 | { |
||||
| 227 | return Attribute::make( |
||||
| 228 | get: fn () => $this->rubies_total > 0 |
||||
| 229 | ); |
||||
| 230 | } |
||||
| 231 | |||||
| 232 | /** |
||||
| 233 | * Get the account full name. Return the username if the user |
||||
| 234 | * has not set his first name and last name. |
||||
| 235 | * |
||||
| 236 | * @return Attribute |
||||
| 237 | */ |
||||
| 238 | protected function fullName(): Attribute |
||||
| 239 | { |
||||
| 240 | return Attribute::make( |
||||
| 241 | get: function () { |
||||
| 242 | if ($this->trashed()) { |
||||
| 243 | return $this->username; |
||||
| 244 | } |
||||
| 245 | |||||
| 246 | $fullName = $this->parse($this->account, 'first_name') . ' ' . $this->parse($this->account, 'last_name'); |
||||
| 247 | |||||
| 248 | return empty(mb_trim($fullName)) ? $this->username : $fullName; |
||||
| 249 | } |
||||
| 250 | ); |
||||
| 251 | } |
||||
| 252 | } |
||||
| 253 |