Completed
Push — develop ( c603db...77b219 )
by
unknown
16:24 queued 08:23
created

User::getStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license       MIT
7
 */
8
9
namespace Auth\Entity;
10
11
use Core\Entity\AbstractIdentifiableEntity;
12
use Core\Entity\Collection\ArrayCollection;
13
use Core\Entity\DraftableEntityInterface;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
16
use Organizations\Entity\OrganizationReferenceInterface;
17
use Settings\Repository\SettingsEntityResolver;
18
19
/**
20
 * Defines an user model
21
 *
22
 * @ODM\Document(collection="users", repositoryClass="Auth\Repository\User")
23
 *  * @ODM\Indexes({
24
 *      @ODM\Index(keys={
25
 *                  "login"="text",
26
 *                  "role"="text",
27
 *                    "info.email"="text",
28
 *                    "info.firstName"="text",
29
 *                    "info.lastName"="text"
30
 *                 }, name="fulltext")
31
 * })
32
 */
33
class User extends AbstractIdentifiableEntity implements UserInterface, DraftableEntityInterface
34
{
35
    /**
36
     * Users login name
37
     *
38
     * @var string
39
     * @ODM\Field(type="string")
40
     * @ODM\Index(unique=true, sparse=true, order="asc")
41
     */
42
    protected $login;
43
44
    /**
45
     * Role of an user. Currently "user" or "recruiter"
46
     *
47
     * @ODM\Field(type="string")
48
     */
49
    protected $role;
50
51
    /**
52
     * Users contact data.
53
     *
54
     * @ODM\EmbedOne(targetDocument="Info")
55
     */
56
    protected $info;
57
58
    /**
59
     * Authentification Sessions like oAuth
60
     * After Authentification with OAuth sessions can be stored like a password/key pair
61
     *
62
     * @ODM\EmbedMany(targetDocument="AuthSession")
63
     */
64
    protected $authSessions;
65
66
    /**
67
     * Users login password
68
     *
69
     * @ODM\Field(type="string")
70
     */
71
    protected $credential;
72
73
    /**
74
     * Users primary email address
75
     *
76
     * @ODM\Field(type="string")
77
     */
78
    protected $email;
79
80
    /**
81
     * pre-shared key, which allows an external application to authenticate
82
     *
83
     * @var String
84
     * @ODM\Field(type="string")
85
     */
86
    protected $secret;
87
88
    /**
89
     * Can contain various HybridAuth profiles.
90
     * Deprecated: replaced by User::$profiles
91
     *
92
     * @var array
93
     * @deprecated
94
     * @ODM\Hash
95
     */
96
    protected $profile = array();
97
98
    /**
99
     * Can contain various HybridAuth profiles.
100
     *
101
     * @var array
102
     * @ODM\Hash
103
     */
104
    protected $profiles = [];
105
106
    /** @var array
107
     * @ODM\EmbedMany(discriminatorField="_entity")
108
     */
109
    protected $settings;
110
111
    /**
112
     * This is not a persistent property!
113
     *
114
     * @var SettingsEntityResolver
115
     */
116
    protected $settingsEntityResolver;
117
118
    /**
119
     * User groups.
120
     *
121
     * @var Collection
122
     * @ODM\ReferenceMany(targetDocument="Group", mappedBy="owner", simple=true, cascade="all")
123
     */
124
    protected $groups;
125
126
    /**
127
     * User tokens. Is generated when recovering Passwords as a short term key.
128
     *
129
     * @var Collection
130
     * @ODM\EmbedMany(targetDocument="Token")
131
     */
132
    protected $tokens;
133
134
    /**
135
     * The organization reference for the user.
136
     *
137
     * This field is not stored in the database, but injected on postLoad via
138
     * {@link \Organizations\Repository\Event\InjectOrganizationReferenceListener}
139
     *
140
     * @var OrganizationReferenceInterface
141
     *
142
     * @since 0.18
143
     */
144
    protected $organization;
145
146
    /**
147
     * Is this entity a draft or not?
148
     *
149
     * @var bool
150
     * @ODM\Boolean
151
     */
152
    protected $isDraft = false;
153
    
154
    /**
155
     * Status of user
156
     *
157
     * @var Status
158
     * @ODM\EmbedOne(targetDocument="Status")
159
     * @ODM\Index
160
     */
161
    protected $status;
162
163
    /**
164
     * @see http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/best-practices.html
165
     * It is recommended best practice to initialize any business collections in documents in the constructor.
166
     * {mg: What about lazy loading? Initialize the Collection in the getter, if none is set? Reduce overload.}
167
     */
168
    public function __construct()
169
    {
170
        $this->status = new Status();
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function isDraft()
177
    {
178
        return $this->isDraft;
179
    }
180
181
    /**
182
     * @param bool $flag
183
     * @return $this
184
     */
185
    public function setIsDraft($flag)
186
    {
187
        $this->isDraft = (bool) $flag;
188
189
        return $this;
190
    }
191
192
193
    /** {@inheritdoc} */
194
    public function setLogin($login)
195
    {
196
        $this->login = trim((String)$login);
197
        return $this;
198
    }
199
200
    /** {@inheritdoc} */
201
    public function getLogin()
202
    {
203
        return $this->login;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function setRole($role)
210
    {
211
        $this->role = $role;
212
        return $this;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function getRole()
219
    {
220
        if (!$this->role) {
221
            $this->setRole('user');
222
        }
223
        return $this->role;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function getRoleId()
230
    {
231
        return $this->getRole();
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function setInfo(InfoInterface $info)
238
    {
239
        $this->info = $info;
240
        return $this;
241
    }
242
243
    /** {@inheritdoc} */
244
    public function getInfo()
245
    {
246
        if (null == $this->info) {
247
            $this->setInfo(new Info());
248
        }
249
        return $this->info;
250
    }
251
252
    /**
253
     * @param $key
254
     * @param $sessionParameter
255
     * @return $this
256
     */
257
    public function updateAuthSession($key, $sessionParameter)
258
    {
259
        $notExists = true;
260
261
        foreach ($this->authSessions as $authSession) {
262
            /* @var $authSession AuthSession */
263
            if ($key == $authSession->getName()) {
264
                $authSession->setSession($sessionParameter);
265
                $notExists = false;
266
            }
267
        }
268
        if ($notExists) {
269
            $authSession = new AuthSession();
270
            $authSession->setName($key);
271
            $authSession->setSession($sessionParameter);
272
            $this->authSessions[] = $authSession;
273
        }
274
        return $this;
275
    }
276
277
    /**
278
     * @param $key
279
     * @return null
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
280
     */
281
    public function getAuthSession($key)
282
    {
283
        $result = null;
284
285
        foreach ($this->authSessions as $authSession) {
286
            /* @var $authSession AuthSession */
287
            if ($key == $authSession->getName()) {
288
                $result = $authSession->getSession();
289
            }
290
        }
291
        return $result;
292
    }
293
294
    /**
295
     * removes a stored Session
296
     * @param string|null $key providerName, if null, remove all sessions
297
     * @return $this
298
     */
299
    public function removeSessionData($key = null)
300
    {
301
        $authSessionRefresh = array();
302
        foreach ($this->authSessions as $authSession) {
303
            /* @var $authSession AuthSession */
304
            if (isset($key) && $key != $authSession->getName()) {
305
                $authSessionRefresh[] = $authSession;
306
            }
307
        }
308
        $this->authSessions = $authSessionRefresh;
309
        return $this;
310
    }
311
312
    /** {@inheritdoc} */
313
    public function getCredential()
314
    {
315
        return $this->credential;
316
    }
317
318
    /** {@inheritdoc} */
319
    public function setPassword($password)
320
    {
321
        $filter = new Filter\CredentialFilter();
322
        $credential = $filter->filter($password);
323
        return $this->setCredential($credential);
324
    }
325
326
    /** {@inheritdoc} */
327
    public function setCredential($credential)
328
    {
329
        $this->credential = $credential;
330
        return $this;
331
    }
332
333
    /** {@inheritdoc} */
334
    public function getSecret()
335
    {
336
        if (isset($this->secret)) {
337
            return $this->secret;
338
        }
339
        return $this->credential;
340
    }
341
342
    /** {@inheritdoc} */
343
    public function setSecret($secret)
344
    {
345
        $this->secret = $secret;
346
        return $this;
347
    }
348
349
    /** {@inheritdoc} */
350
    public function getEmail()
351
    {
352
        return $this->email ?: $this->getInfo()->getEmail();
353
    }
354
355
    /** {@inheritdoc} */
356
    public function setEmail($email)
357
    {
358
        $this->email = $email;
359
        return $this;
360
    }
361
362
    /** {@inheritdoc} */
363
    public function setProfile(array $profile)
364
    {
365
        $this->profile = $profile;
0 ignored issues
show
Deprecated Code introduced by
The property Auth\Entity\User::$profile has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
366
        return $this;
367
    }
368
369
    /** {@inheritdoc} */
370
    public function getProfile($provider = null)
371
    {
372
        if (!isset($provider))
373
        {
374
            return $this->profiles;
375
        }
376
        
377
        return isset($this->profiles[$provider]) ? $this->profiles[$provider] : [];
378
    }
379
380
    /**
381
     * @param string $provider
382
     * @param array $data
383
     * @return \Auth\Entity\User
384
     */
385
    public function addProfile($provider, array $data)
386
    {
387
        $this->profiles[$provider] = $data;
388
        return $this;
389
    }
390
    
391
    /**
392
     * @param string $provider
393
     * @return \Auth\Entity\User
394
     */
395
    public function removeProfile($provider)
396
    {
397
        unset($this->profiles[$provider]);
398
        return $this;
399
    }
400
401
    /** {@inheritdoc} */
402
    public function setSettingsEntityResolver($resolver)
403
    {
404
        $this->settingsEntityResolver = $resolver;
405
    }
406
407
    /** {@inheritdoc} */
408
    public function getSettings($module)
409
    {
410
        if (!isset($module)) {
411
            throw new \InvalidArgumentException('$module must not be null.');
412
        }
413
414
        if (!$this->settings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->settings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
415
            $this->settings = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Core\Entity\Collection\ArrayCollection() of type object<Core\Entity\Collection\ArrayCollection> is incompatible with the declared type array of property $settings.

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...
416
        }
417
418
        foreach ($this->settings as $settings) {
419
            if ($settings->moduleName == $module) {
420
                return $settings;
421
            }
422
        }
423
424
        $settings = $this->settingsEntityResolver->getNewSettingsEntity($module);
425
        $this->settings->add($settings);
426
        return $settings;
427
    }
428
429
    /** {@inheritdoc} */
430
    public function getGroups()
431
    {
432
        if (!$this->groups) {
433
            $this->groups = new ArrayCollection();
434
        }
435
        return $this->groups;
436
    }
437
438
    /** {@inheritdoc} */
439
    public function getGroup($name, $create = false)
440
    {
441
        $groups = $this->getGroups();
442
        foreach ($groups as $group) {
443
            /* @var $group GroupInterface */
444
            if ($group->getName() == $name) {
445
                return $group;
446
            }
447
        }
448
        if ($create) {
449
            $group = new Group($name, $this);
450
            $groups->add($group);
451
            return $group;
452
        }
453
        return null;
454
    }
455
456
    /**
457
     * @return Collection
458
     */
459
    public function getTokens()
460
    {
461
        if (!$this->tokens) {
462
            $this->tokens = new ArrayCollection();
463
        }
464
465
        return $this->tokens;
466
    }
467
468
    /**
469
     * @param Collection $tokens
470
     */
471
    public function setTokens($tokens)
472
    {
473
        $this->tokens = $tokens;
474
    }
475
476
    /**
477
     * @param OrganizationReferenceInterface $organization
478
     * @return $this
479
     */
480
    public function setOrganization(OrganizationReferenceInterface $organization)
481
    {
482
        $this->organization = $organization;
483
484
        return $this;
485
    }
486
487
    /**
488
     * @return bool
489
     */
490
    public function hasOrganization()
491
    {
492
        /* @var $this->organization \Organizations\Entity\OrganizationReference */
493
        return $this->organization &&
494
               $this->organization->hasAssociation();
495
    }
496
497
    /**
498
     * @return OrganizationReferenceInterface
499
     */
500
    public function getOrganization()
501
    {
502
        return $this->organization;
503
    }
504
    
505
    /**
506
     * @return Status
507
     */
508
    public function getStatus()
509
    {
510
        if (!isset($this->status)) {
511
            $this->status = new Status();
512
        }
513
        
514
        return $this->status;
515
    }
516
    
517
    /**
518
     * @param Status $status
519
     */
520
    public function setStatus($status)
521
    {
522
        if (!$status instanceof Status) {
523
            $status = new Status($status);
524
        }
525
        
526
        $this->status = $status;
527
    }
528
    
529
    /**
530
     * @return boolean
531
     */
532
    public function isActive()
533
    {
534
        return $this->getStatus()->getName() === \Jobs\Entity\StatusInterface::ACTIVE;
535
    }
536
}
537