Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

TokenMutator::resolveToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\records\traits;
10
11
use Craft;
12
use flipbox\ember\helpers\ObjectHelper;
13
use flipbox\ember\records\traits\ActiveRecord;
14
use flipbox\patron\Patron;
15
use flipbox\patron\records\Token;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * @property int|null $tokenId
20
 * @property Token|null $token
21
 * @property ActiveQueryInterface $tokenRecord
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
trait TokenMutator
27
{
28
    use ActiveRecord;
29
30
    /**
31
     * @var Token|null
32
     */
33
    private $token;
34
35
    /**
36
     * Set associated tokenId
37
     *
38
     * @param $id
39
     * @return $this
40
     */
41
    public function setTokenId(int $id)
42
    {
43
        $this->tokenId = $id;
44
        return $this;
45
    }
46
47
    /**
48
     * Get associated tokenId
49
     *
50
     * @return int|null
51
     */
52
    public function getTokenId()
53
    {
54
        $id = $this->getAttribute('tokenId');
55
        if (null === $id && null !== $this->token) {
56
            $id = $this->token->id;
57
            $this->setAttribute('tokenId', $id);
58
        }
59
60
        return $id;
61
    }
62
63
    /**
64
     * Associate a token
65
     *
66
     * @param mixed $token
67
     * @return $this
68
     */
69
    public function setToken($token = null)
70
    {
71
        $this->token = null;
72
73
        if (null === ($token = $this->internalResolveToken($token))) {
74
            $this->token = $this->tokenId = null;
75
        } else {
76
            $this->tokenId = $token->id;
77
            $this->token = $token;
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return Token|null
85
     */
86
    public function getToken()
87
    {
88
        if ($this->token === null) {
89
            $token = $this->resolveToken();
90
            $this->setToken($token);
91
            return $token;
92
        }
93
94
        $tokenId = $this->tokenId;
95
        if ($tokenId !== null &&
96
            $tokenId !== $this->token->id
97
        ) {
98
            $this->token = null;
99
            return $this->getToken();
100
        }
101
102
        return $this->token;
103
    }
104
105
    /**
106
     * @return Token|null
107
     */
108
    protected function resolveToken()
109
    {
110
        if (null !== ($model = $this->resolveTokenFromRelation())) {
111
            return $model;
112
        }
113
114
        if (null !== ($model = $this->resolveTokenFromId())) {
115
            return $model;
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * @return Token|null
123
     */
124
    private function resolveTokenFromRelation()
125
    {
126
        if (false === $this->isRelationPopulated('tokenRecord')) {
127
            return null;
128
        }
129
130
        /** @var Token $record */
131
        if (null === ($record = $this->getRelation('tokenRecord'))) {
132
            return null;
133
        }
134
135
        return $record;
136
    }
137
138
    /**
139
     * @return Token|null
140
     */
141
    private function resolveTokenFromId()
142
    {
143
        if (null === $this->tokenId) {
144
            return null;
145
        }
146
147
        return Patron::getInstance()->manageTokens()->find($this->tokenId);
148
    }
149
150
    /**
151
     * @param $token
152
     * @return Token|null
153
     */
154
    protected function internalResolveToken($token = null)
155
    {
156
        if ($token instanceof Token) {
157
            return $token;
158
        }
159
160
        if (is_numeric($token) || is_string($token)) {
161
            return Patron::getInstance()->manageTokens()->find($token);
162
        }
163
164
        try {
165
            $object = Craft::createObject(Token::class, [$token]);
166
        } catch (\Exception $e) {
167
            $object = new Token();
168
            ObjectHelper::populate(
169
                $object,
170
                $token
171
            );
172
        }
173
174
        /** @var Token $object */
175
        return $object;
176
    }
177
178
    /**
179
     * Returns the associated token record.
180
     *
181
     * @return ActiveQueryInterface
182
     */
183
    protected function getTokenRecord(): ActiveQueryInterface
184
    {
185
        return $this->hasOne(
186
            Token::class,
187
            ['id' => 'tokenId']
188
        );
189
    }
190
}
191