Total Complexity | 54 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DataBag often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataBag, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | final class DataBag |
||
17 | { |
||
18 | /** |
||
19 | * The bag! |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $data; |
||
24 | |||
25 | /** |
||
26 | * Original data hash for isDirty check |
||
27 | * |
||
28 | * @var string[] |
||
29 | */ |
||
30 | private $hashes; |
||
31 | |||
32 | /** |
||
33 | * If we have multiple identical get calls in the same request use the cached result |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $cache = []; |
||
38 | |||
39 | /** |
||
40 | * DataBag constructor. |
||
41 | */ |
||
42 | private function __construct() |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return DataBag |
||
48 | */ |
||
49 | public static function create() |
||
50 | { |
||
51 | return new self(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Static constructor |
||
56 | * |
||
57 | * @param string $entityType |
||
58 | * @param array $data |
||
59 | * |
||
60 | * @return DataBag |
||
61 | */ |
||
62 | public static function fromEntityData($entityType, array $data) |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Add additional entities |
||
71 | * |
||
72 | * @param string $entityType |
||
73 | * @param array $data |
||
74 | * |
||
75 | * @return DataBag |
||
76 | */ |
||
77 | public function addEntityData($entityType, array $data) |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Fetch a value from the databag. |
||
86 | * |
||
87 | * $path can be: |
||
88 | * - person.firstName direct property |
||
89 | * - person.emailAddresses.0 indexed by numeric position |
||
90 | * - person.addresses.visit indexed by 'type' property |
||
91 | * - person.addresses.visit.street indexed by 'type' property + get specific property |
||
92 | * |
||
93 | * @param string $path path to the target |
||
94 | * @param mixed $default return value if there's no data |
||
95 | * |
||
96 | * @return mixed |
||
97 | * @throws InvalidDataBagPathException |
||
98 | */ |
||
99 | private function getByPath($path, $default = null) |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Fetch a cached value from the databag. |
||
145 | * |
||
146 | * $path can be: |
||
147 | * - person.firstName direct property |
||
148 | * - person.emailAddresses.0 indexed by numeric position |
||
149 | * - person.addresses.visit indexed by 'type' property |
||
150 | * - person.addresses.visit.street indexed by 'type' property + get specific property |
||
151 | * |
||
152 | * @param string $path path to the target |
||
153 | * @param mixed $default return value if there's no data |
||
154 | * |
||
155 | * @return mixed|null |
||
156 | * @throws InvalidDataBagPathException |
||
157 | */ |
||
158 | public function get($path, $default = null) |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Set a value in the bag. |
||
171 | * |
||
172 | * @param string $path path to the target (see get() for examples) |
||
173 | * @param mixed $value new value |
||
174 | * |
||
175 | * @throws InvalidDataBagPathException |
||
176 | */ |
||
177 | public function set($path, $value) |
||
178 | { |
||
179 | $path = (string)$path; |
||
180 | $this->guardAgainstInvalidPath($path); |
||
181 | |||
182 | unset($this->cache[$path]); |
||
183 | |||
184 | if ($value === null) { |
||
185 | $this->remove($path); |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | $this->setByPath($path, $value); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $path |
||
194 | * @param mixed $value |
||
195 | */ |
||
196 | private function setByPath($path, $value) |
||
197 | { |
||
198 | list($entityType, $path) = explode('.', $path, 2); |
||
199 | |||
200 | // Direct property |
||
201 | if (strpos($path, '.') === false) { |
||
202 | $this->data[$entityType][$path] = $value; |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | // Indexed |
||
207 | list($path, $index) = explode('.', $path, 2); |
||
208 | |||
209 | $field = null; |
||
210 | if (strpos($index, '.') > 0) { |
||
211 | list($index, $field) = explode('.', $index, 2); |
||
212 | } |
||
213 | |||
214 | $target = $index; |
||
215 | if (!is_numeric($index)) { |
||
216 | if (is_array($value)) { |
||
217 | $value['type'] = $index; |
||
218 | } |
||
219 | $index = null; |
||
220 | if (isset($this->data[$entityType][$path])) { |
||
221 | foreach ((array)$this->data[$entityType][$path] as $nodeIndex => $node) { |
||
222 | if ($node['type'] === $target) { |
||
223 | $index = $nodeIndex; |
||
224 | break; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | // No index found, new entry |
||
231 | if ($index === null) { |
||
232 | if ($field === null) { |
||
233 | $this->data[$entityType][$path][] = $value; |
||
234 | return; |
||
235 | } |
||
236 | $value = [ |
||
237 | $field => $value |
||
238 | ]; |
||
239 | if (!is_numeric($target)) { |
||
240 | $value['type'] = $target; |
||
241 | } |
||
242 | $this->data[$entityType][$path][] = $value; |
||
243 | return; |
||
244 | } |
||
245 | |||
246 | // Use found index |
||
247 | if ($field === null) { |
||
248 | $this->data[$entityType][$path][$index] = $value; |
||
249 | return; |
||
250 | } |
||
251 | $this->data[$entityType][$path][$index][$field] = $value; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Check if a certain entity type exists in the dataBag |
||
256 | * |
||
257 | * @param string $entityType |
||
258 | * |
||
259 | * @return bool true if the entity type exists |
||
260 | */ |
||
261 | public function hasEntityData($entityType) |
||
262 | { |
||
263 | return isset($this->data[$entityType]); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Remove a property from the bag. |
||
268 | * |
||
269 | * @param string $path path to the target (see get() for examples) |
||
270 | * @param bool $removeAll remove all when the index is numeric (to prevent a new value after re-indexing) |
||
271 | * |
||
272 | * @throws InvalidDataBagPathException |
||
273 | */ |
||
274 | public function remove($path, $removeAll = true) |
||
275 | { |
||
276 | $path = (string)$path; |
||
277 | $this->guardAgainstInvalidPath($path); |
||
278 | |||
279 | list($entityType, $path) = explode('.', $path, 2); |
||
280 | |||
281 | // Direct property |
||
282 | if (strpos($path, '.') === false) { |
||
283 | if (!isset($this->data[$entityType][$path])) { |
||
284 | return; |
||
285 | } |
||
286 | $this->data[$entityType][$path] = null; |
||
287 | return; |
||
288 | } |
||
289 | |||
290 | $this->removeIndexed($path, $entityType, $removeAll); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param string $path |
||
295 | * @param string $entityType |
||
296 | * @param bool $removeAll |
||
297 | */ |
||
298 | private function removeIndexed($path, $entityType, $removeAll) |
||
299 | { |
||
300 | list($path, $index) = explode('.', $path); |
||
301 | |||
302 | // Target doesn't exist, nothing to remove |
||
303 | if (empty($this->data[$entityType][$path])) { |
||
304 | return; |
||
305 | } |
||
306 | |||
307 | if (is_numeric($index)) { |
||
308 | $index = (int)$index; |
||
309 | if ($removeAll) { |
||
310 | // Remove all (higher) values to prevent a new value after re-indexing |
||
311 | if ($index === 0) { |
||
312 | $this->data[$entityType][$path] = null; |
||
313 | return; |
||
314 | } |
||
315 | $this->data[$entityType][$path] = array_slice($this->data[$entityType][$path], 0, $index); |
||
316 | return; |
||
317 | } |
||
318 | unset($this->data[$entityType][$path][$index]); |
||
319 | } else { |
||
320 | // Filter out all nodes of the specified type |
||
321 | $this->data[$entityType][$path] = array_filter( |
||
322 | $this->data[$entityType][$path], |
||
323 | static function ($node) use ($index) { |
||
324 | return empty($node['type']) || $node['type'] !== $index; |
||
325 | } |
||
326 | ); |
||
327 | } |
||
328 | |||
329 | // If we end up with an empty array make it NULL |
||
330 | if (empty($this->data[$entityType][$path])) { |
||
331 | $this->data[$entityType][$path] = null; |
||
332 | return; |
||
333 | } |
||
334 | |||
335 | // Re-index |
||
336 | $this->data[$entityType][$path] = array_values($this->data[$entityType][$path]); |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Check if the initial data has changed |
||
341 | * |
||
342 | * @param string $entityType entity type to check |
||
343 | * |
||
344 | * @return bool|null true if changed, false if not and null if the entity type is not set |
||
345 | */ |
||
346 | public function isDirty($entityType) |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Get the raw data array |
||
359 | * |
||
360 | * @param string|null $entityType only get the specified type (optional) |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function getState($entityType = null) |
||
365 | { |
||
366 | if ($entityType === null) { |
||
367 | return $this->data; |
||
368 | } |
||
369 | return isset($this->data[$entityType]) ? $this->data[$entityType] : []; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @param string $path |
||
374 | */ |
||
375 | private function guardAgainstInvalidPath($path) |
||
383 | } |
||
384 | } |
||
385 | |||
386 | } |
||
387 |