ObjectByIdOrString::find()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 12
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;
10
11
use flipbox\spark\Records\Record;
12
use yii\base\Object as BaseObject;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.1.0
17
 */
18
abstract class ObjectByIdOrString extends ObjectById
19
{
20
21
    use traits\ObjectByString;
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function find($identifier, string $toScenario = null)
27
    {
28
29
        if ($model = parent::find($identifier, $toScenario)) {
30
            return $model;
31
        }
32
33
        if (!is_string($identifier)) {
34
            return null;
35
        }
36
37
        return $this->findByString($identifier, $toScenario);
38
    }
39
40
    /*******************************************
41
     * CACHE
42
     *******************************************/
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function findCache($identifier)
48
    {
49
50
        if ($model = parent::findCache($identifier)) {
51
            return $model;
52
        }
53
54
        if (!is_string($identifier)) {
55
            return null;
56
        }
57
58
        return $this->findCacheByString($identifier);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function addToCache(BaseObject $object)
65
    {
66
67
        parent::addToCache($object);
68
69
        $this->cacheByString($object);
70
71
        return $this;
72
    }
73
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function findCacheByRecord(Record $record)
79
    {
80
81
        if ($model = parent::findCacheByRecord($record)) {
82
            return $model;
83
        }
84
85
        return $this->findCacheByRecordByString($record);
86
    }
87
}
88