ModelById   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 4
dl 0
loc 99
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 13 3
A findCache() 0 13 4
A findCacheByRecord() 0 14 4
A addToCache() 0 11 2
A findRecordByModel() 0 9 3
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\models\ModelWithId;
13
use flipbox\spark\records\Record;
14
use flipbox\spark\records\RecordWithId;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
abstract class ModelById extends Model
21
{
22
23
    use traits\ModelById;
24
25
    /*******************************************
26
     * FIND/GET
27
     *******************************************/
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function find($identifier, string $toScenario = null)
33
    {
34
35
        if ($model = parent::find($identifier, $toScenario)) {
36
            return $model;
37
        }
38
39
        if (!is_numeric($identifier)) {
40
            return null;
41
        }
42
43
        return $this->findById($identifier, $toScenario);
44
    }
45
46
47
    /*******************************************
48
     * CACHE
49
     *******************************************/
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function findCache($identifier)
55
    {
56
57
        if ($model = parent::findCache($identifier)) {
58
            return $model;
59
        }
60
61
        if (empty($identifier) || !is_numeric($identifier)) {
62
            return null;
63
        }
64
65
        return $this->findCacheById($identifier);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function findCacheByRecord(Record $record)
72
    {
73
74
        if ($model = parent::findCacheByRecord($record)) {
75
            return $model;
76
        }
77
78
        if (!$record instanceof RecordWithId || null === $record->id) {
79
            return null;
80
        }
81
82
        // Check if already in cache by id
83
        return $this->findCacheById($record->id);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function addToCache(BaseModel $model)
90
    {
91
92
        parent::addToCache($model);
93
94
        if ($model instanceof ModelWithId) {
95
            $this->cacheById($model);
96
        }
97
98
        return $this;
99
    }
100
101
    /*******************************************
102
     * RECORD
103
     *******************************************/
104
105
    /**
106
     * @param BaseModel $model
107
     * @return Record|null
108
     */
109
    public function findRecordByModel(BaseModel $model)
110
    {
111
112
        if ($model instanceof ModelWithId && null !== $model->id) {
113
            return $this->findRecordById($model->id);
114
        }
115
116
        return parent::findRecordByModel($model);
117
    }
118
}
119