| Conditions | 10 |
| Paths | 20 |
| Total Lines | 49 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php /** @noinspection MoreThanThreeArgumentsInspection */ |
||
| 129 | public function set( |
||
| 130 | callable $inputDataFunction, |
||
| 131 | $tHashKeys, |
||
| 132 | $tRedisTags, |
||
| 133 | int $iCache = null |
||
| 134 | ): self { |
||
| 135 | try { |
||
| 136 | if ($tHashKeys === [] || $tHashKeys === null || (\is_string($tHashKeys) && trim($tHashKeys) === '')) { |
||
| 137 | throw new InvalidArgumentException('HashKey is required!', 1); |
||
| 138 | } |
||
| 139 | |||
| 140 | $tRedisTags = (array)$tRedisTags; |
||
| 141 | $tHashKeys = (array)$tHashKeys; |
||
| 142 | $iCache = $iCache ?? $this->defaultTtl; |
||
| 143 | |||
| 144 | $hashKey = $this->genHash($tHashKeys); |
||
| 145 | |||
| 146 | if ($this->isEnabled === false) { |
||
| 147 | $this->logger->debug(sprintf('CACHE [%s]: cache omitted [not enabled]', $hashKey)); |
||
| 148 | $this->userData = $inputDataFunction(); |
||
| 149 | } elseif ($iCache > 0) { |
||
| 150 | $oCachedItem = $this->oCache->getItem($hashKey); |
||
| 151 | //do każdego zapytania dajemy TAG związany z projektem |
||
| 152 | $tRedisTags = array_unique(array_merge([$this->projectHash], $tRedisTags)); |
||
| 153 | |||
| 154 | if (!$oCachedItem->isHit() || $oCachedItem->get() === null) { |
||
| 155 | $this->logger->debug(sprintf('CACHE [%s]: old or NULL, reset [ttl=%s]', $hashKey, $iCache)); |
||
| 156 | $this->userData = $inputDataFunction(); |
||
| 157 | $oCachedItem |
||
| 158 | ->set($this->userData) |
||
| 159 | ->setTags($tRedisTags) |
||
| 160 | ->expiresAfter($iCache) |
||
| 161 | ; |
||
| 162 | $this->oCache->save($oCachedItem); |
||
| 163 | } else { |
||
| 164 | $this->logger->debug(sprintf('CACHE [%s]: getting from cache', $hashKey)); |
||
| 165 | $this->userData = $oCachedItem->get(); |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | $this->logger->debug(sprintf('CACHE [%s]: cache omitted [ttl=0]', $hashKey)); |
||
| 169 | $this->userData = $inputDataFunction(); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } catch (\Exception $e) { |
||
| 174 | $this->logger->warning(sprintf('CACHE Error: %s. Using fallback function.', $e->getMessage())); |
||
| 175 | $this->userData = $inputDataFunction(); |
||
| 176 | |||
| 177 | return $this; |
||
| 178 | } |
||
| 205 |