Completed
Push — add-user-settings ( 10fb12 )
by Fèvre
02:25
created

UserEntity::parseMedia()   A

Complexity

Conditions 2
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 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Xetaravel\Models\Entities;
3
4
use Xetaravel\Utility\UserUtility;
5
6
trait UserEntity
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
     * Get the small avatar.
17
     *
18
     * @return string
19
     */
20
    public function getAvatarSmallAttribute(): string
21
    {
22
        return $this->parseMedia('thumbnail.small');
23
    }
24
25
    /**
26
     * Get the medium avatar.
27
     *
28
     * @return string
29
     */
30
    public function getAvatarMediumAttribute(): string
31
    {
32
        return $this->parseMedia('thumbnail.medium');
33
    }
34
35
    /**
36
     * Get the big avatar.
37
     *
38
     * @return string
39
     */
40
    public function getAvatarBigAttribute(): string
41
    {
42
        return $this->parseMedia('thumbnail.big');
43
    }
44
45
    /**
46
     * Get the porofile background.
47
     *
48
     * @return string
49
     */
50
    public function getProfileBackgroundAttribute(): string
51
    {
52
        return UserUtility::getProfileBackground();
53
    }
54
55
    protected function parseMedia(string $type): string
56
    {
57
        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...
58
            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...
59
        }
60
61
        return $this->defaultAvatar;
62
    }
63
}
64