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
|
|
|
/******************************************* |
49
|
|
|
* CACHE |
50
|
|
|
*******************************************/ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function findCache($identifier) |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
if ($model = parent::findCache($identifier)) { |
59
|
|
|
return $model; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (empty($identifier) || !is_numeric($identifier)) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->findCacheById($identifier); |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function findCacheByRecord(Record $record) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
if ($model = parent::findCacheByRecord($record)) { |
77
|
|
|
return $model; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$record instanceof RecordWithId || null === $record->id) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Check if already in cache by id |
85
|
|
|
return $this->findCacheById($record->id); |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function addToCache(BaseModel $model) |
93
|
|
|
{ |
94
|
|
|
|
95
|
|
|
parent::addToCache($model); |
96
|
|
|
|
97
|
|
|
if ($model instanceof ModelWithId) { |
98
|
|
|
$this->cacheById($model); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/******************************************* |
106
|
|
|
* RECORD |
107
|
|
|
*******************************************/ |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param BaseModel $model |
111
|
|
|
* @return Record|null |
112
|
|
|
*/ |
113
|
|
|
public function findRecordByModel(BaseModel $model) |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
if ($model instanceof ModelWithId && null !== $model->id) { |
117
|
|
|
return $this->findRecordById($model->id); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return parent::findRecordByModel($model); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.