Completed
Branch develop (876d53)
by Nate
01:53
created

Accessor::queryOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\ember\services\traits\objects;
10
11
use craft\helpers\ArrayHelper;
12
use flipbox\ember\exceptions\ObjectNotFoundException;
13
use flipbox\ember\helpers\ObjectHelper;
14
use flipbox\ember\services\traits\queries\Accessor as QueryAccessor;
15
use yii\base\BaseObject;
16
use yii\db\ActiveRecord as Record;
17
use yii\db\QueryInterface;
18
19
/**
20
 * Used when an object configuration is stored in the database and upon it's retrieval, an object is
21
 * created and returned. Instances when the class name is stored at the record level, leave the static::objectClass()
22
 * null and the records 'type' or 'class' property will be used.
23
 *
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 *
27
 * @method BaseObject parentQueryOne(QueryInterface $query)
28
 * @method BaseObject[] parentQueryAll(QueryInterface $query)
29
 */
30
trait Accessor
31
{
32
    use QueryAccessor {
33
        queryOne as parentQueryOne;
34
        queryAll as parentQueryAll;
35
    }
36
37
    /*******************************************
38
     * OBJECT CLASSES
39
     *******************************************/
40
41
    /**
42
     * @return string|null
43
     */
44
    abstract public static function objectClass();
45
46
    /**
47
     * @return string
48
     */
49
    public static function objectClassInstance(): string
50
    {
51
        return BaseObject::class;
52
    }
53
54
    /*******************************************
55
     * CREATE
56
     *******************************************/
57
58
    /**
59
     * @param array $config
60
     * @return BaseObject
61
     * @throws \yii\base\InvalidConfigException
62
     */
63
    public function create($config = [])
64
    {
65
        if ($config instanceof Record) {
66
            $config = $this->prepareConfigFromRecord($config);
67
        }
68
69
        return ObjectHelper::create(
70
            $this->prepareConfig($config),
71
            static::objectClassInstance()
72
        );
73
    }
74
75
    /**
76
     * @param array $config
77
     * @return array
78
     */
79
    protected function prepareConfig($config = []): array
80
    {
81
        if (!is_array($config)) {
82
            $config = ArrayHelper::toArray($config, [], false);
83
        }
84
85
        // Auto-set the class
86
        $class = static::objectClass();
87
        if ($class !== null) {
88
            $config['class'] = $class;
89
        }
90
91
        return $config;
92
    }
93
94
    /**
95
     * @param Record $record
96
     * @return array
97
     */
98
    protected function prepareConfigFromRecord(Record $record): array
99
    {
100
        return array_merge(
101
            $record->getRelatedRecords(),
102
            $record->toArray()
103
        );
104
    }
105
106
    /**
107
     * @param Record $record
108
     * @return BaseObject
109
     */
110
    protected function createFromRecord(Record $record)
111
    {
112
        $config = array_merge(
113
            $record->getRelatedRecords(),
114
            $record->toArray()
115
        );
116
117
        return $this->create($config);
118
    }
119
120
121
    /*******************************************
122
     * FIND / GET
123
     *******************************************/
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function find($identifier)
129
    {
130
        $instance = static::objectClassInstance();
131
        if ($identifier instanceof $instance) {
132
            return $identifier;
133
        }
134
135
        return $this->findByCondition($identifier);
136
    }
137
138
    /*******************************************
139
     * CACHE
140
     *******************************************/
141
142
    /**
143
     * @inheritdoc
144
     * @throws \yii\base\InvalidConfigException
145
     */
146
    protected function queryOne(QueryInterface $query)
147
    {
148
        return $this->createFromQueryResult(
149
            $this->parentQueryOne($query)
150
        );
151
    }
152
153
    /**
154
     * @inheritdoc
155
     * @throws \yii\base\InvalidConfigException
156
     */
157
    protected function queryAll(QueryInterface $query)
158
    {
159
        return $this->createAllFromQueryResults(
160
            $this->parentQueryAll($query)
161
        );
162
    }
163
164
    /**
165
     * @param $result
166
     * @return null|BaseObject
167
     */
168
    protected function createFromQueryResult($result)
169
    {
170
        if ($result === null) {
171
            return null;
172
        }
173
174
        return $this->create($result);
175
    }
176
177
    /**
178
     * @param array $results
179
     * @return array
180
     */
181
    protected function createAllFromQueryResults(array $results): array
182
    {
183
        $objects = [];
184
        foreach ($results as $key => $value) {
185
            $objects[$key] = $this->create($value);
186
        }
187
188
        return $objects;
189
    }
190
191
    /*******************************************
192
     * EXCEPTIONS
193
     *******************************************/
194
195
    /**
196
     * @throws ObjectNotFoundException
197
     */
198
    protected function notFoundException()
199
    {
200
        throw new ObjectNotFoundException(
201
            sprintf(
202
                "Object not found."
203
            )
204
        );
205
    }
206
}
207