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\traits; |
10
|
|
|
|
11
|
|
|
use flipbox\spark\exceptions\ObjectNotFoundException; |
12
|
|
|
use flipbox\spark\exceptions\RecordNotFoundException; |
13
|
|
|
use flipbox\spark\objects\ObjectWithId; |
14
|
|
|
use flipbox\spark\records\Record; |
15
|
|
|
use flipbox\spark\records\RecordWithId; |
16
|
|
|
use yii\base\Object as BaseObject; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 2.0.0 |
21
|
|
|
* |
22
|
|
|
* @method RecordWithId|null findRecordByCondition($condition, string $toScenario = null) |
23
|
|
|
*/ |
24
|
|
|
trait ObjectById |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
use Object; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var BaseObject[] |
31
|
|
|
*/ |
32
|
|
|
protected $_cacheById = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Record $record |
36
|
|
|
* @param string|null $toScenario |
37
|
|
|
* @return BaseObject |
38
|
|
|
*/ |
39
|
|
|
abstract protected function findByRecord(Record $record, string $toScenario = null): BaseObject; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/******************************************* |
43
|
|
|
* FIND/GET BY ID |
44
|
|
|
*******************************************/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int $id |
48
|
|
|
* @param string|null $toScenario |
49
|
|
|
* @return BaseObject|null |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function findById(int $id, string $toScenario = null) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
// Check cache |
55
|
|
|
if (!$object = $this->findCacheById($id)) { |
56
|
|
|
|
57
|
|
|
// Find record in db |
58
|
|
|
if ($record = $this->findRecordByCondition( |
59
|
|
|
['id' => $id] |
60
|
|
|
) |
61
|
|
|
) { |
62
|
|
|
|
63
|
|
|
// Perhaps in cache |
64
|
|
|
$object = $this->findByRecord($record, $toScenario); |
65
|
|
|
|
66
|
|
|
} else { |
67
|
|
|
|
68
|
|
|
$this->_cacheById[$id] = null; |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $object; |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $id |
82
|
|
|
* @param string|null $toScenario |
83
|
|
|
* @return BaseObject |
84
|
|
|
* @throws ObjectNotFoundException |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function getById(int $id, string $toScenario = null): BaseObject |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
// Find by ID |
90
|
|
|
if (!$object = $this->findById($id, $toScenario)) { |
91
|
|
|
|
92
|
|
|
$this->notFoundByIdException($id); |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $object; |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $id |
102
|
|
|
* @param string|null $toScenario |
103
|
|
|
* @return BaseObject|null |
104
|
|
|
*/ |
105
|
|
|
public function freshFindById(int $id, string $toScenario = null) |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
// Find record in db |
109
|
|
|
if (!$record = $this->findRecordById($id)) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->createFromRecord($record, $toScenario); |
|
|
|
|
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int $id |
119
|
|
|
* @param string|null $toScenario |
120
|
|
|
* @return BaseObject |
121
|
|
|
* @throws ObjectNotFoundException |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function freshGetById(int $id, string $toScenario = null): BaseObject |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
|
126
|
|
|
if (!$object = $this->freshFindById($id, $toScenario)) { |
127
|
|
|
|
128
|
|
|
$this->notFoundByIdException($id); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $object; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/******************************************* |
138
|
|
|
* CACHE |
139
|
|
|
*******************************************/ |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Find an existing cache by ID |
143
|
|
|
* |
144
|
|
|
* @param $id |
145
|
|
|
* @return BaseObject|null |
146
|
|
|
*/ |
147
|
|
|
public function findCacheById(int $id) |
148
|
|
|
{ |
149
|
|
|
|
150
|
|
|
// Check if already in addToCache |
151
|
|
|
if ($this->isCachedById($id)) { |
152
|
|
|
|
153
|
|
|
return $this->_cacheById[$id]; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return null; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Identify whether in cache by ID |
163
|
|
|
* |
164
|
|
|
* @param $id |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
protected function isCachedById(int $id) |
168
|
|
|
{ |
169
|
|
|
return array_key_exists($id, $this->_cacheById); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param ObjectWithId $object |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
protected function cacheById(ObjectWithId $object) |
177
|
|
|
{ |
178
|
|
|
|
179
|
|
|
// Check if already in cache |
180
|
|
|
if (!$id = $this->isCachedById($object->id)) { |
181
|
|
|
|
182
|
|
|
// Cache it |
183
|
|
|
$this->_cacheById[$id] = $object; |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/******************************************* |
193
|
|
|
* RECORD BY ID |
194
|
|
|
*******************************************/ |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param int $id |
198
|
|
|
* @param string|null $toScenario |
199
|
|
|
* @return RecordWithId|null |
200
|
|
|
*/ |
201
|
|
|
public function findRecordById(int $id, string $toScenario = null) |
202
|
|
|
{ |
203
|
|
|
|
204
|
|
|
return $this->findRecordByCondition( |
205
|
|
|
[ |
206
|
|
|
'id' => $id |
207
|
|
|
], |
208
|
|
|
$toScenario |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param int $id |
215
|
|
|
* @param string|null $toScenario |
216
|
|
|
* @throws RecordNotFoundException |
217
|
|
|
* @return RecordWithId|null |
218
|
|
|
*/ |
219
|
|
|
public function getRecordById(int $id, string $toScenario = null) |
220
|
|
|
{ |
221
|
|
|
|
222
|
|
|
if (!$record = $this->findRecordById($id, $toScenario)) { |
223
|
|
|
|
224
|
|
|
$this->notFoundRecordByIdException($id); |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $record; |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/******************************************* |
234
|
|
|
* EXCEPTIONS |
235
|
|
|
*******************************************/ |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param int|null $id |
239
|
|
|
* @throws ObjectNotFoundException |
240
|
|
|
*/ |
241
|
|
|
protected function notFoundByIdException(int $id = null) |
242
|
|
|
{ |
243
|
|
|
|
244
|
|
|
throw new ObjectNotFoundException( |
245
|
|
|
sprintf( |
246
|
|
|
'Object does not exist with the id "%s".', |
247
|
|
|
(string)$id |
248
|
|
|
) |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param int|null $id |
255
|
|
|
* @throws RecordNotFoundException |
256
|
|
|
*/ |
257
|
|
|
protected function notFoundRecordByIdException(int $id = null) |
258
|
|
|
{ |
259
|
|
|
|
260
|
|
|
throw new RecordNotFoundException( |
261
|
|
|
sprintf( |
262
|
|
|
'Record does not exist with the id "%s".', |
263
|
|
|
(string)$id |
264
|
|
|
) |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
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.