Passed
Push — master ( f2dea1...58a092 )
by Nate
04:47
created

ModelByString::stringValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 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\ModelNotFoundException;
12
use flipbox\spark\exceptions\RecordNotFoundException;
13
use flipbox\spark\models\Model as BaseModel;
14
use flipbox\spark\records\Record;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.2.0
19
 */
20
trait ModelByString
21
{
22
23
    use Model;
24
25
    /**
26
     * @var BaseModel[]
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 BaseModel
39
     */
40
    abstract protected function findByRecord(Record $record, string $toScenario = null): BaseModel;
41
42
    /**
43
     * @param array $config
44
     * @param string|null $toScenario
45
     * @return BaseModel
46
     */
47
    abstract public function create($config = [], string $toScenario = null): BaseModel;
48
49
    /**
50
     * @return string
51
     */
52
    protected function recordStringProperty(): string
53
    {
54
        return $this->stringProperty();
55
    }
56
57
    /**
58
     * @param BaseModel $model
59
     * @return string
60
     */
61
    protected function stringValue(BaseModel $model): string
62
    {
63
64
        $property = $this->stringProperty();
65
66
        return $model->{$property};
67
68
    }
69
70
    /*******************************************
71
     * FIND/GET BY STRING
72
     *******************************************/
73
74
    /**
75
     * @param string $string
76
     * @param string|null $toScenario
77
     * @return BaseModel|null
78
     */
79
    public function findByString(string $string, string $toScenario = null)
80
    {
81
82
        // Check cache
83
        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...
84
85
            // Find record in db
86
            if ($record = $this->findRecordByString($string)) {
87
88
                $model = $this->findByRecord($record, $toScenario);
89
90
            } else {
91
92
                $this->_cacheByString[$string] = null;
93
94
                return null;
95
96
            }
97
98
        }
99
100
        return $model;
101
102
    }
103
104
    /**
105
     * @param string $string
106
     * @param string|null $toScenario
107
     * @return BaseModel|null
108
     * @throws ModelNotFoundException
109
     */
110
    public function getByString(string $string, string $toScenario = null): BaseModel
111
    {
112
113
        if (!$model = $this->findByString($string, $toScenario)) {
114
115
            $this->notFoundByStringException($string);
116
117
        }
118
119
        return $model;
120
121
    }
122
123
    /**
124
     * @param string $string
125
     * @param string|null $toScenario
126
     * @return BaseModel|null
127
     */
128 View Code Duplication
    public function freshFindByString(string $string, string $toScenario = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
131
        // Find record in db
132
        if (!$record = $this->findRecordByString($string)) {
133
            return null;
134
        }
135
136
        $model = $this->create($record, $toScenario);
0 ignored issues
show
Documentation introduced by
$record is of type object<flipbox\spark\records\Record>, 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...
137
138
        return $model;
139
140
    }
141
142
    /**
143
     * @param string $string
144
     * @param string|null $toScenario
145
     * @return BaseModel
146
     * @throws ModelNotFoundException
147
     */
148 View Code Duplication
    public function freshGetByString(string $string, string $toScenario = null): BaseModel
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
151
        if (!$model = $this->freshFindByString($string, $toScenario)) {
152
153
            $this->notFoundByStringException($string);
154
155
        }
156
157
        return $model;
158
159
    }
160
161
    /*******************************************
162
     * CACHE
163
     *******************************************/
164
165
    /**
166
     * Find an existing cache by string
167
     *
168
     * @param string $string
169
     * @return null
170
     */
171
    public function findCacheByString(string $string)
172
    {
173
174
        // Check if already in cache
175
        if (!$this->isCachedByString($string)) {
176
            return null;
177
        }
178
179
        return $this->_cacheByString[$string];
180
181
    }
182
183
    /**
184
     * Identify whether in cache by string
185
     *
186
     * @param string $string
187
     * @return bool
188
     */
189
    private function isCachedByString(string $string): bool
190
    {
191
        return array_key_exists($string, $this->_cacheByString);
192
    }
193
194
195
    /**
196
     * @param BaseModel $model
197
     * @return static
198
     */
199 View Code Duplication
    protected function cacheByString(BaseModel $model)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201
202
        $stringValue = $this->stringValue($model);
203
204
        if (null === $stringValue) {
205
            return $this;
206
        }
207
208
        // Check if already in cache
209
        if (!$this->isCachedByString($stringValue)) {
210
211
            // Cache it
212
            $this->_cacheByString[$stringValue] = $model;
213
214
        }
215
216
        return $this;
217
218
    }
219
220
    /**
221
     * @param Record $record
222
     * @return BaseModel|null
223
     */
224 View Code Duplication
    protected function findCacheByRecordByString(Record $record)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
    {
226
227
        $property = $this->recordStringProperty();
228
229
        $stringValue = $record->{$property};
230
231
        if ($stringValue === null) {
232
            return null;
233
        }
234
235
        return $this->findCacheByString($stringValue);
236
237
    }
238
239
    /*******************************************
240
     * RECORD BY STRING
241
     *******************************************/
242
243
    /**
244
     * @param string $string
245
     * @param string|null $toScenario
246
     * @return Record|null
247
     */
248
    public function findRecordByString(string $string, string $toScenario = null)
249
    {
250
251
        return $this->findRecordByCondition(
252
            [
253
                $this->recordStringProperty() => $string
254
            ],
255
            $toScenario
256
        );
257
258
    }
259
260
    /**
261
     * @param string $string
262
     * @param string|null $toScenario
263
     * @throws RecordNotFoundException
264
     * @return Record|null
265
     */
266
    public function getRecordByString(string $string, string $toScenario = null)
267
    {
268
269
        if (!$record = $this->findRecordByString($string, $toScenario)) {
270
271
            $this->notFoundRecordByStringException($string);
272
273
        }
274
275
        return $record;
276
277
    }
278
279
280
    /**
281
     * @param BaseModel $model
282
     * @param bool $mirrorScenario
283
     * @return Record
284
     */
285 View Code Duplication
    protected function toRecordByString(BaseModel $model, bool $mirrorScenario = true): Record
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
    {
287
288
        // Get record
289
        if (!$record = $this->findRecordByString($this->stringValue($model))) {
290
            return null;
291
        }
292
293
        // Populate the record attributes
294
        $this->transferToRecord($model, $record, $mirrorScenario);
295
296
        return $record;
297
298
    }
299
300
    /*******************************************
301
     * EXCEPTIONS
302
     *******************************************/
303
304
    /**
305
     * @param string|null $string
306
     * @throws ModelNotFoundException
307
     */
308
    protected function notFoundByStringException(string $string = null)
309
    {
310
311
        throw new ModelNotFoundException(
312
            sprintf(
313
                'Model does not exist with the string "%s".',
314
                (string)$string
315
            )
316
        );
317
318
    }
319
320
    /**
321
     * @param string|null $string
322
     * @throws RecordNotFoundException
323
     */
324
    protected function notFoundRecordByStringException(string $string = null)
325
    {
326
327
        throw new RecordNotFoundException(
328
            sprintf(
329
                'Record does not exist with the string "%s".',
330
                (string)$string
331
            )
332
        );
333
334
    }
335
336
}
337