Completed
Push — master ( f74ca7...ea85ac )
by Damien
10:39
created

UserHelper::isUserSuspended()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 */
6
7
namespace flipbox\saml\sp\helpers;
8
9
use craft\elements\User as UserElement;
10
11
/**
12
 * Class UserHelper
13
 *
14
 * @package flipbox\saml\sp\helpers
15
 */
16
class UserHelper
17
{
18
    /**
19
     * @param UserElement $user
20
     * @throws \Throwable
21
     */
22
    public static function enableUser(UserElement $user)
23
    {
24
        if (static::isUserSuspended($user)) {
25
            \Craft::$app->getUsers()->unsuspendUser($user);
26
        }
27
28
        if (static::isUserLocked($user)) {
29
            \Craft::$app->getUsers()->unlockUser($user);
30
        }
31
32
        if (! $user->enabled) {
33
            $user->enabled = true;
34
        }
35
36
        if ($user->archived) {
37
            $user->archived = false;
38
        }
39
40
        if (! static::isUserActive($user)) {
41
            \Craft::$app->getUsers()->activateUser($user);
42
        }
43
    }
44
45
    /**
46
     * @param UserElement $user
47
     * @return bool
48
     */
49 3
    public static function isUserPending(UserElement $user)
50
    {
51 3
        return false === static::isUserActive($user) &&
52 3
            $user->getStatus() === UserElement::STATUS_PENDING;
53
    }
54
55
    /**
56
     * @param UserElement $user
57
     * @return bool
58
     */
59 3
    public static function isUserArchived(UserElement $user)
60
    {
61 3
        return false === static::isUserActive($user) &&
62 3
            $user->getStatus() === UserElement::STATUS_ARCHIVED;
63
    }
64
65
    /**
66
     * @param UserElement $user
67
     * @return bool
68
     */
69 3
    public static function isUserLocked(UserElement $user)
70
    {
71 3
        return false === static::isUserActive($user) &&
72 3
            $user->getStatus() === UserElement::STATUS_LOCKED;
73
    }
74
75
    /**
76
     * @param UserElement $user
77
     * @return bool
78
     */
79 3
    public static function isUserSuspended(UserElement $user)
80
    {
81 3
        return false === static::isUserActive($user) &&
82 3
            $user->getStatus() === UserElement::STATUS_SUSPENDED;
83
    }
84
85
    /**
86
     * @param UserElement $user
87
     * @return bool
88
     */
89 6
    public static function isUserActive(UserElement $user)
90
    {
91 6
        return $user->getStatus() === UserElement::STATUS_ACTIVE;
92
    }
93
}
94