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) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function loadObjectStates($groupId) |
||
| 100 | { |
||
| 101 | $cacheItem = $this->cache->getItem('ez-state-list-'.$groupId.'-by-group'); |
||
| 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) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | public function update($stateId, InputStruct $input) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function setPriority($stateId, $priority) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function delete($stateId) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function setContentState($contentId, $groupId, $stateId) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function getContentState($contentId, $stateGroupId) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function getContentCount($stateId) |
||
| 281 | } |
||
| 282 |