ObjectByIdOrString   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 70
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 13 3
A findCache() 0 13 3
A addToCache() 0 9 1
A findCacheByRecord() 0 9 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;
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