ModelByIdOrString::addToCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 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\models\Model as BaseModel;
12
use flipbox\spark\records\Record;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.1.0
17
 */
18
abstract class ModelByIdOrString extends ModelById
19
{
20
21
    use traits\ModelByString;
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(BaseModel $model)
65
    {
66
67
        parent::addToCache($model);
68
69
        $this->cacheByString($model);
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
    /*******************************************
89
     * RECORD
90
     *******************************************/
91
92
    /**
93
     * @param BaseModel $model
94
     * @return Record|null
95
     */
96
    public function findRecordByModel(BaseModel $model)
97
    {
98
99
        if ($record = parent::findRecordByModel($model)) {
100
            return $record;
101
        }
102
103
        $stringValue = $this->stringValue($model);
104
105
        if ($stringValue !== null) {
106
            return null;
107
        }
108
109
        return $this->findRecordByString($stringValue);
110
    }
111
}
112