|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by jensk on 17-3-2017. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace library\storage\storage; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use library\storage\factories\BrickFactory; |
|
10
|
|
|
|
|
11
|
|
View Code Duplication |
class BricksStorage extends AbstractStorage |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public function getBricks() |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->repository->bricks; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Add a brick |
|
23
|
|
|
* |
|
24
|
|
|
* @param $postValues |
|
25
|
|
|
* |
|
26
|
|
|
* @throws \Exception |
|
27
|
|
|
*/ |
|
28
|
|
|
public function addBrick($postValues) |
|
29
|
|
|
{ |
|
30
|
|
|
$brickObject = BrickFactory::createBrickFromPostValues($postValues); |
|
31
|
|
|
|
|
32
|
|
|
$bricks = $this->repository->bricks; |
|
33
|
|
|
$bricks[] = $brickObject; |
|
34
|
|
|
$this->repository->bricks = $bricks; |
|
35
|
|
|
|
|
36
|
|
|
$this->save(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get a brick by its slug |
|
41
|
|
|
* |
|
42
|
|
|
* @param $slug |
|
43
|
|
|
* |
|
44
|
|
|
* @return \stdClass |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getBrickBySlug($slug) |
|
47
|
|
|
{ |
|
48
|
|
|
$bricks = $this->repository->bricks; |
|
49
|
|
|
foreach ($bricks as $brick) { |
|
50
|
|
|
if ($brick->slug == $slug) { |
|
51
|
|
|
return $brick; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Save changes to a brick |
|
60
|
|
|
* |
|
61
|
|
|
* @param $slug |
|
62
|
|
|
* @param $postValues |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
public function saveBrick($slug, $postValues) |
|
67
|
|
|
{ |
|
68
|
|
|
$brickObject = BrickFactory::createBrickFromPostValues($postValues); |
|
69
|
|
|
|
|
70
|
|
|
$bricks = $this->repository->bricks; |
|
71
|
|
|
foreach ($bricks as $key => $brick) { |
|
72
|
|
|
if ($brick->slug == $slug) { |
|
73
|
|
|
$bricks[$key] = $brickObject; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
$this->repository->bricks = $bricks; |
|
77
|
|
|
$this->save(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Delete a brick by its slug |
|
82
|
|
|
* |
|
83
|
|
|
* @param $slug |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
public function deleteBrickBySlug($slug) |
|
88
|
|
|
{ |
|
89
|
|
|
$bricks = $this->repository->bricks; |
|
90
|
|
|
foreach ($bricks as $key => $brickObject) { |
|
91
|
|
|
if ($brickObject->slug == $slug) { |
|
92
|
|
|
unset($bricks[$key]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$bricks = array_values($bricks); |
|
97
|
|
|
$this->repository->bricks = $bricks; |
|
98
|
|
|
$this->save(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
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.