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

InstanceMutator::resolveInstance()   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\records\ProviderInstance;
15
use yii\db\ActiveQueryInterface;
16
17
/**
18
 * @property int|null $instanceId
19
 * @property ProviderInstance|null $instance
20
 * @property ActiveQueryInterface $instanceRecord
21
 *
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 */
25
trait InstanceMutator
26
{
27
    use ActiveRecord;
28
29
    /**
30
     * @var ProviderInstance|null
31
     */
32
    private $instance;
33
34
    /**
35
     * Set associated instanceId
36
     *
37
     * @param $id
38
     * @return $this
39
     */
40
    public function setInstanceId(int $id)
41
    {
42
        $this->instanceId = $id;
43
        return $this;
44
    }
45
46
    /**
47
     * Get associated instanceId
48
     *
49
     * @return int|null
50
     */
51
    public function getInstanceId()
52
    {
53
        $id = $this->getAttribute('instanceId');
54
        if (null === $id && null !== $this->instance) {
55
            $id = $this->instance->id;
56
            $this->setAttribute('instanceId', $id);
57
        }
58
59
        return $id;
60
    }
61
62
    /**
63
     * Associate a instance
64
     *
65
     * @param mixed $instance
66
     * @return $this
67
     */
68
    public function setInstance($instance = null)
69
    {
70
        $this->instance = null;
71
72
        if (null === ($instance = $this->internalResolveInstance($instance))) {
73
            $this->instance = $this->instanceId = null;
74
        } else {
75
            /** @var ProviderInstance $instance */
76
            $this->instanceId = $instance->id;
77
            $this->instance = $instance;
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return ProviderInstance|null
85
     */
86
    public function getInstance()
87
    {
88
        if ($this->instance === null) {
89
            $instance = $this->resolveInstance();
90
            $this->setInstance($instance);
91
            return $instance;
92
        }
93
94
        $instanceId = $this->instanceId;
95
        if ($instanceId !== null &&
96
            $instanceId !== $this->instance->id
97
        ) {
98
            $this->instance = null;
99
            return $this->getInstance();
100
        }
101
102
        return $this->instance;
103
    }
104
105
    /**
106
     * @return ProviderInstance|null
107
     */
108
    protected function resolveInstance()
109
    {
110
        if (null !== ($model = $this->resolveInstanceFromRelation())) {
111
            return $model;
112
        }
113
114
        if (null !== ($model = $this->resolveInstanceFromId())) {
115
            return $model;
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * @return ProviderInstance|null
123
     */
124
    private function resolveInstanceFromRelation()
125
    {
126
        if (false === $this->isRelationPopulated('instanceRecord')) {
127
            return null;
128
        }
129
130
        /** @var ProviderInstance $record */
131
        if (null === ($record = $this->getRelation('instanceRecord'))) {
132
            return null;
133
        }
134
135
        return $record;
136
    }
137
138
    /**
139
     * @return ProviderInstance|null
140
     */
141
    private function resolveInstanceFromId()
142
    {
143
        if (null === $this->instanceId) {
144
            return null;
145
        }
146
147
        return ProviderInstance::findOne($this->instanceId);
148
    }
149
150
    /**
151
     * @param $instance
152
     * @return ProviderInstance|null
153
     */
154
    protected function internalResolveInstance($instance = null)
155
    {
156
        if ($instance instanceof ProviderInstance) {
157
            return $instance;
158
        }
159
160
        if (is_numeric($instance) || is_string($instance)) {
161
            /** @noinspection PhpIncompatibleReturnTypeInspection */
162
            return ProviderInstance::findOne($instance);
163
        }
164
165
        try {
166
            $object = Craft::createObject(ProviderInstance::class, [$instance]);
167
        } catch (\Exception $e) {
168
            $object = new ProviderInstance();
169
            ObjectHelper::populate(
170
                $object,
171
                $instance
172
            );
173
        }
174
175
        /** @var ProviderInstance $object */
176
        return $object;
177
    }
178
179
    /**
180
     * Returns the associated instance record.
181
     *
182
     * @return ActiveQueryInterface
183
     */
184
    protected function getInstanceRecord(): ActiveQueryInterface
185
    {
186
        return $this->hasOne(
187
            ProviderInstance::class,
188
            ['id' => 'instanceId']
189
        );
190
    }
191
}
192