This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Mgid\KafkaBundle\Command; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use JsonException; |
||
| 7 | use Mgid\KafkaBundle\DependencyInjection\Traits\{ConsumerTrait, LoggerTrait}; |
||
| 8 | use RdKafka\Message; |
||
| 9 | use Symfony\Component\Console; |
||
| 10 | use Throwable; |
||
| 11 | |||
| 12 | abstract class Consumer extends Console\Command\Command |
||
| 13 | { |
||
| 14 | use ConsumerTrait, LoggerTrait; |
||
| 15 | |||
| 16 | public const QUEUE_NAME = 'topic_name'; |
||
| 17 | public const TIMEOUT_OPTION = 'timeout'; |
||
| 18 | |||
| 19 | private bool $isAlive = true; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | final public function __construct() |
||
| 25 | { |
||
| 26 | $name = (string) \preg_replace('#consumer$#i', '', static::class); |
||
| 27 | $name = \mb_strtolower((string) \preg_replace('#([a-z])([A-Z])#', '$1-$2', \strtr($name, ['\\' => ':']))); |
||
| 28 | |||
| 29 | parent::__construct($name); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | final protected function configure(): void |
||
| 36 | { |
||
| 37 | $this->addOption( |
||
| 38 | static::TIMEOUT_OPTION, |
||
| 39 | null, |
||
| 40 | Console\Input\InputOption::VALUE_OPTIONAL, |
||
| 41 | 'Consuming timeout', |
||
| 42 | \Mgid\KafkaBundle\Consumer\Consumer::DEFAULT_TIMEOUT |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | * |
||
| 49 | * @throws Throwable |
||
| 50 | */ |
||
| 51 | final public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int |
||
| 52 | { |
||
| 53 | $this->addSignalHandler(); |
||
| 54 | |||
| 55 | $this->consumer->subscribe([static::QUEUE_NAME]); |
||
| 56 | |||
| 57 | $timeout = $this->getTimeout($input); |
||
| 58 | |||
| 59 | while ($this->isAlive) { |
||
| 60 | $this->handle($this->consumer->consume($timeout)); |
||
| 61 | } |
||
| 62 | |||
| 63 | return self::SUCCESS; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param Message $message |
||
| 68 | * |
||
| 69 | * @throws Throwable |
||
| 70 | */ |
||
| 71 | protected function handle(Message $message): void |
||
| 72 | { |
||
| 73 | switch ($message->err) { |
||
| 74 | case \RD_KAFKA_RESP_ERR_NO_ERROR: |
||
| 75 | try { |
||
| 76 | $this->onMessage($this->getPayload($message)); |
||
| 77 | } catch (Exception $exception) { |
||
| 78 | $this->onException($message, $exception); |
||
| 79 | } |
||
| 80 | break; |
||
| 81 | case \RD_KAFKA_RESP_ERR__TIMED_OUT: |
||
| 82 | $this->onTimeout($message); |
||
| 83 | break; |
||
| 84 | case \RD_KAFKA_RESP_ERR__PARTITION_EOF: |
||
| 85 | $this->onEnd($message); |
||
| 86 | break; |
||
| 87 | default: |
||
| 88 | $this->onError($message); |
||
| 89 | $this->stop($message->err); |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param array<mixed> $data |
||
| 96 | * |
||
| 97 | * @throws Throwable |
||
| 98 | */ |
||
| 99 | abstract protected function onMessage(array $data): void; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param Message $message |
||
| 103 | * @param Exception $exception |
||
| 104 | */ |
||
| 105 | protected function onException(Message $message, Exception $exception): void |
||
| 106 | { |
||
| 107 | $this->logger->error($exception->getMessage(), ['payload' => $message->payload]); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param Message $message |
||
| 112 | */ |
||
| 113 | protected function onEnd(Message $message): void |
||
| 114 | { |
||
| 115 | $this->logger->debug($message->errstr(), ['code' => $message->err]); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param Message $message |
||
| 120 | */ |
||
| 121 | protected function onTimeout(Message $message): void |
||
| 122 | { |
||
| 123 | $this->logger->debug($message->errstr(), ['code' => $message->err]); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param Message $message |
||
| 128 | */ |
||
| 129 | protected function onError(Message $message): void |
||
| 130 | { |
||
| 131 | $this->logger->error($message->errstr(), ['code' => $message->err, 'payload' => $message->payload]); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param Message $message |
||
| 136 | * |
||
| 137 | * @return array<mixed> |
||
| 138 | * |
||
| 139 | * @throws Exception|JsonException |
||
| 140 | */ |
||
| 141 | protected function getPayload(Message $message): array |
||
| 142 | { |
||
| 143 | return \json_decode($message->payload, true, 512, \JSON_THROW_ON_ERROR); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param int $signal |
||
| 148 | * |
||
| 149 | * @throws \RdKafka\Exception |
||
| 150 | */ |
||
| 151 | final protected function stop(int $signal = 0): void |
||
| 152 | { |
||
| 153 | $this->isAlive = false; |
||
| 154 | |||
| 155 | $this->consumer->unsubscribe(); |
||
| 156 | |||
| 157 | $this->logger->info('Process termination', ['signal' => $signal]); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param Console\Input\InputInterface $input |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | final private function getTimeout(Console\Input\InputInterface $input): int |
||
| 166 | { |
||
| 167 | /** @var string $timeout */ |
||
| 168 | $timeout = $input->getOption(static::TIMEOUT_OPTION); |
||
| 169 | |||
| 170 | return (int) $timeout; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Consumer stopping handler. |
||
| 175 | * |
||
| 176 | * @throws \RdKafka\Exception |
||
| 177 | */ |
||
| 178 | final private function addSignalHandler(): void |
||
| 179 | { |
||
| 180 | $handler = function (int $signal) { |
||
| 181 | $this->stop($signal); |
||
| 182 | }; |
||
| 183 | |||
| 184 | \pcntl_async_signals(true); |
||
| 185 | \pcntl_signal(\SIGINT, $handler); |
||
| 186 | \pcntl_signal(\SIGTERM, $handler); |
||
| 187 | } |
||
| 188 | } |
||
| 189 |