Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class ObjectStateHandler extends AbstractHandler implements ObjectStateHandlerInterface |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public function createGroup(InputStruct $input) |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function loadGroup($groupId) |
||
36 | { |
||
37 | $cacheItem = $this->cache->getItem('ez-state-group-' . $groupId); |
||
38 | if ($cacheItem->isHit()) { |
||
39 | return $cacheItem->get(); |
||
40 | } |
||
41 | |||
42 | $this->logger->logCall(__METHOD__, array('groupId' => $groupId)); |
||
43 | $group = $this->persistenceHandler->objectStateHandler()->loadGroup($groupId); |
||
44 | |||
45 | $cacheItem->set($group); |
||
46 | $cacheItem->tag(['state-group-' . $group->id]); |
||
47 | $this->cache->save($cacheItem); |
||
48 | |||
49 | return $group; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function loadGroupByIdentifier($identifier) |
||
56 | { |
||
57 | $cacheItem = $this->cache->getItem('ez-state-group-' . $identifier . '-by-identifier'); |
||
58 | if ($cacheItem->isHit()) { |
||
59 | return $cacheItem->get(); |
||
60 | } |
||
61 | |||
62 | $this->logger->logCall(__METHOD__, array('groupId' => $identifier)); |
||
63 | $group = $this->persistenceHandler->objectStateHandler()->loadGroupByIdentifier($identifier); |
||
64 | |||
65 | $cacheItem->set($group); |
||
66 | $cacheItem->tag(['state-group-' . $group->id]); |
||
67 | $this->cache->save($cacheItem); |
||
68 | |||
69 | return $group; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function loadAllGroups($offset = 0, $limit = -1) |
||
76 | { |
||
77 | $cacheItem = $this->cache->getItem('ez-state-group-all'); |
||
78 | if ($cacheItem->isHit()) { |
||
79 | return array_slice($cacheItem->get(), $offset, $limit > -1 ? $limit : null); |
||
80 | } |
||
81 | |||
82 | $this->logger->logCall(__METHOD__, array('offset' => $offset, 'limit' => $limit)); |
||
83 | $stateGroups = $this->persistenceHandler->objectStateHandler()->loadAllGroups(0, -1); |
||
84 | |||
85 | $cacheItem->set($stateGroups); |
||
86 | $cacheTags = []; |
||
87 | foreach ($stateGroups as $group) { |
||
88 | $cacheTags[] = 'state-group-' . $group->id; |
||
89 | } |
||
90 | $cacheItem->tag($cacheTags); |
||
91 | $this->cache->save($cacheItem); |
||
92 | |||
93 | return array_slice($stateGroups, $offset, $limit > -1 ? $limit : null); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function loadObjectStates($groupId) |
||
100 | { |
||
101 | $cacheItem = $this->cache->getItem('ez-state-list-by-group-' . $groupId); |
||
102 | if ($cacheItem->isHit()) { |
||
103 | return $cacheItem->get(); |
||
104 | } |
||
105 | |||
106 | $this->logger->logCall(__METHOD__, array('groupId' => $groupId)); |
||
107 | $objectStates = $this->persistenceHandler->objectStateHandler()->loadObjectStates($groupId); |
||
108 | |||
109 | $cacheItem->set($objectStates); |
||
110 | $cacheTags = ['state-group-' . $groupId]; |
||
111 | foreach ($objectStates as $state) { |
||
112 | $cacheTags[] = 'state-' . $state->id; |
||
113 | } |
||
114 | $cacheItem->tag($cacheTags); |
||
115 | $this->cache->save($cacheItem); |
||
116 | |||
117 | return $objectStates; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function updateGroup($groupId, InputStruct $input) |
||
124 | { |
||
125 | $this->logger->logCall(__METHOD__, array('groupId' => $groupId, 'struct' => $input)); |
||
126 | $return = $this->persistenceHandler->objectStateHandler()->updateGroup($groupId, $input); |
||
127 | |||
128 | $this->cache->invalidateTags(['state-group-' . $groupId]); |
||
129 | |||
130 | return $return; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function deleteGroup($groupId) |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function create($groupId, InputStruct $input) |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | View Code Duplication | public function load($stateId) |
|
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | View Code Duplication | public function loadByIdentifier($identifier, $groupId) |
|
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function update($stateId, InputStruct $input) |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | public function setPriority($stateId, $priority) |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function delete($stateId) |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | public function setContentState($contentId, $groupId, $stateId) |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | View Code Duplication | public function getContentState($contentId, $stateGroupId) |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function getContentCount($stateId) |
||
280 | } |
||
281 |