|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ApacheBorys\Retry; |
| 6 | + |
| 7 | +use ApacheBorys\Retry\Entity\Config; |
| 8 | +use ApacheBorys\Retry\HandlerExceptionDeclarator\StandardHandlerExceptionDeclarator; |
| 9 | +use ApacheBorys\Retry\Interfaces\Executor; |
| 10 | +use ApacheBorys\Retry\Interfaces\HandlerExceptionDeclaratorInterface; |
| 11 | +use ApacheBorys\Retry\Interfaces\Transport; |
| 12 | +use ApacheBorys\Retry\Traits\LogWrapper; |
| 13 | +use Psr\Container\ContainerExceptionInterface; |
| 14 | +use Psr\Container\ContainerInterface; |
| 15 | +use Psr\Container\NotFoundExceptionInterface; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Psr\Log\LogLevel; |
| 18 | + |
| 19 | +class HandlerFactory |
| 20 | +{ |
| 21 | + use LogWrapper; |
| 22 | + |
| 23 | + private array $config; |
| 24 | + |
| 25 | + private ?LoggerInterface $logger; |
| 26 | + |
| 27 | + public function __construct(array $config, LoggerInterface $logger = null) |
| 28 | + { |
| 29 | + $this->config = $config; |
| 30 | + $this->logger = $logger; |
| 31 | + } |
| 32 | + |
| 33 | + public function createExceptionHandler(?ContainerInterface $container = null): ExceptionHandler |
| 34 | + { |
| 35 | + return new ExceptionHandler($this->compileConfig($container), $this->instantiateDeclarator($container), $this->logger); |
| 36 | + } |
| 37 | + |
| 38 | + public function createMessageHandler(?ContainerInterface $container = null): MessageHandler |
| 39 | + { |
| 40 | + return new MessageHandler($this->compileConfig($container), $this->instantiateDeclarator($container), $this->logger); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return Config[] |
| 45 | + * @psalm-suppress ArgumentTypeCoercion |
| 46 | + * @psalm-suppress PropertyTypeCoercion |
| 47 | + */ |
| 48 | + private function compileConfig(?ContainerInterface $container): array |
| 49 | + { |
| 50 | + $result = []; |
| 51 | + |
| 52 | + foreach ($this->config['items'] as $retryName => $configNode) { |
| 53 | + $result[$retryName] = new Config( |
| 54 | + (string) $retryName, |
| 55 | + (string) $configNode['exception'], |
| 56 | + (int) $configNode['maxRetries'], |
| 57 | + $configNode['formula'], |
| 58 | + $this->instantiateClass( |
| 59 | + $configNode['transport']['class'], |
| 60 | + $configNode['transport']['arguments'] ?? [], |
| 61 | + Transport::class, |
| 62 | + $container |
| 63 | + ), |
| 64 | + $this->instantiateClass( |
| 65 | + $configNode['executor']['class'], |
| 66 | + $configNode['executor']['arguments'] ?? [], |
| 67 | + Executor::class, |
| 68 | + $container |
| 69 | + ) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + $this->sendLogRecordInitConfigs(count($result)); |
| 74 | + |
| 75 | + return $result; |
| 76 | + } |
| 77 | + |
| 78 | + private function instantiateDeclarator(?ContainerInterface $container): HandlerExceptionDeclaratorInterface |
| 79 | + { |
| 80 | + /** @var HandlerExceptionDeclaratorInterface $declarator */ |
| 81 | + $declarator = $this->instantiateClass( |
| 82 | + $this->config['handlerExceptionDeclarator']['class'] ?? StandardHandlerExceptionDeclarator::class, |
| 83 | + $this->config['handlerExceptionDeclarator']['arguments'] ?? [], |
| 84 | + HandlerExceptionDeclaratorInterface::class, |
| 85 | + $container |
| 86 | + ); |
| 87 | + |
| 88 | + $this->sendLogRecordInitDeclarator(get_class($declarator)); |
| 89 | + |
| 90 | + return $declarator; |
| 91 | + } |
| 92 | + |
| 93 | + private function sendLogRecordInitDeclarator(string $declaratorClass): void |
| 94 | + { |
| 95 | + $this->sendLogRecord( |
| 96 | + LogLevel::INFO, |
| 97 | + sprintf('Init for %s with handler exception declarator %s', get_class($this), $declaratorClass) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + private function sendLogRecordInitConfigs(int $qty): void |
| 102 | + { |
| 103 | + $this->sendLogRecord(LogLevel::INFO, sprintf('Init for %s configs. Quantity: %d', get_class($this), $qty)); |
| 104 | + } |
| 105 | + |
| 106 | + private function compileArguments(array $arguments, ?ContainerInterface $container): array |
| 107 | + { |
| 108 | + $result = []; |
| 109 | + |
| 110 | + foreach ($arguments as $arg) { |
| 111 | + if (is_string($arg)) { |
| 112 | + $tempResult = $this->checkClassInContainer($arg, $container); |
| 113 | + |
| 114 | + if (!is_null($tempResult)) { |
| 115 | + $result[] = $tempResult; |
| 116 | + continue; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (is_array($arg) && isset($arg['class'], $arg['arguments'])) { |
| 121 | + $result[] = new $arg['class'](...$this->compileArguments($arg['arguments'], $container)); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $result[] = $arg; |
| 126 | + } |
| 127 | + |
| 128 | + return $result; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @return HandlerExceptionDeclaratorInterface|Transport|Executor |
| 133 | + */ |
| 134 | + private function instantiateClass( |
| 135 | + string $class, |
| 136 | + array $arguments, |
| 137 | + string $shouldBeInstanceOf, |
| 138 | + ?ContainerInterface $container |
| 139 | + ): object { |
| 140 | + $result = $this->checkClassInContainer($class, $container) ?? new $class(...$this->compileArguments($arguments, $container)); |
| 141 | + |
| 142 | + if (!($result instanceof $shouldBeInstanceOf)) { |
| 143 | + throw new \LogicException( |
| 144 | + sprintf('The generated class %s is not an inctance of %', get_class($result), $shouldBeInstanceOf) |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + return $result; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @throws ContainerExceptionInterface |
| 153 | + * @throws NotFoundExceptionInterface |
| 154 | + */ |
| 155 | + private function checkClassInContainer(string $class, ?ContainerInterface $container): ?object |
| 156 | + { |
| 157 | + if ($class[0] === '@') { |
| 158 | + if ($container instanceof ContainerInterface && $container->has(substr($class, 1))) { |
| 159 | + return $container->get(substr($class, 1)); |
| 160 | + } else { |
| 161 | + throw new \LogicException( |
| 162 | + sprintf( |
| 163 | + 'Can\'t get %s instance from container to instantiate Transport or Executor', |
| 164 | + substr($class, 1) |
| 165 | + ) |
| 166 | + ); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return null; |
| 171 | + } |
| 172 | +} |
0 commit comments