Example #1
0
 public function handleMessage($consumer, Consumer $consumerSpec, $meta = null, Message $message, Channel $channel, Client $client)
 {
     $data = $message->content;
     if ($meta) {
         switch ($message->getHeader("content-type")) {
             case ContentTypes::APPLICATION_JSON:
                 if ($meta instanceof JsonMetaInterface) {
                     $data = $meta->fromJson($data);
                 } else {
                     throw new BunnyException("Meta class does not support JSON.");
                 }
                 break;
             case ContentTypes::APPLICATION_PROTOBUF:
                 if ($meta instanceof ProtobufMetaInterface) {
                     $data = $meta->fromProtobuf($data);
                 } else {
                     throw new BunnyException("Meta class does not support Protobuf.");
                 }
                 break;
             default:
                 throw new BunnyException("Message does not have 'content-type' header, cannot deserialize data.");
         }
     }
     $consumer->{$consumerSpec->method}($data, $message, $channel, $client);
     if ($consumerSpec->maxMessages !== null && ++$this->messages >= $consumerSpec->maxMessages) {
         $client->stop();
     }
 }
Example #2
0
 private function consume(Message $msg, InputInterface $input, OutputInterface $output)
 {
     $content = $msg->content;
     if ($input->getOption('format') && '' !== ($contentType = strtolower($msg->getHeader('content-type', '')))) {
         if ('application/json' === strtolower($contentType)) {
             if ("null" !== ($pretty = json_encode(json_decode($content, true), JSON_PRETTY_PRINT))) {
                 $content = $pretty;
             } else {
                 return $output->writeln("<bg=red>Expected JSON payload, received: {$content}</>");
             }
         }
     }
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $output->writeln(print_r($msg, true));
     } else {
         $cKey = "{$msg->exchange}:{$msg->routingKey}";
         if (array_key_exists($cKey, $this->colourLookup)) {
             $output->writeln(sprintf("<{$this->colourLookup[$cKey]}>%s</>", $content));
         } else {
             $output->writeln("{$cKey} > {$content}");
         }
     }
 }