Object::findAllRecords()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use Craft;
12
use flipbox\spark\exceptions\RecordNotFoundException;
13
use flipbox\spark\helpers\QueryHelper;
14
use flipbox\spark\helpers\RecordHelper;
15
use flipbox\spark\records\Record;
16
use yii\db\ActiveQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.1.0
21
 */
22
trait Object
23
{
24
25
    /**
26
     * @return string
27
     */
28
    public abstract static function recordClass(): string;
29
30
    /**
31
     * @return string
32
     */
33
    public static function recordClassInstance(): string
34
    {
35
        return Record::class;
36
    }
37
38
    /**
39
     * @param array $config
40
     * @return \yii\db\ActiveQuery
41
     */
42
    public function getRecordQuery($config = []): ActiveQuery
43
    {
44
45
        /** @var Record $recordClass */
46
        $recordClass = $this->recordClass();
47
48
        $query = $recordClass::find();
49
50
        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...
51
            QueryHelper::configure(
52
                $query,
53
                $config
54
            );
55
        }
56
57
        return $query;
58
    }
59
60
    /*******************************************
61
     * CREATE
62
     *******************************************/
63
64
    /**
65
     * @param array $attributes
66
     * @param string $toScenario
67
     * @return Record
68
     */
69
    public function createRecord(array $attributes = [], string $toScenario = null)
70
    {
71
72
        /** @var string $recordClass */
73
        $recordClass = static::recordClass();
74
75
        /** @var Record $record */
76
        $record = new $recordClass();
77
78
        // Set scenario
79
        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...
80
            $record->setScenario($toScenario);
81
        }
82
83
        // Do we need to set properties too
84
        if (!empty($attributes)) {
85
            $record->setAttributes($attributes);
86
        }
87
88
        return $record;
89
    }
90
91
    /**
92
     * @param $condition
93
     * @param string $toScenario
94
     * @return Record|null
95
     */
96
    public function findRecordByCondition($condition, string $toScenario = null)
97
    {
98
99
        if (empty($condition)) {
100
            return null;
101
        }
102
103
        return $this->findRecordByCriteria(
104
            RecordHelper::conditionToCriteria($condition),
105
            $toScenario
106
        );
107
    }
108
109
    /**
110
     * @param $criteria
111
     * @param string $toScenario
112
     * @return Record
113
     */
114
    public function findRecordByCriteria($criteria, string $toScenario = null)
115
    {
116
117
        $query = $this->getRecordQuery($criteria);
118
119
        /** @var Record $record */
120
        if ($record = $query->one()) {
121
            // Set scenario
122
            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...
123
                $record->setScenario($toScenario);
124
            }
125
        }
126
127
        return $record;
128
    }
129
130
    /**
131
     * @param $condition
132
     * @param string $toScenario
133
     * @return Record
134
     * @throws RecordNotFoundException
135
     */
136
    public function getRecordByCondition($condition, string $toScenario = null)
137
    {
138
139
        if (!$record = $this->findRecordByCondition($condition, $toScenario)) {
140
            $this->notFoundRecordException();
141
        }
142
143
        return $record;
144
    }
145
146
    /**
147
     * @param $criteria
148
     * @param string $toScenario
149
     * @return Record
150
     * @throws RecordNotFoundException
151
     */
152
    public function getRecordByCriteria($criteria, string $toScenario = null)
153
    {
154
155
        if (!$record = $this->findRecordByCriteria($criteria, $toScenario)) {
156
            $this->notFoundRecordException();
157
        }
158
159
        return $record;
160
    }
161
162
163
    /**
164
     * @param string $toScenario
165
     * @return Record[]
166
     */
167
    public function findAllRecords(string $toScenario = null)
168
    {
169
        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...
170
    }
171
172
    /**
173
     * @param array $condition
174
     * @param string $toScenario
175
     * @return Record[]
176
     */
177
    public function findAllRecordsByCondition($condition = [], string $toScenario = null)
178
    {
179
180
        return $this->findAllRecordsByCriteria(
181
            RecordHelper::conditionToCriteria($condition),
182
            $toScenario
183
        );
184
    }
185
186
    /**
187
     * @param array $criteria
188
     * @param string $toScenario
189
     * @return Record[]
190
     */
191
    public function findAllRecordsByCriteria($criteria = [], string $toScenario = null)
192
    {
193
194
        $query = $this->getRecordQuery($criteria);
195
196
        /** @var Record[] $record s */
197
        $records = $query->all();
198
199
        // Set scenario
200
        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...
201
202
            /** @var Record $record */
203
            foreach ($records as $record) {
204
                // Set scenario
205
                $record->setScenario($toScenario);
206
            }
207
        }
208
209
        return $records;
210
    }
211
212
213
    /**
214
     * @deprecated
215
     * @param array $condition
216
     * @param string $toScenario
217
     * @return Record[]
218
     * @throws RecordNotFoundException
219
     */
220
    public function getAllRecords($condition = [], string $toScenario = null)
221
    {
222
223
        Craft::$app->getDeprecator()->log(
224
            __METHOD__,
225
            'Use the "getAllRecordsByCondition" method'
226
        );
227
228
        return $this->getAllRecordsByCondition($condition, $toScenario);
229
    }
230
231
    /**
232
     * @param array $condition
233
     * @param string $toScenario
234
     * @return Record[]
235
     * @throws RecordNotFoundException
236
     */
237
    public function getAllRecordsByCondition($condition = [], string $toScenario = null)
238
    {
239
240
        if (!$records = $this->findAllRecordsByCondition($condition, $toScenario)) {
241
            $this->notFoundRecordException();
242
        }
243
244
        return $records;
245
    }
246
247
    /**
248
     * @param array $criteria
249
     * @param string $toScenario
250
     * @return Record[]
251
     * @throws RecordNotFoundException
252
     */
253
    public function getAllRecordsByCriteria($criteria = [], string $toScenario = null)
254
    {
255
256
        if (!$records = $this->findAllRecordsByCriteria($criteria, $toScenario)) {
257
            $this->notFoundRecordException();
258
        }
259
260
        return $records;
261
    }
262
263
    /*******************************************
264
     * EXCEPTIONS
265
     *******************************************/
266
267
    /**
268
     * @throws RecordNotFoundException
269
     */
270
    protected function notFoundRecordException()
271
    {
272
273
        throw new RecordNotFoundException(
274
            sprintf(
275
                "Record does not exist."
276
            )
277
        );
278
    }
279
}
280