Completed
Branch develop (4949ff)
by Nate
02:09
created

Accessor::prepareConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
crap 12
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
trait Accessor
28
{
29
    use QueryAccessor {
30
        queryOne as parentQueryOne;
31
        queryAll as parentQueryAll;
32
    }
33
34
    /*******************************************
35
     * OBJECT CLASSES
36
     *******************************************/
37
38
    /**
39
     * @return string|null
40
     */
41
    abstract public static function objectClass();
42
43
    /**
44
     * @return string
45
     */
46
    public static function objectClassInstance(): string
47
    {
48
        return BaseObject::class;
49
    }
50
51
    /*******************************************
52
     * CREATE
53
     *******************************************/
54
55
    /**
56
     * @param array $config
57
     * @return BaseObject
58
     * @throws \yii\base\InvalidConfigException
59
     */
60
    public function create($config = [])
61
    {
62
        if ($config instanceof Record) {
63
            $config = $this->prepareConfigFromRecord($config);
64
        }
65
66
        return ObjectHelper::create(
67
            $this->prepareConfig($config),
68
            static::objectClassInstance()
69
        );
70
    }
71
72
    /**
73
     * @param array $config
74
     * @return array
75
     */
76
    protected function prepareConfig($config = []): array
77
    {
78
        if (!is_array($config)) {
79
            $config = ArrayHelper::toArray($config, [], false);
80
        }
81
82
        // Auto-set the class
83
        $class = static::objectClass();
84
        if ($class !== null) {
85
            $config['class'] = $class;
86
        }
87
88
        return $config;
89
    }
90
91
    /**
92
     * @param Record $record
93
     * @return array
94
     */
95
    protected function prepareConfigFromRecord(Record $record): array
96
    {
97
        return array_merge(
98
            $record->getRelatedRecords(),
99
            $record->toArray()
100
        );
101
    }
102
103
    /**
104
     * @param Record $record
105
     * @return BaseObject
106
     * @throws \yii\base\InvalidConfigException
107
     */
108
    protected function createFromRecord(Record $record)
109
    {
110
        $config = array_merge(
111
            $record->getRelatedRecords(),
112
            $record->toArray()
113
        );
114
115
        return $this->create($config);
116
    }
117
118
119
    /*******************************************
120
     * FIND / GET
121
     *******************************************/
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function find($identifier)
127
    {
128
        $instance = static::objectClassInstance();
129
        if ($identifier instanceof $instance) {
130
            return $identifier;
131
        }
132
133
        return $this->findByCondition($identifier);
134
    }
135
136
    /*******************************************
137
     * CACHE
138
     *******************************************/
139
140
    /**
141
     * @inheritdoc
142
     * @throws \yii\base\InvalidConfigException
143
     */
144
    protected function queryOne(QueryInterface $query)
145
    {
146
        return $this->createFromQueryResult(
147
            $this->parentQueryOne($query)
0 ignored issues
show
Bug introduced by
The method parentQueryOne() does not exist on flipbox\ember\services\traits\objects\Accessor. Did you maybe mean queryOne()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
148
        );
149
    }
150
151
    /**
152
     * @inheritdoc
153
     * @throws \yii\base\InvalidConfigException
154
     */
155
    protected function queryAll(QueryInterface $query)
156
    {
157
        return $this->createAllFromQueryResults(
158
            $this->parentQueryAll($query)
0 ignored issues
show
Bug introduced by
The method parentQueryAll() does not exist on flipbox\ember\services\traits\objects\Accessor. Did you maybe mean queryAll()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
159
        );
160
    }
161
162
    /**
163
     * @param $result
164
     * @return null|BaseObject
165
     */
166
    protected function createFromQueryResult($result)
167
    {
168
        if ($result === null) {
169
            return null;
170
        }
171
172
        return $this->create($result);
173
    }
174
175
    /**
176
     * @param array $results
177
     * @return array
178
     * @throws \yii\base\InvalidConfigException
179
     */
180
    protected function createAllFromQueryResults(array $results): array
181
    {
182
        $objects = [];
183
        foreach ($results as $key => $value) {
184
            $objects[$key] = $this->create($value);
185
        }
186
187
        return $objects;
188
    }
189
190
    /*******************************************
191
     * EXCEPTIONS
192
     *******************************************/
193
194
    /**
195
     * @throws ObjectNotFoundException
196
     */
197
    protected function notFoundException()
198
    {
199
        throw new ObjectNotFoundException(
200
            sprintf(
201
                "Object not found."
202
            )
203
        );
204
    }
205
}
206