Completed
Push — develop ( 45ab37...1fc05f )
by Nate
16:44
created

TokenMutator   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 0
loc 163
ccs 0
cts 94
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setTokenId() 0 5 1
A getTokenId() 0 10 3
A setToken() 0 13 2
A getToken() 0 18 4
A resolveToken() 0 12 3
A resolveTokenFromRelation() 0 13 3
A resolveTokenFromId() 0 8 2
A internalResolveToken() 0 23 5
A getTokenRecord() 0 7 1
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\patron\Patron;
14
use flipbox\patron\records\Token;
15
use yii\db\ActiveQueryInterface;
16
17
/**
18
 * @property int|null $tokenId
19
 * @property Token|null $token
20
 * @property ActiveQueryInterface $tokenRecord
21
 *
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 */
25
trait TokenMutator
26
{
27
    /**
28
     * @var Token|null
29
     */
30
    private $token;
31
32
    /**
33
     * Set associated tokenId
34
     *
35
     * @param $id
36
     * @return $this
37
     */
38
    public function setTokenId(int $id)
39
    {
40
        $this->tokenId = $id;
41
        return $this;
42
    }
43
44
    /**
45
     * Get associated tokenId
46
     *
47
     * @return int|null
48
     */
49
    public function getTokenId()
50
    {
51
        $id = $this->getAttribute('tokenId');
0 ignored issues
show
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52
        if (null === $id && null !== $this->token) {
53
            $id = $this->token->id;
54
            $this->setAttribute('tokenId', $id);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
        }
56
57
        return $id;
58
    }
59
60
    /**
61
     * Associate a token
62
     *
63
     * @param mixed $token
64
     * @return $this
65
     */
66
    public function setToken($token = null)
67
    {
68
        $this->token = null;
69
70
        if (null === ($token = $this->internalResolveToken($token))) {
71
            $this->token = $this->tokenId = null;
72
        } else {
73
            $this->tokenId = $token->id;
74
            $this->token = $token;
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return Token|null
82
     */
83
    public function getToken()
84
    {
85
        if ($this->token === null) {
86
            $token = $this->resolveToken();
87
            $this->setToken($token);
88
            return $token;
89
        }
90
91
        $tokenId = $this->tokenId;
92
        if ($tokenId !== null &&
93
            $tokenId !== $this->token->id
94
        ) {
95
            $this->token = null;
96
            return $this->getToken();
97
        }
98
99
        return $this->token;
100
    }
101
102
    /**
103
     * @return Token|null
104
     */
105
    protected function resolveToken()
106
    {
107
        if (null !== ($model = $this->resolveTokenFromRelation())) {
108
            return $model;
109
        }
110
111
        if (null !== ($model = $this->resolveTokenFromId())) {
112
            return $model;
113
        }
114
115
        return null;
116
    }
117
118
    /**
119
     * @return Token|null
120
     */
121
    private function resolveTokenFromRelation()
122
    {
123
        if (false === $this->isRelationPopulated('tokenRecord')) {
0 ignored issues
show
Bug introduced by
It seems like isRelationPopulated() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
124
            return null;
125
        }
126
127
        /** @var Token $record */
128
        if (null === ($record = $this->getRelation('tokenRecord'))) {
0 ignored issues
show
Bug introduced by
It seems like getRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
129
            return null;
130
        }
131
132
        return $record;
133
    }
134
135
    /**
136
     * @return Token|null
137
     */
138
    private function resolveTokenFromId()
139
    {
140
        if (null === $this->tokenId) {
141
            return null;
142
        }
143
144
        return Patron::getInstance()->manageTokens()->find($this->tokenId);
145
    }
146
147
    /**
148
     * @param $token
149
     * @return Token|null
150
     */
151
    protected function internalResolveToken($token = null)
152
    {
153
        if ($token instanceof Token) {
154
            return $token;
155
        }
156
157
        if (is_numeric($token) || is_string($token)) {
158
            return Patron::getInstance()->manageTokens()->find($token);
159
        }
160
161
        try {
162
            $object = Craft::createObject(Token::class, [$token]);
163
        } catch (\Exception $e) {
164
            $object = new Token();
165
            ObjectHelper::populate(
166
                $object,
167
                $token
168
            );
169
        }
170
171
        /** @var Token $object */
172
        return $object;
173
    }
174
175
    /**
176
     * Returns the associated token record.
177
     *
178
     * @return ActiveQueryInterface
179
     */
180
    protected function getTokenRecord(): ActiveQueryInterface
181
    {
182
        return $this->hasOne(
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
183
            Token::class,
184
            ['id' => 'tokenId']
185
        );
186
    }
187
}
188