Completed
Push — master ( 0fc735...e9e143 )
by
07:12
created

User::setConfirmationType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DoS\UserBundle\Model;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Sylius\Component\Rbac\Model\Role;
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
     * {@inheritdoc}
53
     */
54
    public function getAuthorizationRoles()
55
    {
56
        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 56 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...
57
    }
58
59
    /**
60
     * @param ArrayCollection|\Sylius\Component\Rbac\Model\Role[] $authorizationRoles
61
     */
62
    public function setAuthorizationRoles($authorizationRoles)
63
    {
64
        if (!$authorizationRoles instanceof Collection) {
65
            $authorizationRoles = new ArrayCollection($authorizationRoles);
66
        }
67
68
        $this->authorizationRoles = $authorizationRoles;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function resizeSecurityRoles()
75
    {
76
        $this->roles = array(self::DEFAULT_ROLE);
77
78
        foreach($this->authorizationRoles as $role) {
79
            foreach($role->getSecurityRoles() as $r) {
80
                $this->roles[] = $r;
81
            }
82
        }
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isLocked()
89
    {
90
        return $this->locked;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getDisplayName()
97
    {
98
        $customer = $this->getCustomer();
99
100
        return $this->displayname ?:
101
            (
102
                $customer && trim($customer->getFullName())
103
                ? $customer->getFullName()
104
                : $this->username
105
            )
106
        ;
107
    }
108
109
    /**
110
     * @param null|string $displayname
111
     */
112
    public function setDisplayName($displayname = null)
113
    {
114
        $this->displayname = $displayname;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getLocale()
121
    {
122
        return $this->locale;
123
    }
124
125
    /**
126
     * @param string $locale
127
     */
128
    public function setLocale($locale)
129
    {
130
        $this->locale = $locale;
131
    }
132
133
    /**
134
     * @return string|void
135
     */
136
    public function getLang()
137
    {
138
        if ($this->locale) {
139
            if (preg_match('/_([a-z]{2})/i', $this->locale, $match)) {
140
                return strtolower($match[1]);
141
            }
142
        }
143
144
        return;
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function getMediaPath()
151
    {
152
        return '/user/' . $this->usernameCanonical;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function setPicture(ImageInterface $picture = null)
159
    {
160
        $this->picture = $picture;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getPicture()
167
    {
168
        return $this->picture;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getProfilePicture()
175
    {
176
        if ($this->picture) {
177
            return $this->picture->getMediaId();
178
        }
179
180
        foreach ($this->oauthAccounts as $account) {
181
            if ($avatar = $account->getProfilePicture()) {
182
                return $avatar;
183
            }
184
        }
185
186
        return;
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192
    public function getConfirmedAt()
193
    {
194
        return $this->confirmedAt;
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200
    public function setConfirmedAt(\DateTime $confirmedAt = null)
201
    {
202
        $this->confirmedAt = $confirmedAt;
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    public function confirmed(\DateTime $confirmedAt = null)
209
    {
210
        $this->setConfirmedAt($confirmedAt ?: new \DateTime());
211
        $this->setEnabled(true);
212
        $this->setConfirmationToken(null);
213
        $this->setPasswordRequestedAt(null);
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219
    public function isConfirmed()
220
    {
221
        return $this->confirmedAt || $this->enabled;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function setEnabled($boolean)
228
    {
229
        $this->enabled = (Boolean)$boolean;
230
231
        if (!$this->isConfirmed()) {
232
            $this->enabled = false;
233
        }
234
235
        return $this;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function getConfirmationChannel($propertyPath)
242
    {
243
        $accessor = PropertyAccess::createPropertyAccessor();
244
245
        return $accessor->getValue($this, $propertyPath);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function getConfirmationRequestedAt()
252
    {
253
        return $this->getPasswordRequestedAt();
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function setConfirmationRequestedAt(\DateTime $dateTime = null)
260
    {
261
        $this->setPasswordRequestedAt($dateTime);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function getConfirmationConfirmedAt()
268
    {
269
        return $this->getConfirmedAt();
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function setConfirmationConfirmedAt(\DateTime $dateTime = null)
276
    {
277
        $this->setConfirmedAt($dateTime);
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function isConfirmationConfirmed()
284
    {
285
        return $this->isConfirmed();
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function confirmationRequest($token)
292
    {
293
        $this->setConfirmationToken($token);
294
        $this->setConfirmationRequestedAt(new \DateTime());
295
        $this->setEnabled(false);
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301
    public function confirmationConfirm()
302
    {
303
        $this->confirmed();
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function getConfirmationType()
310
    {
311
        return $this->confirmationType;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function setConfirmationType($confirmationType)
318
    {
319
        $this->confirmationType = $confirmationType;
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325
    public function confirmationDisableAccess()
326
    {
327
        $this->enabled = false;
328
    }
329
330
    /**
331
     * {@inheritdoc}
332
     */
333
    public function confirmationEnableAccess()
334
    {
335
        $this->enabled = true;
336
    }
337
}
338