Completed
Branch develop (9e5d0f)
by Nate
01:59
created

AccessorActiveRecord   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 6
dl 0
loc 245
ccs 0
cts 114
cp 0
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
recordClass() 0 1 ?
A recordClassInstance() 0 4 1
A getRecordQuery() 0 16 2
A createRecord() 0 20 3
A findRecordByCondition() 0 11 2
A findRecordByCriteria() 0 14 3
A getRecordByCondition() 0 8 2
A getRecordByCriteria() 0 8 2
A findAllRecords() 0 4 1
A findAllRecordsByCondition() 0 7 1
A findAllRecordsByCriteria() 0 19 3
A getAllRecords() 0 9 1
A getAllRecordsByCondition() 0 8 2
A getAllRecordsByCriteria() 0 8 2
A notFoundRecordException() 0 8 1
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;
10
11
use Craft;
12
use craft\db\ActiveRecord as Record;
13
use flipbox\ember\exceptions\RecordNotFoundException;
14
use flipbox\ember\helpers\QueryHelper;
15
use flipbox\ember\helpers\RecordHelper;
16
use yii\db\ActiveQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait AccessorActiveRecord
23
{
24
    /**
25
     * @return string
26
     */
27
    public abstract static function recordClass(): string;
28
29
    /**
30
     * @return string
31
     */
32
    public static function recordClassInstance(): string
33
    {
34
        return Record::class;
35
    }
36
37
    /**
38
     * @param array $config
39
     * @return \yii\db\ActiveQuery
40
     */
41
    public function getRecordQuery($config = []): ActiveQuery
42
    {
43
        /** @var Record $recordClass */
44
        $recordClass = $this->recordClass();
45
46
        $query = $recordClass::find();
47
48
        if ($config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
49
            QueryHelper::configure(
50
                $query,
51
                $config
52
            );
53
        }
54
55
        return $query;
56
    }
57
58
    /*******************************************
59
     * CREATE
60
     *******************************************/
61
62
    /**
63
     * @param array $attributes
64
     * @param string $toScenario
65
     * @return Record
66
     */
67
    public function createRecord(array $attributes = [], string $toScenario = null)
68
    {
69
        /** @var string $recordClass */
70
        $recordClass = static::recordClass();
71
72
        /** @var Record $record */
73
        $record = new $recordClass();
74
75
        // Set scenario
76
        if ($toScenario) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toScenario of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
77
            $record->setScenario($toScenario);
78
        }
79
80
        // Do we need to set properties too
81
        if (!empty($attributes)) {
82
            $record->setAttributes($attributes);
83
        }
84
85
        return $record;
86
    }
87
88
    /**
89
     * @param $condition
90
     * @param string $toScenario
91
     * @return Record|null
92
     */
93
    public function findRecordByCondition($condition, string $toScenario = null)
94
    {
95
        if (empty($condition)) {
96
            return null;
97
        }
98
99
        return $this->findRecordByCriteria(
100
            RecordHelper::conditionToCriteria($condition),
101
            $toScenario
102
        );
103
    }
104
105
    /**
106
     * @param $criteria
107
     * @param string $toScenario
108
     * @return Record
109
     */
110
    public function findRecordByCriteria($criteria, string $toScenario = null)
111
    {
112
        $query = $this->getRecordQuery($criteria);
113
114
        /** @var Record $record */
115
        if ($record = $query->one()) {
116
            // Set scenario
117
            if ($toScenario) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toScenario of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
118
                $record->setScenario($toScenario);
119
            }
120
        }
121
122
        return $record;
123
    }
124
125
    /**
126
     * @param $condition
127
     * @param string $toScenario
128
     * @return Record
129
     * @throws RecordNotFoundException
130
     */
131
    public function getRecordByCondition($condition, string $toScenario = null)
132
    {
133
        if (!$record = $this->findRecordByCondition($condition, $toScenario)) {
134
            $this->notFoundRecordException();
135
        }
136
137
        return $record;
138
    }
139
140
    /**
141
     * @param $criteria
142
     * @param string $toScenario
143
     * @return Record
144
     * @throws RecordNotFoundException
145
     */
146
    public function getRecordByCriteria($criteria, string $toScenario = null)
147
    {
148
        if (!$record = $this->findRecordByCriteria($criteria, $toScenario)) {
149
            $this->notFoundRecordException();
150
        }
151
152
        return $record;
153
    }
154
155
156
    /**
157
     * @param string $toScenario
158
     * @return Record[]
159
     */
160
    public function findAllRecords(string $toScenario = null)
161
    {
162
        return $this->findAllRecordsByCondition(null, $toScenario);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
    }
164
165
    /**
166
     * @param array $condition
167
     * @param string $toScenario
168
     * @return Record[]
169
     */
170
    public function findAllRecordsByCondition($condition = [], string $toScenario = null)
171
    {
172
        return $this->findAllRecordsByCriteria(
173
            RecordHelper::conditionToCriteria($condition),
174
            $toScenario
175
        );
176
    }
177
178
    /**
179
     * @param array $criteria
180
     * @param string $toScenario
181
     * @return Record[]
182
     */
183
    public function findAllRecordsByCriteria($criteria = [], string $toScenario = null)
184
    {
185
        $query = $this->getRecordQuery($criteria);
186
187
        /** @var Record[] $record s */
188
        $records = $query->all();
189
190
        // Set scenario
191
        if ($toScenario) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toScenario of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
192
193
            /** @var Record $record */
194
            foreach ($records as $record) {
195
                // Set scenario
196
                $record->setScenario($toScenario);
197
            }
198
        }
199
200
        return $records;
201
    }
202
203
204
    /**
205
     * @deprecated
206
     * @param array $condition
207
     * @param string $toScenario
208
     * @return Record[]
209
     * @throws RecordNotFoundException
210
     */
211
    public function getAllRecords($condition = [], string $toScenario = null)
212
    {
213
        Craft::$app->getDeprecator()->log(
214
            __METHOD__,
215
            'Use the "getAllRecordsByCondition" method'
216
        );
217
218
        return $this->getAllRecordsByCondition($condition, $toScenario);
219
    }
220
221
    /**
222
     * @param array $condition
223
     * @param string $toScenario
224
     * @return Record[]
225
     * @throws RecordNotFoundException
226
     */
227
    public function getAllRecordsByCondition($condition = [], string $toScenario = null)
228
    {
229
        if (!$records = $this->findAllRecordsByCondition($condition, $toScenario)) {
230
            $this->notFoundRecordException();
231
        }
232
233
        return $records;
234
    }
235
236
    /**
237
     * @param array $criteria
238
     * @param string $toScenario
239
     * @return Record[]
240
     * @throws RecordNotFoundException
241
     */
242
    public function getAllRecordsByCriteria($criteria = [], string $toScenario = null)
243
    {
244
        if (!$records = $this->findAllRecordsByCriteria($criteria, $toScenario)) {
245
            $this->notFoundRecordException();
246
        }
247
248
        return $records;
249
    }
250
251
    /*******************************************
252
     * EXCEPTIONS
253
     *******************************************/
254
255
    /**
256
     * @throws RecordNotFoundException
257
     */
258
    protected function notFoundRecordException()
259
    {
260
        throw new RecordNotFoundException(
261
            sprintf(
262
                "Record does not exist."
263
            )
264
        );
265
    }
266
}
267