Completed
Push — master ( eae9dd...13bbf2 )
by Nate
06:11
created

UserMutatorTrait::isUserSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
use flipbox\craft\ember\helpers\ObjectHelper;
14
15
/**
16
 * @property int|null $userId
17
 *
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait UserMutatorTrait
22
{
23
    /**
24
     * @var User|null
25
     */
26
    private $user;
27
28
    /**
29
     * @return bool
30
     */
31
    public function isUserSet(): bool
32
    {
33
        return null !== $this->user;
34
    }
35
36
    /**
37
     * Set associated userId
38
     *
39
     * @param $id
40
     * @return $this
41
     */
42
    public function setUserId(int $id)
43
    {
44
        $this->userId = $id;
45
        return $this;
46
    }
47
48
    /**
49
     * Get associated userId
50
     *
51
     * @return int|null
52
     */
53
    public function getUserId()
54
    {
55
        if (null === $this->userId && null !== $this->user) {
56
            $this->userId = $this->user->id;
57
        }
58
59
        return $this->userId;
60
    }
61
62
    /**
63
     * Associate a user
64
     *
65
     * @param mixed $user
66
     * @return $this
67
     */
68
    public function setUser($user = null)
69
    {
70
        $this->user = null;
71
72
        if (!$user = $this->internalResolveUser($user)) {
73
            $this->user = $this->userId = null;
74
        } else {
75
            $this->userId = $user->id;
76
            $this->user = $user;
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * @return User|null
84
     */
85
    public function getUser()
86
    {
87
        if ($this->user === null) {
88
            $user = $this->resolveUser();
89
            $this->setUser($user);
90
            return $user;
91
        }
92
93
        $userId = $this->userId;
94
        if ($userId !== null &&
95
            $userId !== $this->user->id
96
        ) {
97
            $this->user = null;
98
            return $this->getUser();
99
        }
100
101
        return $this->user;
102
    }
103
104
    /**
105
     * @return User|null
106
     */
107
    protected function resolveUser()
108
    {
109
        if ($model = $this->resolveUserFromId()) {
110
            return $model;
111
        }
112
113
        return null;
114
    }
115
116
    /**
117
     * @return User|null
118
     */
119
    private function resolveUserFromId()
120
    {
121
        if (null === $this->userId) {
122
            return null;
123
        }
124
125
        return Craft::$app->getUsers()->getUserById($this->userId);
126
    }
127
128
    /**
129
     * @param $user
130
     * @return User|null
131
     */
132
    protected function internalResolveUser($user = null)
133
    {
134
        if ($user === null) {
135
            return null;
136
        }
137
138
        if ($user instanceof User) {
139
            return $user;
140
        }
141
142
        if (is_numeric($user)) {
143
            return Craft::$app->getUsers()->getUserById($user);
144
        }
145
146
        if (is_string($user)) {
147
            return Craft::$app->getUsers()->getUserByUsernameOrEmail($user);
148
        }
149
150
        try {
151
            $object = Craft::createObject(User::class, [$user]);
152
        } catch (\Exception $e) {
153
            $object = new User();
154
            ObjectHelper::populate(
155
                $object,
156
                $user
157
            );
158
        }
159
160
        /** @var User $object */
161
        return $object;
162
    }
163
}
164