User::setProfiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace SDIS62\Core\User\Entity;
4
5
use Datetime;
6
use SDIS62\Core\Common\Entity\IdentityTrait;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use SDIS62\Core\User\Exception\InvalidGenderException;
9
use SDIS62\Core\User\Exception\InvalidPictureUrlException;
10
use SDIS62\Core\User\Exception\InvalidEmailAddressException;
11
12
class User
13
{
14
    use IdentityTrait;
15
16
    /**
17
     * Prénom de l'utilisateur
18
     *
19
     * @var string
20
     */
21
    protected $first_name;
22
23
    /**
24
     * Nom de l'utilisateur
25
     *
26
     * @var string
27
     */
28
    protected $last_name;
29
30
    /**
31
     * Sexe de l'utilisateur
32
     *
33
     * @var string
34
     */
35
    protected $gender;
36
37
    /**
38
     * Date de naissance
39
     *
40
     * @var \DateTime|null
41
     */
42
    protected $birthday;
43
44
     /**
45
      * URL de l'avatar de l'utilisateur
46
      *
47
      * @var string|null
48
      */
49
     protected $profile_pic_url;
50
51
     /**
52
      * Email
53
      *
54
      * @var string|null
55
      */
56
     protected $email;
57
58
    /**
59
     * Profils liés à l'utilisateur
60
     *
61
     * @var SDIS62\Core\User\Entity\Profile[]
62
     */
63
    protected $profiles;
64
65
    /*
66
     * Constructeur
67
     *
68
     * @param string $gender
69
     * @param string $first_name Prénom de l'utilisateur
70
     * @param string $last_name  Nom de l'utilisateur
71
     * @param string $email      Mail de l'utilisateur
72
     */
73 183
    public function __construct($gender, $first_name, $last_name, $email)
74
    {
75 183
        $this->setGender($gender);
76 183
        $this->setFirstName($first_name);
77 183
        $this->setLastName($last_name);
78 183
        $this->setEmail($email);
79
80 183
        $this->profiles = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<SDI...e\User\Entity\Profile>> of property $profiles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81 183
    }
82
83
    /**
84
     * Get the value of Prénom de l'utilisateur
85
     *
86
     * @return string
87
     */
88 12
    public function getFirstName()
89
    {
90 12
        return $this->first_name;
91
    }
92
93
    /**
94
     * Set the value of Prénom de l'utilisateur
95
     *
96
     * @param string first_name
97
     *
98
     * @return self
99
     */
100 183
    public function setFirstName($first_name)
101
    {
102 183
        $first_name = ucwords(strtolower((string) $first_name));
103 183
        $first_name = str_replace(' ', '-', $first_name);
104 183
        $this->first_name = $first_name;
105
106 183
        return $this;
107
    }
108
109
    /**
110
     * Get the value of Nom de l'utilisateur
111
     *
112
     * @return string
113
     */
114 12
    public function getLastName()
115
    {
116 12
        return $this->last_name;
117
    }
118
119
    /**
120
     * Set the value of Nom de l'utilisateur
121
     *
122
     * @param string last_name
123
     *
124
     * @return self
125
     */
126 183
    public function setLastName($last_name)
127
    {
128 183
        $this->last_name = strtoupper((string) $last_name);
129
130 183
        return $this;
131
    }
132
133
    /**
134
     * Récupération du nom complet
135
     *
136
     * @return string
137
     */
138 6
    public function getFullName()
139
    {
140 6
        $prefix = '';
141
142 6
        foreach ($this->getProfiles() as $profile) {
143 3
            if ($profile instanceof Profile\Sdis\SapeurPompierSdisProfile && $profile->isPro()) {
144 3
                $prefix = $profile->getGrade().' ';
145 3
                break;
146
            }
147 4
        }
148
149 6
        return $prefix.$this->getLastName().' '.$this->getFirstName();
150
    }
151
152
    /**
153
     * Get the value of Sexe de l'utilisateur
154
     *
155
     * @return string
156
     */
157 6
    public function getGender()
158
    {
159 6
        return $this->gender;
160
    }
161
162
    /**
163
     * Set the value of Sexe de l'utilisateur
164
     *
165
     * @param string gender
166
     * @throws InvalidGenderException
167
     *
168
     * @return self
169
     */
170 183
    public function setGender($gender)
171
    {
172 183
        if ($gender !== 'male' && $gender !== 'female') {
173 3
            throw new InvalidGenderException("Mauvais sexe affecté à l'utilisateur");
174
        }
175
176 183
        $this->gender = $gender;
177
178 183
        return $this;
179
    }
180
181
    /**
182
     * Get the value of Date de naissance
183
     *
184
     * @return \DateTime
185
     */
186 9
    public function getBirthday()
187
    {
188 9
        return $this->birthday;
189
    }
190
191
    /**
192
     * Set the value of Date de naissance. SI le paramètre est une chaine de caractère,
193
     * la date doit correspondre au format Y-m-d.
194
     *
195
     * @param \DateTime|string|null birthday
196
     *
197
     * @return self
198
     */
199 9
    public function setBirthday($birthday)
200
    {
201 9
        if ($birthday instanceof Datetime) {
202 3
            $this->birthday = $birthday;
203 9
        } elseif ($birthday === null) {
204 3
            $this->birthday = null;
205 2
        } else {
206 9
            $this->birthday = DateTime::createFromFormat('d-m-Y', (string) $birthday);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Datetime::createFromFor...Y', (string) $birthday) can also be of type false. However, the property $birthday is declared as type object<DateTime>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
207
        }
208
209 9
        return $this;
210
    }
211
212
    /**
213
     * Récupération de l'age de l'utilisateur
214
     *
215
     * @return int|null
216
     */
217 3
    public function getAge()
218
    {
219 3
        if ($this->getBirthday() === null) {
220 3
            return;
221
        }
222
223 3
        $now = new DateTime();
224 3
        $age = $this->getBirthday()->diff($now);
225
226 3
        return $age->y;
227
    }
228
229
    /**
230
     * Get the value of URL de l'avatar de l'utilisateur
231
     *
232
     * @return string|null
233
     */
234 6
    public function getPictureUrl()
235
    {
236 6
        return $this->profile_pic_url;
237
    }
238
239
    /**
240
     * Set the value of URL de l'avatar de l'utilisateur
241
     *
242
     * @param string|null profile_pic_url
243
     *
244
     * @return self
245
     */
246 9
    public function setPictureUrl($profile_pic_url)
247
    {
248 9
        if (!filter_var($profile_pic_url, FILTER_VALIDATE_URL)) {
249 3
            throw new InvalidPictureUrlException("URL incorrecte.");
250
        }
251
252 6
        $this->profile_pic_url = (string) strtolower($profile_pic_url);
253
254 6
        return $this;
255
    }
256
257
    /**
258
     * Get the value of Email
259
     *
260
     * @return string|null
261
     */
262 6
    public function getEmail()
263
    {
264 6
        return $this->email;
265
    }
266
267
    /**
268
     * Set the value of Email
269
     *
270
     * @param string|null email
271
     * @throws InvalidEmailAddressException
272
     *
273
     * @return self
274
     */
275 183
    public function setEmail($email)
276
    {
277 183
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
278 3
            throw new InvalidEmailAddressException("Email incorrect.");
279
        }
280
281 183
        $this->email = (string) strtolower($email);
282
283 183
        return $this;
284
    }
285
286
    /**
287
     * Récupération de la liste des profils
288
     *
289
     * @return SDIS62\Core\User\Entity\Profile[]
290
     */
291 15
    public function getProfiles()
292
    {
293 15
        return $this->profiles;
294
    }
295
296
    /**
297
     * Ajoute un profil à l'utilisateur
298
     *
299
     * @param  SDIS62\Core\User\Entity\Profile $profile
300
     * @return self
301
     */
302 126
    public function addProfile(Profile $profile)
303
    {
304 126
        $this->profiles[] = $profile;
305
306 126
        return $this;
307
    }
308
309
    /**
310
     * Ajoute un ensemble de profils
311
     *
312
     * @param  SDIS62\Core\User\Entity\Profile[] $profiles
313
     * @return self
314
     */
315 3
    public function setProfiles(array $profiles)
316
    {
317 3
        $this->profiles->clear();
0 ignored issues
show
Bug introduced by
The method clear cannot be called on $this->profiles (of type array<integer,object<SDI...e\User\Entity\Profile>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
318
319 3
        foreach ($profiles as $profile) {
320 3
            $this->addProfile($profile);
321 2
        }
322
323 3
        return $this;
324
    }
325
}
326