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 1.2.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
|
|
View Code Duplication |
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
|
|
|
/******************************************* |
48
|
|
|
* CACHE |
49
|
|
|
*******************************************/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function findCache($identifier) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
if ($object = parent::findCache($identifier)) { |
58
|
|
|
return $object; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!is_numeric($identifier)) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->findCacheById($identifier); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function findCacheByRecord(Record $record) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
if ($object = parent::findCacheByRecord($record)) { |
76
|
|
|
return $object; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!$record instanceof RecordWithId) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Check if already in cache by id |
84
|
|
|
return $this->findCacheById($record->id); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function addToCache(BaseObject $object) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
parent::addToCache($object); |
95
|
|
|
|
96
|
|
|
if ($object instanceof ObjectWithId) { |
97
|
|
|
$this->cacheById($object); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
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.