Completed
Push — master ( 69ca06...8af5d6 )
by Nate
03:36
created

UserMutator::getUser()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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