User::getRoles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace DoS\UserBundle\Model;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Sylius\Component\Rbac\Model\Role;
7
use Sylius\Component\Rbac\Model\RoleInterface;
8
use Sylius\Component\User\Model\User as BaseUser;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Sylius\Component\Media\Model\ImageInterface;
11
12
class User extends BaseUser implements UserInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $locale;
18
19
    /**
20
     * @var string
21
     */
22
    protected $displayname;
23
24
    /**
25
     * @var string
26
     */
27
    protected $confirmationType;
28
29
    /**
30
     * @var \DateTime
31
     */
32
    protected $confirmedAt;
33
34
    /**
35
     * @var ArrayCollection|Role[]
36
     */
37
    protected $authorizationRoles;
38
39
    /**
40
     * @var ImageInterface
41
     */
42
    protected $picture;
43
44
    public function __construct()
45
    {
46
        parent::__construct();
47
48
        $this->authorizationRoles = new ArrayCollection();
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function isLocked()
55
    {
56
        return $this->locked;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getDisplayName()
63
    {
64
        $customer = $this->getCustomer();
65
66
        return $this->displayname ?:
67
            (
68
                $customer && trim($customer->getFullName())
69
                ? $customer->getFullName()
70
                : $this->username
71
            )
72
        ;
73
    }
74
75
    /**
76
     * @param null|string $displayname
77
     */
78
    public function setDisplayName($displayname = null)
79
    {
80
        $this->displayname = $displayname;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getLocale()
87
    {
88
        return $this->locale;
89
    }
90
91
    /**
92
     * @param string $locale
93
     */
94
    public function setLocale($locale)
95
    {
96
        $this->locale = $locale;
97
    }
98
99
    /**
100
     * @return string|void
101
     */
102
    public function getLang()
103
    {
104
        if ($this->locale) {
105
            if (preg_match('/_([a-z]{2})/i', $this->locale, $match)) {
106
                return strtolower($match[1]);
107
            }
108
        }
109
110
        return;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getMediaPath()
117
    {
118
        return '/user/' . $this->usernameCanonical;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function setPicture(ImageInterface $picture = null)
125
    {
126
        $this->picture = $picture;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getPicture()
133
    {
134
        return $this->picture;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getProfilePicture()
141
    {
142
        if ($this->picture) {
143
            return $this->picture->getMediaId();
144
        }
145
146
        foreach ($this->oauthAccounts as $account) {
147
            if ($avatar = $account->getProfilePicture()) {
148
                return $avatar;
149
            }
150
        }
151
152
        return;
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    public function getConfirmedAt()
159
    {
160
        return $this->confirmedAt;
161
    }
162
163
    /**
164
     * @inheritdoc
165
     */
166
    public function setConfirmedAt(\DateTime $confirmedAt = null)
167
    {
168
        $this->confirmedAt = $confirmedAt;
169
    }
170
171
    /**
172
     * @inheritdoc
173
     */
174
    public function confirmed(\DateTime $confirmedAt = null)
175
    {
176
        $this->setConfirmedAt($confirmedAt ?: new \DateTime());
177
        $this->setEnabled(true);
178
        $this->setConfirmationToken(null);
179
        $this->setPasswordRequestedAt(null);
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185
    public function isConfirmed()
186
    {
187
        return $this->confirmedAt || $this->enabled;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function setEnabled($boolean)
194
    {
195
        $this->enabled = (Boolean)$boolean;
196
197
        if (!$this->isConfirmed()) {
198
            $this->enabled = false;
199
        }
200
201
        return $this;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function getConfirmationChannel($propertyPath)
208
    {
209
        $accessor = PropertyAccess::createPropertyAccessor();
210
211
        return $accessor->getValue($this, $propertyPath);
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getConfirmationRequestedAt()
218
    {
219
        return $this->getPasswordRequestedAt();
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function setConfirmationRequestedAt(\DateTime $dateTime = null)
226
    {
227
        $this->setPasswordRequestedAt($dateTime);
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function getConfirmationConfirmedAt()
234
    {
235
        return $this->getConfirmedAt();
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function setConfirmationConfirmedAt(\DateTime $dateTime = null)
242
    {
243
        $this->setConfirmedAt($dateTime);
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function isConfirmationConfirmed()
250
    {
251
        return $this->isConfirmed();
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function confirmationRequest($token)
258
    {
259
        $this->setConfirmationToken($token);
260
        $this->setConfirmationRequestedAt(new \DateTime());
261
        $this->setEnabled(false);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function confirmationConfirm()
268
    {
269
        $this->confirmed();
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function getConfirmationType()
276
    {
277
        return $this->confirmationType;
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function setConfirmationType($confirmationType)
284
    {
285
        $this->confirmationType = $confirmationType;
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function confirmationDisableAccess()
292
    {
293
        $this->enabled = false;
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function confirmationEnableAccess()
300
    {
301
        $this->enabled = true;
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307
    public function getAuthorizationRoles()
308
    {
309
        return $this->authorizationRoles;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->authorizationRoles; of type Doctrine\Common\Collecti...onent\Rbac\Model\Role[] adds the type Doctrine\Common\Collections\ArrayCollection to the return on line 309 which is incompatible with the return type declared by the interface Sylius\Component\Rbac\Mo...::getAuthorizationRoles of type Sylius\Component\Rbac\Model\RoleInterface[].
Loading history...
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315
    public function addAuthorizationRole(RoleInterface $role)
316
    {
317
        if (!$this->hasAuthorizationRole($role)) {
318
            $this->authorizationRoles->add($role);
319
        }
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325
    public function removeAuthorizationRole(RoleInterface $role)
326
    {
327
        if ($this->hasAuthorizationRole($role)) {
328
            $this->authorizationRoles->removeElement($role);
329
        }
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335
    public function hasAuthorizationRole(RoleInterface $role)
336
    {
337
        return $this->authorizationRoles->contains($role);
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function getRoles()
344
    {
345
        $roles = parent::getRoles();
346
347
        foreach ($this->getAuthorizationRoles() as $role) {
348
            $roles = array_merge($roles, $role->getSecurityRoles());
349
        }
350
351
        return $roles;
352
    }
353
}
354