/**
  * {@inheritdoc}
  */
 public function normalize($value, $format = null, array $context = [])
 {
     if (!$this->normalizer instanceof NormalizerInterface) {
         throw new \LogicException('Serializer must be a normalizer');
     }
     if (null === $value->getData()) {
         return null;
     }
     $productId = $context[ProductNormalizer::MONGO_ID];
     $productCollection = $context[ProductNormalizer::MONGO_COLLECTION_NAME];
     $data = [];
     $data['_id'] = $this->mongoFactory->createMongoId();
     $data['attribute'] = $value->getAttribute()->getId();
     $data['entity'] = $this->mongoFactory->createMongoDBRef($productCollection, $productId);
     if (null !== $value->getLocale()) {
         $data['locale'] = $value->getLocale();
     }
     if (null !== $value->getScope()) {
         $data['scope'] = $value->getScope();
     }
     $backendType = $value->getAttribute()->getBackendType();
     if ('options' !== $backendType) {
         $data[$backendType] = $this->normalizeValueData($value->getData(), $backendType, $context);
     } else {
         $data['optionIds'] = $this->normalizeValueData($value->getData(), $backendType, $context);
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  *
  * Override to do a massive save for the products
  */
 public function saveAll(array $products, array $options = [])
 {
     if (empty($products)) {
         return;
     }
     $this->collection = $this->objectManager->getDocumentCollection($this->productClass);
     $this->eventDispatcher->dispatch(StorageEvents::PRE_SAVE_ALL, new GenericEvent($products));
     $allOptions = $this->optionsResolver->resolveSaveAllOptions($options);
     // TODO check the "schedule" options to remove the completeness or not
     // TODO manage the "recalculate" options
     $productsToInsert = [];
     $productsToUpdate = [];
     foreach ($products as $product) {
         if (null === $product->getId()) {
             $productsToInsert[] = $product;
             $product->setId($this->mongoFactory->createMongoId());
         } else {
             $productsToUpdate[] = $product;
         }
     }
     $insertDocs = $this->getDocsFromProducts($productsToInsert);
     $updateDocs = $this->getDocsFromProducts($productsToUpdate);
     if (count($insertDocs) > 0) {
         $this->insertDocuments($insertDocs);
     }
     if (count($updateDocs) > 0) {
         $this->updateDocuments($updateDocs);
     }
     $this->versionPersister->bulkPersist($products);
     $this->eventDispatcher->dispatch(StorageEvents::POST_SAVE_ALL, new GenericEvent($products));
 }
 /**
  * {@inheritdoc}
  *
  * Override to do a massive save for the products
  */
 public function saveAll(array $products, array $options = [])
 {
     if (empty($products)) {
         return;
     }
     $options = $this->optionsResolver->resolveSaveAllOptions($options);
     $this->eventDispatcher->dispatch(StorageEvents::PRE_SAVE_ALL, new GenericEvent($products, $options));
     $productsToInsert = [];
     $productsToUpdate = [];
     foreach ($products as $product) {
         if (null === $product->getId()) {
             $productsToInsert[] = $product;
             $product->setId($this->mongoFactory->createMongoId());
         } else {
             $productsToUpdate[] = $product;
         }
         if (true === $options['schedule'] || true === $options['recalculate']) {
             $this->completenessManager->schedule($product);
         }
         if (true === $options['recalculate']) {
             $this->completenessManager->generateMissingForProduct($product);
         }
     }
     $insertDocs = $this->getDocsFromProducts($productsToInsert);
     $updateDocs = $this->getDocsFromProducts($productsToUpdate);
     if (count($insertDocs) > 0) {
         $this->insertDocuments($insertDocs);
     }
     if (count($updateDocs) > 0) {
         $this->updateDocuments($updateDocs);
     }
     $versions = $this->bulkVersionBuilder->buildVersions($products);
     $this->versionSaver->saveAll($versions);
     $this->eventDispatcher->dispatch(StorageEvents::POST_SAVE_ALL, new GenericEvent($products, $options));
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($product, $format = null, array $context = [])
 {
     if (!$this->normalizer instanceof NormalizerInterface) {
         throw new \LogicException('Serializer must be a normalizer');
     }
     $data = [];
     if (null !== $product->getId()) {
         $data[self::MONGO_ID] = $this->mongoFactory->createMongoId($product->getId());
     } else {
         $data[self::MONGO_ID] = $this->mongoFactory->createMongoId();
     }
     $context[self::MONGO_ID] = $data[self::MONGO_ID];
     if (null !== $product->getCreated()) {
         $data['created'] = $this->normalizer->normalize($product->getCreated(), self::FORMAT, $context);
     } else {
         $data['created'] = $this->mongoFactory->createMongoDate();
     }
     $data['updated'] = $this->mongoFactory->createMongoDate();
     if (null !== $product->getFamily()) {
         $data['family'] = $product->getFamily()->getId();
     }
     $data['enabled'] = $product->isEnabled();
     $data['groupIds'] = $this->normalizeGroups($product->getGroups());
     $data['categoryIds'] = $this->normalizeCategories($product->getCategories());
     $data['associations'] = $this->normalizeAssociations($product->getAssociations(), $context);
     $data['values'] = $this->normalizeValues($product->getValues(), $context);
     $data['normalizedData'] = $this->normalizer->normalize($product, 'mongodb_json');
     $data['completenesses'] = [];
     unset($data['normalizedData']['completenesses']);
     return $data;
 }
 /**
  * Get the products ids as an array of MongoDBRef
  *
  * @param Collection|ProductInterface[] $products
  * @param string                        $productCollection
  *
  * @return array
  */
 protected function normalizeProducts($products, $productCollection)
 {
     $data = [];
     foreach ($products as $product) {
         $data[] = $this->mongoFactory->createMongoDBRef($productCollection, $product->getId());
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($media, $format = null, array $context = [])
 {
     $data = [];
     $data['_id'] = $this->mongoFactory->createMongoId();
     $data['filename'] = $media->getFilename();
     $data['originalFilename'] = $media->getOriginalFilename();
     $data['mimeType'] = $media->getMimeType();
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($price, $format = null, array $context = [])
 {
     $data = [];
     $data['_id'] = $this->mongoFactory->createMongoId();
     $data['currency'] = $price->getCurrency();
     if (null !== $price->getData()) {
         $data['data'] = (double) $price->getData();
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($metric, $format = null, array $context = [])
 {
     $data = ['_id' => $this->mongoFactory->createMongoId(), 'family' => $metric->getFamily()];
     if (null === $metric->getData() || '' === $metric->getData() || null === $metric->getUnit() || '' === $metric->getUnit()) {
         return $data;
     }
     $this->createMetricBaseValues($metric);
     $data['unit'] = $metric->getUnit();
     $data['data'] = $metric->getData();
     $data['baseUnit'] = $metric->getBaseUnit();
     $data['baseData'] = $metric->getBaseData();
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function write(array $products)
 {
     $this->collection = $this->documentManager->getDocumentCollection($this->productClass);
     $productsToInsert = [];
     $productsToUpdate = [];
     foreach ($products as $product) {
         if (null === $product->getId()) {
             $productsToInsert[] = $product;
             $product->setId($this->mongoFactory->createMongoId());
         } else {
             $productsToUpdate[] = $product;
         }
     }
     $this->eventDispatcher->dispatch(self::PRE_INSERT, new GenericEvent($productsToInsert));
     $this->eventDispatcher->dispatch(self::PRE_UPDATE, new GenericEvent($productsToUpdate));
     $insertDocs = $this->getDocsFromProducts($productsToInsert);
     $updateDocs = $this->getDocsFromProducts($productsToUpdate);
     if (count($insertDocs) > 0) {
         $this->insertDocuments($insertDocs);
     }
     if (count($updateDocs) > 0) {
         $this->updateDocuments($updateDocs);
     }
     $this->pendingPersister->persistPendingVersions($products);
     $this->eventDispatcher->dispatch(self::POST_INSERT, new GenericEvent($productsToInsert));
     $this->eventDispatcher->dispatch(self::POST_UPDATE, new GenericEvent($productsToUpdate));
     $this->documentManager->clear();
     $this->cacheClearer->clear();
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($version, $format = null, array $context = [])
 {
     if (!$this->normalizer instanceof NormalizerInterface) {
         throw new \LogicException('Serializer must be a normalizer');
     }
     $data = [];
     if (null !== $version->getId()) {
         $data[self::MONGO_ID] = $this->mongoFactory->createMongoId($version->getId());
     } else {
         $data[self::MONGO_ID] = $this->mongoFactory->createMongoId();
     }
     $data['author'] = $version->getAuthor();
     $data['resourceName'] = $version->getResourceName();
     $data['resourceId'] = (string) $version->getResourceId();
     $data['snapshot'] = $version->getSnapshot();
     $data['changeset'] = $version->getChangeset();
     $data['context'] = $version->getContext();
     $data['version'] = $version->getVersion();
     $data['loggedAt'] = $this->normalizer->normalize($version->getLoggedAt(), self::FORMAT);
     $data['pending'] = $version->isPending();
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($dateTime, $format = null, array $context = [])
 {
     return $this->mongoFactory->createMongoDate($dateTime->getTimestamp());
 }