ObjectById::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\objects\ObjectWithId;
12
use flipbox\spark\Records\Record;
13
use flipbox\spark\records\RecordWithId;
14
use yii\base\Object as BaseObject;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
abstract class ObjectById extends Object
21
{
22
23
    use traits\ObjectById;
24
25
    /*******************************************
26
     * FIND/GET
27
     *******************************************/
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function find($identifier, string $toScenario = null)
33
    {
34
35
        if ($object = parent::find($identifier, $toScenario)) {
36
            return $object;
37
        }
38
39
        if (!is_numeric($identifier)) {
40
            return null;
41
        }
42
43
        return $this->findById($identifier, $toScenario);
44
    }
45
46
    /*******************************************
47
     * CACHE
48
     *******************************************/
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function findCache($identifier)
54
    {
55
56
        if ($object = parent::findCache($identifier)) {
57
            return $object;
58
        }
59
60
        if (!is_numeric($identifier)) {
61
            return null;
62
        }
63
64
        return $this->findCacheById($identifier);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function findCacheByRecord(Record $record)
71
    {
72
73
        if ($object = parent::findCacheByRecord($record)) {
74
            return $object;
75
        }
76
77
        if (!$record instanceof RecordWithId) {
78
            return null;
79
        }
80
81
        // Check if already in cache by id
82
        return $this->findCacheById($record->id);
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function addToCache(BaseObject $object)
89
    {
90
91
        parent::addToCache($object);
92
93
        if ($object instanceof ObjectWithId) {
94
            $this->cacheById($object);
95
        }
96
97
        return $this;
98
    }
99
}
100