/** * {@inheritdoc} */ public function process($item) { $entity = $this->findOrCreateObject($item); try { $this->updater->update($entity, $item); } catch (\InvalidArgumentException $exception) { $this->skipItemWithMessage($item, $exception->getMessage(), $exception); } $violations = $this->validator->validate($entity); if ($violations->count() > 0) { $this->objectDetacher->detach($entity); $this->skipItemWithConstraintViolations($item, $violations); } $rawParameters = $entity->getRawParameters(); if (!empty($rawParameters)) { $job = $this->jobRegistry->get($entity->getJobName()); $parameters = $this->jobParamsFactory->create($job, $rawParameters); $violations = $this->jobParamsValidator->validate($job, $parameters); if ($violations->count() > 0) { $this->objectDetacher->detach($entity); $this->skipItemWithConstraintViolations($item, $violations); } } return $entity; }
/** * {@inheritdoc} */ public function purge(array $options = []) { $versionsToPurge = []; $versionsPurgedCount = 0; $optionResolver = new OptionsResolver(); $this->configureOptions($optionResolver); $options = $optionResolver->resolve($options); $versionsCursor = $this->versionRepository->findPotentiallyPurgeableBy($options); foreach ($versionsCursor as $version) { $this->eventDispatcher->dispatch(PurgeVersionEvents::PRE_ADVISEMENT, new PreAdvisementVersionEvent($version)); if ($this->isVersionPurgeable($version, $options)) { $this->eventDispatcher->dispatch(PurgeVersionEvents::PRE_PURGE, new PrePurgeVersionEvent($version)); $versionsPurgedCount++; $versionsToPurge[] = $version; if (count($versionsToPurge) >= self::BULK_THRESHOLD) { $this->versionRemover->removeAll($versionsToPurge); $this->objectDetacher->detachAll($versionsToPurge); $versionsToPurge = []; } } else { $this->objectDetacher->detach($version); } } $this->versionRemover->removeAll($versionsToPurge); $this->objectDetacher->detachAll($versionsToPurge); return $versionsPurgedCount; }
/** * {@inheritdoc} */ public function process($variantGroup) { $variantGroupStandard = $this->normalizer->normalize($variantGroup, null, ['with_variant_group_values' => true, 'identifier' => $variantGroup->getCode()]); $parameters = $this->stepExecution->getJobParameters(); if ($parameters->has('with_media') && $parameters->get('with_media')) { $directory = $this->stepExecution->getJobExecution()->getExecutionContext()->get(JobInterface::WORKING_DIRECTORY_PARAMETER); $this->fetchMedia($variantGroup, $directory); } $this->objectDetacher->detach($variantGroup); return $variantGroupStandard; }
/** * {@inheritdoc} */ public function process($item) { $entity = $this->findOrCreateObject($item); try { $this->updater->update($entity, $item); } catch (\InvalidArgumentException $exception) { $this->skipItemWithMessage($item, $exception->getMessage(), $exception); } $violations = $this->validate($entity); if ($violations->count() > 0) { $this->objectDetacher->detach($entity); $this->skipItemWithConstraintViolations($item, $violations); } return $entity; }
/** * {@inheritdoc} */ public function process($product) { $this->initSecurityContext($this->stepExecution); $this->productBuilder->addMissingProductValues($product); $parameters = $this->stepExecution->getJobParameters(); $normalizerContext = $this->getNormalizerContext($parameters); $productStandard = $this->normalizer->normalize($product, 'json', $normalizerContext); if ($this->areAttributesToFilter($parameters)) { $productStandard = $this->filterProperties($productStandard, $parameters->get('selected_properties')); } if ($parameters->has('with_media') && $parameters->get('with_media')) { $directory = $this->stepExecution->getJobExecution()->getExecutionContext()->get(JobInterface::WORKING_DIRECTORY_PARAMETER); $this->fetchMedia($product, $directory); } $this->detacher->detach($product); return $productStandard; }
/** * @param ProductInterface[] $products * * @return array ['products' => ProductInterface[], 'violations' => []] */ protected function validateProducts(array $products) { $validProducts = $products; $productViolations = []; // TODO add a service to format violation constraint in the same way foreach ($products as $productIndex => $product) { $violations = $this->productValidator->validate($product); $productIdentifier = (string) $product->getIdentifier(); if ($violations->count() !== 0) { $this->productDetacher->detach($product); unset($validProducts[$productIndex]); $productViolations[$productIdentifier] = []; foreach ($violations as $violation) { $productViolations[$productIdentifier][] = sprintf("%s : %s", $violation->getMessage(), $violation->getInvalidValue()); } } } return ['products' => $validProducts, 'violations' => $productViolations]; }
/** * {@inheritdoc} */ public function process($product) { $parameters = $this->stepExecution->getJobParameters(); $structure = $parameters->get('filters')['structure']; $channel = $this->channelRepository->findOneByIdentifier($structure['scope']); $this->productBuilder->addMissingProductValues($product, [$channel], $channel->getLocales()->toArray()); $productStandard = $this->normalizer->normalize($product, 'json', ['channels' => [$channel->getCode()], 'locales' => array_intersect($channel->getLocaleCodes(), $parameters->get('filters')['structure']['locales'])]); if ($this->areAttributesToFilter($parameters)) { $attributesToFilter = $this->getAttributesToFilter($parameters); $productStandard['values'] = $this->filterValues($productStandard['values'], $attributesToFilter); } if ($parameters->has('with_media') && $parameters->get('with_media')) { $directory = $this->stepExecution->getJobExecution()->getExecutionContext()->get(JobInterface::WORKING_DIRECTORY_PARAMETER); $this->fetchMedia($product, $directory); } else { $mediaAttributes = $this->attributeRepository->findMediaAttributeCodes(); $productStandard['values'] = array_filter($productStandard['values'], function ($attributeCode) use($mediaAttributes) { return !in_array($attributeCode, $mediaAttributes); }, ARRAY_FILTER_USE_KEY); } $this->detacher->detach($product); return $productStandard; }
/** * {@inheritdoc} */ public function process($product) { if (null !== $this->productBuilder) { $this->productBuilder->addMissingProductValues($product); } $data['media'] = []; $productMedias = $this->getProductMedias($product); if (count($productMedias) > 0) { try { $data['media'] = $this->serializer->normalize($productMedias, 'flat', ['field_name' => 'media', 'prepare_copy' => true]); } catch (FileNotFoundException $e) { throw new InvalidItemException($e->getMessage(), ['item' => $product->getIdentifier()->getData(), 'uploadDirectory' => $this->uploadDirectory]); } } $data['product'] = $this->serializer->normalize($product, 'flat', $this->getNormalizerContext()); $this->objectDetacher->detach($product); return $data; }
/** * Set data from $actions to the given $product * * Actions should look like that * * $actions = * [ * 'normalized_values' => [ * 'name' => [ * [ * 'locale' => null, * 'scope' => null, * 'data' => 'The name' * ] * ], * 'description' => [ * [ * 'locale' => 'en_US', * 'scope' => 'ecommerce', * 'data' => 'The description for en_US ecommerce' * ] * ] * ] * ] * * @param ProductInterface $product * @param array $actions * * @throws \LogicException * * @return ProductInterface $product */ protected function updateProduct(ProductInterface $product, array $actions) { $normalizedValues = json_decode($actions['normalized_values'], true); $filteredValues = []; foreach ($normalizedValues as $attributeCode => $values) { /** * We don't call that method directly on the product model because it hydrates * lot of models and it causes memory leak... */ if ($this->isAttributeEditable($product, $attributeCode)) { $filteredValues[$attributeCode] = $values; } } if (empty($filteredValues)) { $this->stepExecution->incrementSummaryInfo('skipped_products'); $this->addWarning($product); $this->productDetacher->detach($product); return null; } $this->productUpdater->update($product, $filteredValues); return $product; }
/** * Detaches the product from the unit of work is the responsibility of the writer but in this case we * want ensure that an updated and invalid product will not be used in the association processor. * Also we don't want to keep skipped products in memory * * @param ProductInterface $product */ protected function detachProduct(ProductInterface $product) { $this->detacher->detach($product); }
/** * Detaches the object from the unit of work. * * Detach an object from the UOW is the responsibility of the writer, but to do so, it should know the * skipped items or we should use an explicit persist strategy * * @param mixed $object */ protected function detachObject($object) { $this->detacher->detach($object); }
/** * @param array $productsPage */ protected function detachProducts(array $productsPage) { foreach ($productsPage as $product) { $this->objectDetacher->detach($product); } }
/** * {@inheritdoc} */ public function process($product) { $normalizedProduct = parent::process($product); $this->objectDetacher->detach($product); return $normalizedProduct; }
/** * {@inheritdoc} */ public function process($item) { $normalizedItem = $this->normalizer->normalize($item); $this->objectDetacher->detach($item); return $normalizedItem; }