Completed
Push — develop ( 3f9c00...d25215 )
by Nate
02:35
created

UserMutatorTrait::resolveUserFromId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\objects;
10
11
use Craft;
12
use craft\elements\User;
13
14
/**
15
 * This trait accepts both an User or and User Id and ensures that the both
16
 * the User and the Id are in sync. If one changes (and does not match the other) it
17
 * resolves (removes / updates) the other.
18
 *
19
 * In addition, this trait is primarily useful when a new User is set and saved; the User
20
 * Id can be retrieved without needing to explicitly set the newly created Id.
21
 *
22
 * @property User|null $user
23
 *
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 2.0.0
26
 */
27
trait UserMutatorTrait
28
{
29
    /**
30
     * @var User|null
31
     */
32
    private $user;
33
34
    /**
35
     * Internally set the User Id.  This can be overridden. A record for example
36
     * should use `setAttribute`.
37
     *
38
     * @param int|null $id
39
     * @return $this
40
     */
41
    abstract protected function internalSetUserId(int $id = null);
42
43
    /**
44
     * Internally get the User Id.  This can be overridden.  A record for example
45
     * should use `getAttribute`.
46
     *
47
     * @return int|null
48
     */
49
    abstract protected function internalGetUserId();
50
51
    /**
52
     * @return bool
53
     */
54
    public function isUserSet(): bool
55
    {
56
        return null !== $this->user;
57
    }
58
59
    /**
60
     * Set associated userId
61
     *
62
     * @param $id
63
     * @return $this
64
     */
65
    public function setUserId(int $id)
66
    {
67
        $this->internalSetUserId($id);
68
69
        if (null !== $this->user && $id !== $this->user->id) {
70
            $this->user = null;
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * Get associated userId
78
     *
79
     * @return int|null
80
     */
81
    public function getUserId()
82
    {
83
        if (null === $this->internalGetUserId() && null !== $this->user) {
84
            $this->setUserId($this->user->id);
85
        }
86
87
        return $this->internalGetUserId();
88
    }
89
90
    /**
91
     * Associate a user
92
     *
93
     * @param mixed $user
94
     * @return $this
95
     */
96
    public function setUser($user = null)
97
    {
98
        $this->user = null;
99
        $this->internalSetUserId(null);
100
101
        if (null !== ($user = $this->verifyUser($user))) {
102
            $this->user = $user;
103
            $this->internalSetUserId($user->id);
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return User|null
111
     */
112
    public function getUser()
113
    {
114
        if ($this->user === null) {
115
            $user = $this->resolveUser();
116
            $this->setUser($user);
117
            return $user;
118
        }
119
120
        $userId = $this->internalGetUserId();
121
        if ($userId !== null && $userId !== $this->user->id) {
122
            $this->user = null;
123
            return $this->getUser();
124
        }
125
126
        return $this->user;
127
    }
128
129
    /**
130
     * @return User|null
131
     */
132
    protected function resolveUser()
133
    {
134
        if ($user = $this->resolveUserFromId()) {
135
            return $user;
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * @return User|null
143
     */
144
    private function resolveUserFromId()
145
    {
146
        if (null === ($userId = $this->internalGetUserId())) {
147
            return null;
148
        }
149
150
        return Craft::$app->getUsers()->getUserById($userId);
151
    }
152
153
    /**
154
     * @param $user
155
     * @return User|null
156
     */
157
    protected function verifyUser($user = null)
158
    {
159
        if ($user === null) {
160
            return null;
161
        }
162
163
        if ($user instanceof User) {
164
            return $user;
165
        }
166
167
        if (is_numeric($user)) {
168
            return Craft::$app->getUsers()->getUserById($user);
169
        }
170
171
        if (is_string($user)) {
172
            return Craft::$app->getUsers()->getUserByUsernameOrEmail($user);
173
        }
174
175
        return null;
176
    }
177
}
178