ObjectByString   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 260
ccs 0
cts 107
cp 0
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
stringProperty() 0 1 ?
findByRecord() 0 1 ?
A recordStringProperty() 0 4 1
A stringValue() 0 7 1
A findByString() 0 17 3
A getByString() 0 9 2
A freshFindByString() 0 10 2
A freshGetByString() 0 9 2
A findCacheByString() 0 10 2
A isCachedByString() 0 4 1
A cacheByString() 0 17 3
A findCacheByRecordByString() 0 13 2
A findRecordByString() 0 10 1
A getRecordByString() 0 9 2
A notFoundByStringException() 0 10 1
A notFoundRecordByStringException() 0 10 1
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 flipbox\spark\exceptions\ObjectNotFoundException;
12
use flipbox\spark\exceptions\RecordNotFoundException;
13
use flipbox\spark\records\Record;
14
use yii\base\Object as BaseObject;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait ObjectByString
21
{
22
23
    use Object;
24
25
    /**
26
     * @var BaseObject[]
27
     */
28
    protected $cacheByString = [];
29
30
    /**
31
     * @return string
32
     */
33
    abstract protected function stringProperty(): string;
34
35
    /**
36
     * @param Record $record
37
     * @param string|null $toScenario
38
     * @return BaseObject
39
     */
40
    abstract protected function findByRecord(Record $record, string $toScenario = null): BaseObject;
41
42
    /**
43
     * @return string
44
     */
45
    protected function recordStringProperty(): string
46
    {
47
        return $this->stringProperty();
48
    }
49
50
    /**
51
     * @param BaseObject $object
52
     * @return string
53
     */
54
    protected function stringValue(BaseObject $object)
55
    {
56
57
        $property = $this->stringProperty();
58
59
        return $object->{$property};
60
    }
61
62
    /*******************************************
63
     * FIND/GET BY STRING
64
     *******************************************/
65
66
    /**
67
     * @param string $string
68
     * @param string|null $toScenario
69
     * @return BaseObject|null
70
     */
71
    public function findByString(string $string, string $toScenario = null)
72
    {
73
74
        // Check cache
75
        if (!$model = $this->findCacheByString($string)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as $this->findCacheByString($string) (which targets flipbox\spark\services\t...ng::findCacheByString()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
            // Find record in db
77
            if ($record = $this->findRecordByString($string)) {
78
                $model = $this->findByRecord($record, $toScenario);
79
            } else {
80
                $this->cacheByString[$string] = null;
81
82
                return null;
83
            }
84
        }
85
86
        return $model;
87
    }
88
89
    /**
90
     * @param string $string
91
     * @param string|null $toScenario
92
     * @return BaseObject|null
93
     * @throws ObjectNotFoundException
94
     */
95
    public function getByString(string $string, string $toScenario = null): BaseObject
96
    {
97
98
        if (!$model = $this->findByString($string, $toScenario)) {
99
            $this->notFoundByStringException($string);
100
        }
101
102
        return $model;
103
    }
104
105
    /**
106
     * @param string $string
107
     * @param string|null $toScenario
108
     * @return BaseObject|null
109
     */
110
    public function freshFindByString(string $string, string $toScenario = null)
111
    {
112
113
        // Find record in db
114
        if (!$record = $this->findRecordByString($string)) {
115
            return null;
116
        }
117
118
        return $this->createFromRecord($record, $toScenario);
0 ignored issues
show
Bug introduced by
It seems like createFromRecord() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
    }
120
121
    /**
122
     * @param string $string
123
     * @param string|null $toScenario
124
     * @return BaseObject
125
     * @throws ObjectNotFoundException
126
     */
127
    public function freshGetByString(string $string, string $toScenario = null): BaseObject
128
    {
129
130
        if (!$model = $this->freshFindByString($string, $toScenario)) {
131
            $this->notFoundByStringException($string);
132
        }
133
134
        return $model;
135
    }
136
137
    /*******************************************
138
     * CACHE
139
     *******************************************/
140
141
    /**
142
     * Find an existing cache by string
143
     *
144
     * @param string $string
145
     * @return null
146
     */
147
    public function findCacheByString(string $string)
148
    {
149
150
        // Check if already in cache
151
        if (!$this->isCachedByString($string)) {
152
            return null;
153
        }
154
155
        return $this->cacheByString[$string];
156
    }
157
158
    /**
159
     * Identify whether in cache by string
160
     *
161
     * @param string $string
162
     * @return bool
163
     */
164
    private function isCachedByString(string $string): bool
165
    {
166
        return array_key_exists($string, $this->cacheByString);
167
    }
168
169
170
    /**
171
     * @param BaseObject $model
172
     * @return static
173
     */
174
    protected function cacheByString(BaseObject $model)
175
    {
176
177
        $stringValue = $this->stringValue($model);
178
179
        if (null === $stringValue) {
180
            return $this;
181
        }
182
183
        // Check if already in cache
184
        if (!$this->isCachedByString($stringValue)) {
185
            // Cache it
186
            $this->cacheByString[$stringValue] = $model;
187
        }
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param Record $record
194
     * @return BaseObject|null
195
     */
196
    protected function findCacheByRecordByString(Record $record)
197
    {
198
199
        $property = $this->recordStringProperty();
200
201
        $stringValue = $record->{$property};
202
203
        if ($stringValue === null) {
204
            return null;
205
        }
206
207
        return $this->findCacheByString($stringValue);
208
    }
209
210
    /*******************************************
211
     * RECORD BY STRING
212
     *******************************************/
213
214
    /**
215
     * @param string $string
216
     * @param string|null $toScenario
217
     * @return Record|null
218
     */
219
    public function findRecordByString(string $string, string $toScenario = null)
220
    {
221
222
        return $this->findRecordByCondition(
223
            [
224
                $this->recordStringProperty() => $string
225
            ],
226
            $toScenario
227
        );
228
    }
229
230
    /**
231
     * @param string $string
232
     * @param string|null $toScenario
233
     * @throws RecordNotFoundException
234
     * @return Record|null
235
     */
236
    public function getRecordByString(string $string, string $toScenario = null)
237
    {
238
239
        if (!$record = $this->findRecordByString($string, $toScenario)) {
240
            $this->notFoundRecordByStringException($string);
241
        }
242
243
        return $record;
244
    }
245
246
    /*******************************************
247
     * EXCEPTIONS
248
     *******************************************/
249
250
    /**
251
     * @param string|null $string
252
     * @throws ObjectNotFoundException
253
     */
254
    protected function notFoundByStringException(string $string = null)
255
    {
256
257
        throw new ObjectNotFoundException(
258
            sprintf(
259
                'Object does not exist with the string "%s".',
260
                (string)$string
261
            )
262
        );
263
    }
264
265
    /**
266
     * @param string|null $string
267
     * @throws RecordNotFoundException
268
     */
269
    protected function notFoundRecordByStringException(string $string = null)
270
    {
271
272
        throw new RecordNotFoundException(
273
            sprintf(
274
                'Record does not exist with the string "%s".',
275
                (string)$string
276
            )
277
        );
278
    }
279
}
280