| Total Complexity | 42 |
| Total Lines | 430 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RedisRepository 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 RedisRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class RedisRepository extends AbstractRepository implements ListRepositoryInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Client |
||
| 27 | */ |
||
| 28 | private $client; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * RedisRepository constructor. |
||
| 32 | * |
||
| 33 | * @param Client $client |
||
| 34 | */ |
||
| 35 | public function __construct(Client $client) |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param ListCollection $list |
||
| 42 | * @param null $ttl |
||
|
|
|||
| 43 | * @param null $chunkSize |
||
| 44 | * |
||
| 45 | * @return mixed |
||
| 46 | * |
||
| 47 | * @throws ListAlreadyExistsException |
||
| 48 | */ |
||
| 49 | public function create(ListCollection $list, $ttl = null, $chunkSize = null) |
||
| 50 | { |
||
| 51 | // check if list already exists in memory |
||
| 52 | $listUuid = (string) $list->getUuid(); |
||
| 53 | if ($this->existsListInIndex($listUuid) && $this->exists($listUuid)) { |
||
| 54 | throw new ListAlreadyExistsException('List '.$list->getUuid().' already exists in memory.'); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!$chunkSize && !is_int($chunkSize)) { |
||
| 58 | $chunkSize = self::CHUNKSIZE; |
||
| 59 | } |
||
| 60 | |||
| 61 | $items = $list->getElements(); |
||
| 62 | |||
| 63 | // persist in memory array in chunks |
||
| 64 | $arrayChunks = array_chunk($items, $chunkSize, true); |
||
| 65 | |||
| 66 | $options = [ |
||
| 67 | 'cas' => true, |
||
| 68 | 'watch' => $this->getArrayChunksKeys($listUuid, count($arrayChunks)), |
||
| 69 | 'retry' => 3, |
||
| 70 | ]; |
||
| 71 | |||
| 72 | // persist all in a transaction |
||
| 73 | $this->client->transaction($options, function ($tx) use ($arrayChunks, $list, $ttl, $listUuid, $items, $chunkSize) { |
||
| 74 | foreach ($arrayChunks as $chunkNumber => $item) { |
||
| 75 | foreach ($item as $key => $element) { |
||
| 76 | $listChunkUuid = $listUuid.self::SEPARATOR.self::CHUNK.'-'.($chunkNumber + 1); |
||
| 77 | $elementUuid = $element->getUuid(); |
||
| 78 | $body = $element->getBody(); |
||
| 79 | |||
| 80 | $tx->hset( |
||
| 81 | (string) $listChunkUuid, |
||
| 82 | (string) $elementUuid, |
||
| 83 | (string) $body |
||
| 84 | ); |
||
| 85 | |||
| 86 | // set ttl |
||
| 87 | if ($ttl) { |
||
| 88 | $tx->expire( |
||
| 89 | (string) $listChunkUuid, |
||
| 90 | $ttl |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | // set headers |
||
| 97 | if ($list->getHeaders()) { |
||
| 98 | foreach ($list->getHeaders() as $key => $header) { |
||
| 99 | $this->client->hset( |
||
| 100 | $listUuid.self::SEPARATOR.self::HEADERS, |
||
| 101 | $key, |
||
| 102 | $header |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($ttl) { |
||
| 107 | $this->client->expire($listUuid.self::SEPARATOR.self::HEADERS, $ttl); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | // add list to index |
||
| 112 | $this->addOrUpdateListToIndex( |
||
| 113 | $listUuid, |
||
| 114 | (int) count($items), |
||
| 115 | (int) count($arrayChunks), |
||
| 116 | (int) $chunkSize, |
||
| 117 | $ttl |
||
| 118 | ); |
||
| 119 | }); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $listUuid |
||
| 124 | * @param $numberOfChunks |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | private function getArrayChunksKeys($listUuid, $numberOfChunks) |
||
| 129 | { |
||
| 130 | $arrayChunksKeys = []; |
||
| 131 | for ($i = 0; $i < $numberOfChunks; ++$i) { |
||
| 132 | $arrayChunksKeys[] = $listUuid.self::SEPARATOR.self::CHUNK.'-'.($i + 1); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $arrayChunksKeys; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $listUuid |
||
| 140 | * @param $elementUuid |
||
| 141 | * |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | public function deleteElement($listUuid, $elementUuid) |
||
| 145 | { |
||
| 146 | $numberOfChunks = $this->getNumberOfChunks($listUuid); |
||
| 147 | $chunkSize = $this->getChunkSize($listUuid); |
||
| 148 | |||
| 149 | $options = [ |
||
| 150 | 'cas' => true, |
||
| 151 | 'watch' => $this->getArrayChunksKeys($listUuid, $numberOfChunks), |
||
| 152 | 'retry' => 3, |
||
| 153 | ]; |
||
| 154 | |||
| 155 | // delete in a transaction |
||
| 156 | $this->client->transaction($options, function ($tx) use ($listUuid, $numberOfChunks, $chunkSize, $elementUuid) { |
||
| 157 | for ($i = 1; $i <= $numberOfChunks; ++$i) { |
||
| 158 | $chunkNumber = $listUuid.self::SEPARATOR.self::CHUNK.'-'.$i; |
||
| 159 | $chunk = $this->client->hgetall($chunkNumber); |
||
| 160 | |||
| 161 | if (array_key_exists($elementUuid, $chunk)) { |
||
| 162 | // delete elements from chunk |
||
| 163 | $tx->hdel($chunkNumber, $elementUuid); |
||
| 164 | |||
| 165 | // update list index |
||
| 166 | $prevIndex = $this->getIndex($listUuid); |
||
| 167 | $this->addOrUpdateListToIndex( |
||
| 168 | $listUuid, |
||
| 169 | ($prevIndex['size'] - 1), |
||
| 170 | $numberOfChunks, |
||
| 171 | $chunkSize, |
||
| 172 | $prevIndex['ttl'] |
||
| 173 | ); |
||
| 174 | |||
| 175 | // delete headers if counter = 0 |
||
| 176 | $headersKey = $listUuid.self::SEPARATOR.self::HEADERS; |
||
| 177 | if ($this->getCounter($listUuid) === 0) { |
||
| 178 | $tx->del($headersKey); |
||
| 179 | } |
||
| 180 | |||
| 181 | break; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | }); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param $listUuid |
||
| 189 | * @param $size |
||
| 190 | * @param $numberOfChunks |
||
| 191 | * @param null $ttl |
||
| 192 | */ |
||
| 193 | private function addOrUpdateListToIndex($listUuid, $size, $numberOfChunks, $chunkSize, $ttl = null) |
||
| 194 | { |
||
| 195 | $indexKey = ListRepositoryInterface::INDEX; |
||
| 196 | $this->client->hset( |
||
| 197 | $indexKey, |
||
| 198 | (string) $listUuid, |
||
| 199 | serialize([ |
||
| 200 | 'uuid' => $listUuid, |
||
| 201 | 'created_on' => new \DateTimeImmutable(), |
||
| 202 | 'size' => $size, |
||
| 203 | 'chunks' => $numberOfChunks, |
||
| 204 | 'chunk-size' => $chunkSize, |
||
| 205 | 'headers' => $this->getHeaders($listUuid), |
||
| 206 | 'ttl' => $ttl, |
||
| 207 | ]) |
||
| 208 | ); |
||
| 209 | |||
| 210 | if ($size === 0) { |
||
| 211 | $this->removeListFromIndex((string) $listUuid); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $listUuid |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function exists($listUuid) |
||
| 221 | { |
||
| 222 | $listFirstChunk = $this->client->hgetall($listUuid.self::SEPARATOR.self::CHUNK.'-1'); |
||
| 223 | |||
| 224 | return (count($listFirstChunk) === 0) ? false : true; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param $listUuid |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function findListByUuid($listUuid) |
||
| 233 | { |
||
| 234 | $collection = $this->client->hgetall($listUuid.self::SEPARATOR.self::CHUNK.'-1'); |
||
| 235 | $number = $this->getNumberOfChunks($listUuid); |
||
| 236 | |||
| 237 | for ($i = 2; $i <= $number; ++$i) { |
||
| 238 | $collection = array_merge($collection, $this->client->hgetall($listUuid.self::SEPARATOR.self::CHUNK.'-'.$i)); |
||
| 239 | } |
||
| 240 | |||
| 241 | return array_map('unserialize', $collection); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | public function flush() |
||
| 248 | { |
||
| 249 | $this->client->flushall(); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $listUuid |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function getHeaders($listUuid) |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param null $listUuid |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function getIndex($listUuid = null) |
||
| 268 | { |
||
| 269 | $indexKey = ListRepositoryInterface::INDEX; |
||
| 270 | $index = $this->client->hgetall($indexKey); |
||
| 271 | $this->removeExpiredListsFromIndex($index); |
||
| 272 | |||
| 273 | if ($listUuid) { |
||
| 274 | return (isset($index[(string) $listUuid])) ? unserialize($this->client->hget($indexKey, $listUuid)) : null; |
||
| 275 | } |
||
| 276 | |||
| 277 | return array_map('unserialize', $this->client->hgetall($indexKey)); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public function getStatistics() |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param $listUuid |
||
| 290 | * @param ListElement $listElement |
||
| 291 | * |
||
| 292 | * @throws ListElementNotConsistentException |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function pushElement($listUuid, ListElement $listElement) |
||
| 337 | ); |
||
| 338 | }); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param $listUuid |
||
| 343 | * |
||
| 344 | * @return mixed |
||
| 345 | */ |
||
| 346 | public function removeListFromIndex($listUuid) |
||
| 347 | { |
||
| 348 | $this->client->hdel( |
||
| 349 | ListRepositoryInterface::INDEX, |
||
| 350 | $listUuid |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param $listUuid |
||
| 356 | * @param $elementUuid |
||
| 357 | * @param array $data |
||
| 358 | * |
||
| 359 | * @throws ListElementNotConsistentException |
||
| 360 | * |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | public function updateElement($listUuid, $elementUuid, $data) |
||
| 364 | { |
||
| 365 | $numberOfChunks = $this->getNumberOfChunks($listUuid); |
||
| 366 | |||
| 367 | $options = [ |
||
| 368 | 'cas' => true, |
||
| 369 | 'watch' => $this->getArrayChunksKeys($listUuid, $numberOfChunks), |
||
| 370 | 'retry' => 3, |
||
| 371 | ]; |
||
| 372 | |||
| 373 | // persist in a transaction |
||
| 374 | $this->client->transaction($options, function ($tx) use ($numberOfChunks, $listUuid, $elementUuid, $data) { |
||
| 375 | for ($i = 1; $i <= $numberOfChunks; ++$i) { |
||
| 376 | $chunkNumber = $listUuid.self::SEPARATOR.self::CHUNK.'-'.$i; |
||
| 377 | $chunk = $this->client->hgetall($chunkNumber); |
||
| 378 | |||
| 379 | if (array_key_exists($elementUuid, $chunk)) { |
||
| 380 | $listElement = $this->findElement( |
||
| 381 | (string) $listUuid, |
||
| 382 | (string) $elementUuid |
||
| 383 | ); |
||
| 384 | |||
| 385 | $updatedElementBody = $this->updateListElementBody($listElement, $data); |
||
| 386 | |||
| 387 | if (!ListElementConsistencyChecker::isConsistent($updatedElementBody, $this->findListByUuid($listUuid))) { |
||
| 388 | throw new ListElementNotConsistentException('Element '.(string) $elementUuid.' is not consistent with list data.'); |
||
| 389 | } |
||
| 390 | |||
| 391 | $updatedElement = new ListElement( |
||
| 392 | new ListElementUuid($elementUuid), |
||
| 393 | $updatedElementBody |
||
| 394 | ); |
||
| 395 | $body = $updatedElement->getBody(); |
||
| 396 | |||
| 397 | $tx->hset( |
||
| 398 | $chunkNumber, |
||
| 399 | $elementUuid, |
||
| 400 | $body |
||
| 401 | ); |
||
| 402 | |||
| 403 | break; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | }); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param $listUuid |
||
| 411 | * @param null $ttl |
||
| 412 | * |
||
| 413 | * @return mixed |
||
| 414 | * |
||
| 415 | * @throws ListDoesNotExistsException |
||
| 416 | */ |
||
| 417 | public function updateTtl($listUuid, $ttl) |
||
| 453 | ); |
||
| 454 | }); |
||
| 455 | } |
||
| 456 | } |
||
| 457 |