public function handleMessage(Change $change, Message $message, Channel $channel) { if (!$change->getProduct()) { throw new \InvalidArgumentException("Badly routed message '{$message->content}' with routing key '{$message->routingKey}'."); } $product = $change->getProduct(); /** @var Eshop $eshop */ $eshop = $this->eshopRepository->getOneById($product->getEshopId()); $product->setEshop($eshop); $categoryIds = $product->getCategoryIds(); if (!empty($categoryIds)) { $product->setCategories($this->categoryRepository->find(["_id" => ['$in' => $product->getCategoryIds()]])); } if (!$this->elasticsearch->indices()->existsAlias(["name" => $this->catalogIndexAliasName])) { $this->initIndex(); } $response = $this->elasticsearch->index(["index" => $this->catalogIndexAliasName, "type" => ProductMeta::SHORT_NAME, "id" => (string) $product->getId(), "version" => $product->getV(), "version_type" => "external_gte", "body" => ProductMeta::toObject($product, "json:")]); $this->log->info("Indexed Product#{$product->getId()} (v={$product->getV()}, created=" . json_encode($response["created"]) . ")."); $channel->ack($message); }
/** * Serializes \CC15\MQ\VO\Change to object * * @param Change $object * @param string $group * * @throws \InvalidArgumentException * * @return object */ public static function toObject($object, $group = NULL) { if ($object === null) { return null; } if (!isset(self::$groups[$group])) { throw new \InvalidArgumentException('Group \'' . $group . '\' not supported for ' . 'CC15\\MQ\\VO\\Change' . '.'); } else { $id = self::$groups[$group]; } if (!$object instanceof Change) { throw new \InvalidArgumentException('You have to pass object of class CC15\\MQ\\VO\\Change.'); } $output = array(); if (($id & 1) > 0) { $output['product'] = ProductMeta::toObject($object->product, $group); } if (($id & 2) > 0 && isset($object->product)) { $output['product'] = ProductMeta::toObject($object->product, $group); } if (($id & 1) > 0) { $output['category'] = CategoryMeta::toObject($object->category, $group); } if (($id & 2) > 0 && isset($object->category)) { $output['category'] = CategoryMeta::toObject($object->category, $group); } return (object) $output; }