@@ -29,10 +29,12 @@ discard block |
||
| 29 | 29 | public function get($key, $default = null) |
| 30 | 30 | { |
| 31 | 31 | $this->validateKey($key); |
| 32 | - if (\array_key_exists($key, $this->cache) && !$this->isExpired($key)) { |
|
| 32 | + if (\array_key_exists($key, $this->cache) && !$this->isExpired($key)) |
|
| 33 | + { |
|
| 33 | 34 | /** @psalm-var mixed $value */ |
| 34 | 35 | $value = $this->cache[$key][0]; |
| 35 | - if (\is_object($value)) { |
|
| 36 | + if (\is_object($value)) |
|
| 37 | + { |
|
| 36 | 38 | $value = clone $value; |
| 37 | 39 | } |
| 38 | 40 | |
@@ -46,10 +48,12 @@ discard block |
||
| 46 | 48 | { |
| 47 | 49 | $this->validateKey($key); |
| 48 | 50 | $expiration = $this->ttlToExpiration($ttl); |
| 49 | - if ($expiration < 0) { |
|
| 51 | + if ($expiration < 0) |
|
| 52 | + { |
|
| 50 | 53 | return $this->delete($key); |
| 51 | 54 | } |
| 52 | - if (\is_object($value)) { |
|
| 55 | + if (\is_object($value)) |
|
| 56 | + { |
|
| 53 | 57 | $value = clone $value; |
| 54 | 58 | } |
| 55 | 59 | $this->cache[$key] = [$value, $expiration]; |
@@ -81,7 +85,8 @@ discard block |
||
| 81 | 85 | $this->validateKeys($keys); |
| 82 | 86 | /** @psalm-var string[] $keys */ |
| 83 | 87 | $result = []; |
| 84 | - foreach ($keys as $key) { |
|
| 88 | + foreach ($keys as $key) |
|
| 89 | + { |
|
| 85 | 90 | /** @psalm-var mixed */ |
| 86 | 91 | $result[$key] = $this->get($key, $default); |
| 87 | 92 | } |
@@ -99,7 +104,8 @@ discard block |
||
| 99 | 104 | /** |
| 100 | 105 | * @psalm-var mixed $value |
| 101 | 106 | */ |
| 102 | - foreach ($values as $key => $value) { |
|
| 107 | + foreach ($values as $key => $value) |
|
| 108 | + { |
|
| 103 | 109 | $this->set((string)$key, $value, $ttl); |
| 104 | 110 | } |
| 105 | 111 | return $this->returnOnSet; |
@@ -110,7 +116,8 @@ discard block |
||
| 110 | 116 | $keys = $this->iterableToArray($keys); |
| 111 | 117 | $this->validateKeys($keys); |
| 112 | 118 | /** @var string[] $keys */ |
| 113 | - foreach ($keys as $key) { |
|
| 119 | + foreach ($keys as $key) |
|
| 120 | + { |
|
| 114 | 121 | $this->delete($key); |
| 115 | 122 | } |
| 116 | 123 | return $this->returnOnDelete; |
@@ -131,7 +138,8 @@ discard block |
||
| 131 | 138 | public function getValues(): array |
| 132 | 139 | { |
| 133 | 140 | $result = []; |
| 134 | - foreach ($this->cache as $key => $value) { |
|
| 141 | + foreach ($this->cache as $key => $value) |
|
| 142 | + { |
|
| 135 | 143 | /** @psalm-var mixed */ |
| 136 | 144 | $result[$key] = $value[0]; |
| 137 | 145 | } |
@@ -157,11 +165,16 @@ discard block |
||
| 157 | 165 | { |
| 158 | 166 | $ttl = $this->normalizeTtl($ttl); |
| 159 | 167 | |
| 160 | - if ($ttl === null) { |
|
| 168 | + if ($ttl === null) |
|
| 169 | + { |
|
| 161 | 170 | $expiration = self::EXPIRATION_INFINITY; |
| 162 | - } elseif ($ttl <= 0) { |
|
| 171 | + } |
|
| 172 | + elseif ($ttl <= 0) |
|
| 173 | + { |
|
| 163 | 174 | $expiration = self::EXPIRATION_EXPIRED; |
| 164 | - } else { |
|
| 175 | + } |
|
| 176 | + else |
|
| 177 | + { |
|
| 165 | 178 | $expiration = $ttl + time(); |
| 166 | 179 | } |
| 167 | 180 | |
@@ -177,11 +190,13 @@ discard block |
||
| 177 | 190 | */ |
| 178 | 191 | private function normalizeTtl($ttl): ?int |
| 179 | 192 | { |
| 180 | - if ($ttl instanceof DateInterval) { |
|
| 193 | + if ($ttl instanceof DateInterval) |
|
| 194 | + { |
|
| 181 | 195 | return (new DateTime('@0'))->add($ttl)->getTimestamp(); |
| 182 | 196 | } |
| 183 | 197 | |
| 184 | - if (\is_string($ttl)) { |
|
| 198 | + if (\is_string($ttl)) |
|
| 199 | + { |
|
| 185 | 200 | return (int)$ttl; |
| 186 | 201 | } |
| 187 | 202 | |
@@ -195,7 +210,8 @@ discard block |
||
| 195 | 210 | */ |
| 196 | 211 | private function iterableToArray($iterable): array |
| 197 | 212 | { |
| 198 | - if (!is_iterable($iterable)) { |
|
| 213 | + if (!is_iterable($iterable)) |
|
| 214 | + { |
|
| 199 | 215 | throw new \InvalidArgumentException(\sprintf('Iterable is expected, got %s.', \gettype($iterable))); |
| 200 | 216 | } |
| 201 | 217 | return $iterable instanceof Traversable ? \iterator_to_array($iterable) : $iterable; |
@@ -206,7 +222,8 @@ discard block |
||
| 206 | 222 | */ |
| 207 | 223 | private function validateKey($key): void |
| 208 | 224 | { |
| 209 | - if (!\is_string($key) || $key === '' || \strpbrk($key, '{}()/\@:')) { |
|
| 225 | + if (!\is_string($key) || $key === '' || \strpbrk($key, '{}()/\@:')) |
|
| 226 | + { |
|
| 210 | 227 | throw new \InvalidArgumentException('Invalid key value.'); |
| 211 | 228 | } |
| 212 | 229 | } |
@@ -217,7 +234,8 @@ discard block |
||
| 217 | 234 | private function validateKeys(array $keys): void |
| 218 | 235 | { |
| 219 | 236 | /** @psalm-var mixed $key */ |
| 220 | - foreach ($keys as $key) { |
|
| 237 | + foreach ($keys as $key) |
|
| 238 | + { |
|
| 221 | 239 | $this->validateKey($key); |
| 222 | 240 | } |
| 223 | 241 | } |
@@ -76,7 +76,8 @@ |
||
| 76 | 76 | 'test' => $this->defaultCache, |
| 77 | 77 | 'array' => new ArrayCache(), |
| 78 | 78 | ][$name] ?? null; |
| 79 | - if ($result === null) { |
|
| 79 | + if ($result === null) |
|
| 80 | + { |
|
| 80 | 81 | throw new NotFoundException(); |
| 81 | 82 | } |
| 82 | 83 | return $result; |
@@ -55,8 +55,10 @@ |
||
| 55 | 55 | { |
| 56 | 56 | $manager = new CacheManager($config, $container); |
| 57 | 57 | |
| 58 | - foreach ($config->getAliases() as $alias => $storageName) { |
|
| 59 | - $container->bind($alias, static function (CacheManager $manager) use ($storageName) { |
|
| 58 | + foreach ($config->getAliases() as $alias => $storageName) |
|
| 59 | + { |
|
| 60 | + $container->bind($alias, static function (CacheManager $manager) use ($storageName) |
|
| 61 | + { |
|
| 60 | 62 | return $manager->storage($storageName); |
| 61 | 63 | }); |
| 62 | 64 | } |
@@ -25,21 +25,30 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function createInjection(ReflectionClass $class, string $context = null): CacheInterface |
| 27 | 27 | { |
| 28 | - try { |
|
| 29 | - if ($context === null) { |
|
| 28 | + try |
|
| 29 | + { |
|
| 30 | + if ($context === null) |
|
| 31 | + { |
|
| 30 | 32 | $connection = $this->provider->storage(); |
| 31 | - } else { |
|
| 33 | + } |
|
| 34 | + else |
|
| 35 | + { |
|
| 32 | 36 | // Get Cache by context |
| 33 | - try { |
|
| 37 | + try |
|
| 38 | + { |
|
| 34 | 39 | $connection = $this->provider->storage($context); |
| 35 | - } catch (InvalidArgumentException $e) { |
|
| 40 | + } |
|
| 41 | + catch (InvalidArgumentException $e) |
|
| 42 | + { |
|
| 36 | 43 | // Case when context doesn't match to configured connections |
| 37 | 44 | return $this->provider->storage(); |
| 38 | 45 | } |
| 39 | 46 | } |
| 40 | 47 | |
| 41 | 48 | $this->matchType($class, $context, $connection); |
| 42 | - } catch (\Throwable $e) { |
|
| 49 | + } |
|
| 50 | + catch (\Throwable $e) |
|
| 51 | + { |
|
| 43 | 52 | throw new ContainerException(sprintf("Can't inject the required cache. %s", $e->getMessage()), 0, $e); |
| 44 | 53 | } |
| 45 | 54 | |
@@ -54,7 +63,8 @@ discard block |
||
| 54 | 63 | private function matchType(ReflectionClass $class, ?string $context, CacheInterface $connection): void |
| 55 | 64 | { |
| 56 | 65 | $className = $class->getName(); |
| 57 | - if ($className !== CacheInterface::class && !$connection instanceof $className) { |
|
| 66 | + if ($className !== CacheInterface::class && !$connection instanceof $className) |
|
| 67 | + { |
|
| 58 | 68 | throw new \RuntimeException( |
| 59 | 69 | \sprintf( |
| 60 | 70 | "The cache obtained by the context `%s` doesn't match the type `%s`.", |
@@ -25,21 +25,30 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function createInjection(ReflectionClass $class, string $context = null): QueueInterface |
| 27 | 27 | { |
| 28 | - try { |
|
| 29 | - if ($context === null) { |
|
| 28 | + try |
|
| 29 | + { |
|
| 30 | + if ($context === null) |
|
| 31 | + { |
|
| 30 | 32 | $connection = $this->queueManager->getConnection(); |
| 31 | - } else { |
|
| 33 | + } |
|
| 34 | + else |
|
| 35 | + { |
|
| 32 | 36 | // Get Queue by context |
| 33 | - try { |
|
| 37 | + try |
|
| 38 | + { |
|
| 34 | 39 | $connection = $this->queueManager->getConnection($context); |
| 35 | - } catch (InvalidArgumentException $e) { |
|
| 40 | + } |
|
| 41 | + catch (InvalidArgumentException $e) |
|
| 42 | + { |
|
| 36 | 43 | // Case when context doesn't match to configured connections |
| 37 | 44 | return $this->queueManager->getConnection(); |
| 38 | 45 | } |
| 39 | 46 | } |
| 40 | 47 | |
| 41 | 48 | $this->matchType($class, $context, $connection); |
| 42 | - } catch (\Throwable $e) { |
|
| 49 | + } |
|
| 50 | + catch (\Throwable $e) |
|
| 51 | + { |
|
| 43 | 52 | throw new ContainerException(sprintf("Can't inject the required queue. %s", $e->getMessage()), 0, $e); |
| 44 | 53 | } |
| 45 | 54 | |
@@ -54,7 +63,8 @@ discard block |
||
| 54 | 63 | private function matchType(ReflectionClass $class, ?string $context, QueueInterface $connection): void |
| 55 | 64 | { |
| 56 | 65 | $className = $class->getName(); |
| 57 | - if ($className !== QueueInterface::class && !$connection instanceof $className) { |
|
| 66 | + if ($className !== QueueInterface::class && !$connection instanceof $className) |
|
| 67 | + { |
|
| 58 | 68 | throw new \RuntimeException( |
| 59 | 69 | \sprintf( |
| 60 | 70 | "The queue obtained by the context `%s` doesn't match the type `%s`.", |