Completed
Push — master ( 50c6ef...1b86f4 )
by Fèvre
03:12
created

UserPresenter::getAvatarMediumAttribute()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
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
     * Get the account first name.
17
     *
18
     * @return string
19
     */
20
    public function getFirstNameAttribute(): string
21
    {
22
        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...
23
    }
24
25
    /**
26
     * Get the account last name.
27
     *
28
     * @return string
29
     */
30
    public function getLastNameAttribute(): string
31
    {
32
        return $this->parse($this->account, 'last_name');
33
    }
34
35
    /**
36
     * Get the account full name.
37
     *
38
     * @return string
39
     */
40
    public function getFullNameAttribute(): string
41
    {
42
        return $this->parse($this->account, 'first_name') . ' ' . $this->parse($this->account, 'last_name');
43
    }
44
45
    /**
46
     * Get the account facebook.
47
     *
48
     * @return string
49
     */
50
    public function getFacebookAttribute(): string
51
    {
52
        return $this->parse($this->account, 'facebook');
53
    }
54
55
    /**
56
     * Get the account twitter.
57
     *
58
     * @return string
59
     */
60
    public function getTwitterAttribute(): string
61
    {
62
        return $this->parse($this->account, 'twitter');
63
    }
64
65
    /**
66
     * Get the account biography.
67
     *
68
     * @return string
69
     */
70
    public function getBiographyAttribute(): string
71
    {
72
        return $this->parse($this->account, 'biography');
73
    }
74
75
    /**
76
     * Get the account signature.
77
     *
78
     * @return string
79
     */
80
    public function getSignatureAttribute(): string
81
    {
82
        return $this->parse($this->account, 'signature');
83
    }
84
85
    /**
86
     * Get the small avatar.
87
     *
88
     * @return string
89
     */
90
    public function getAvatarSmallAttribute(): string
91
    {
92
        return $this->parseMedia('thumbnail.small');
93
    }
94
95
    /**
96
     * Get the medium avatar.
97
     *
98
     * @return string
99
     */
100
    public function getAvatarMediumAttribute(): string
101
    {
102
        return $this->parseMedia('thumbnail.medium');
103
    }
104
105
    /**
106
     * Get the big avatar.
107
     *
108
     * @return string
109
     */
110
    public function getAvatarBigAttribute(): string
111
    {
112
        return $this->parseMedia('thumbnail.big');
113
    }
114
115
    /**
116
     * Get the profile background.
117
     *
118
     * @return string
119
     */
120
    public function getProfileBackgroundAttribute(): string
121
    {
122
        return UserUtility::getProfileBackground();
123
    }
124
125
    /**
126
     * Get the profile url.
127
     *
128
     * @return string
129
     */
130
    public function getProfileUrlAttribute(): string
131
    {
132
        if (!isset($this->slug)) {
133
            return '';
134
        }
135
136
        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...
137
    }
138
139
    /**
140
     * Parse a mdedia and return it if isset or return the default avatar.
141
     *
142
     * @param string $type The type of the media to get.
143
     *
144
     * @return string
145
     */
146
    protected function parseMedia(string $type): string
147
    {
148
        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...
149
            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...
150
        }
151
152
        return $this->defaultAvatar;
153
    }
154
155
    /**
156
     * Parse an attribute and return its value or empty if null.
157
     *
158
     * @param Object|null $relation The relation or the user object.
159
     *       Can be `$this` or `$this->account` for exemple
160
     * @param string|null $attribute The attribute to parse.
161
     *
162
     * @return string
163
     */
164
    protected function parse($relation, $attribute): string
165
    {
166
        if ($relation === null || $relation->{$attribute} === null) {
167
            return '';
168
        }
169
170
        return $relation->{$attribute};
171
    }
172
}
173