/** * {@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 update($category, array $data, array $options = []) { try { $this->categoryUpdater->update($category, $data, $options); if (isset($data['labels']) && $category instanceof TranslatableInterface) { $this->translatableUpdater->update($category, $data['labels']); } } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException($e->getMessage()); } return $this; }
/** * Fetch medias in local filesystem * * @param GroupInterface $variantGroup * @param string $directory */ protected function fetchMedia(GroupInterface $variantGroup, $directory) { if (null === ($productTemplate = $variantGroup->getProductTemplate())) { return; } $identifier = $variantGroup->getCode(); $this->variantGroupUpdater->update($variantGroup, ['values' => $productTemplate->getValuesData()]); $this->mediaFetcher->fetchAll($productTemplate->getValues(), $directory, $identifier); foreach ($this->mediaFetcher->getErrors() as $error) { $this->stepExecution->addWarning($error['message'], [], new DataInvalidItem($error['media'])); } }
/** * {@inheritdoc} */ public function process($item) { $convertedItem = $this->channelConverter->convert($item); $channel = $this->findOrCreateChannel($convertedItem); try { $this->updater->update($channel, $convertedItem); } catch (\InvalidArgumentException $exception) { $this->skipItemWithMessage($item, $exception->getMessage(), $exception); } $violations = $this->validator->validate($channel); if ($violations->count() > 0) { $this->skipItemWithConstraintViolations($item, $violations); } return $channel; }
/** * {@inheritdoc} */ public function process($item) { $convertedItem = $this->groupConverter->convert($item); $attributeGroup = $this->findOrCreateAttributeGroup($convertedItem); try { $this->updater->update($attributeGroup, $convertedItem); } catch (\InvalidArgumentException $exception) { $this->skipItemWithMessage($item, $exception->getMessage(), $exception); } $violations = $this->validator->validate($attributeGroup); if ($violations->count() > 0) { $this->skipItemWithConstraintViolations($item, $violations); } return $attributeGroup; }
/** * {@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; }
/** * Updates product with the provided request data * * @param ProductInterface $product * @param array $data */ protected function updateProduct(ProductInterface $product, array $data) { $values = $this->localizedConverter->convertToDefaultFormats($data['values'], ['locale' => $this->userContext->getUiLocale()->getCode()]); $values = $this->emptyValuesFilter->filter($product, $values); unset($data['values']); $data = array_replace($data, $values); $this->productUpdater->update($product, $data); }
/** * Transform an array of values to ProductValues * * @param array $arrayValues * * @return ArrayCollection */ protected function transformArrayToValues(array $arrayValues) { $product = $this->productBuilder->createProduct(); $this->productUpdater->update($product, $arrayValues); $values = $product->getValues(); $values->removeElement($product->getIdentifier()); return $values; }
/** * 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 ecommerce' * ], * [ * 'locale' => 'en_US', * 'scope' => 'mobile', * 'data' => 'The description for mobile' * ] * ] * ] * ] * * @param ProductInterface $product * @param array $actions * * @throws \LogicException * * @return ProductInterface $product */ protected function updateProduct(ProductInterface $product, array $actions) { $values = $this->prepareProductValues($product, $actions); if (empty($values)) { $this->stepExecution->incrementSummaryInfo('skipped_products'); $this->stepExecution->addWarning($this->getName(), 'pim_enrich.mass_edit_action.edit-common-attributes.message.no_valid_attribute', [], $product); return null; } $this->productUpdater->update($product, $values); return $product; }
/** * Apply current values to a fake product and test its integrity with the product validator. * If violations are raised, values are not valid. * * Errors are stored in json format to be useable by the Product Edit Form. * * @return bool */ public function hasValidValues() { $data = json_decode($this->values, true); $locale = $this->userContext->getUiLocale()->getCode(); $data = $this->localizedConverter->convertToDefaultFormats($data, ['locale' => $locale]); $product = $this->productBuilder->createProduct('FAKE_SKU_FOR_MASS_EDIT_VALIDATION_' . microtime()); $this->productUpdater->update($product, $data); $violations = $this->productValidator->validate($product); $violations->addAll($this->localizedConverter->getViolations()); $errors = ['values' => $this->internalNormalizer->normalize($violations, 'internal_api', ['product' => $product])]; $this->errors = json_encode($errors); return 0 === $violations->count(); }
/** * @param Request $request * @param string $code * * @throws NotFoundHttpException If product is not found or the user cannot see it * @throws AccessDeniedHttpException If the user does not have permissions to edit the product * * @return JsonResponse */ public function postAction(Request $request, $code) { $group = $this->groupRepository->findOneByIdentifier($code); if (null === $group) { throw new NotFoundHttpException(sprintf('Group with code "%s" not found', $code)); } $data = json_decode($request->getContent(), true); $this->updater->update($group, $data); $violations = $this->validator->validate($group); if (0 < $violations->count()) { $errors = $this->violationNormalizer->normalize($violations, 'internal_api', $this->userContext->toArray()); return new JsonResponse($errors, 400); } $this->saver->save($group); return new JsonResponse($this->normalizer->normalize($group, 'internal_api', $this->userContext->toArray())); }
/** * @param Request $request * @param string $code * * @throws NotFoundHttpException If product is not found or the user cannot see it * @throws AccessDeniedHttpException If the user does not have right to edit the product * * @return JsonResponse */ public function postAction(Request $request, $code) { $variantGroup = $this->repository->findOneByIdentifier($code); if (null === $variantGroup) { throw new NotFoundHttpException(sprintf('Variant group with code "%s" not found', $code)); } $data = json_decode($request->getContent(), true); $data = $this->convertLocalizedAttributes($data); $data = $this->variantGroupDataFilter->filterCollection($data, null); $this->updater->update($variantGroup, $data); $violations = $this->validator->validate($variantGroup); $violations->addAll($this->validator->validate($variantGroup->getProductTemplate())); $violations->addAll($this->attributeConverter->getViolations()); if (0 < $violations->count()) { $errors = $this->violationNormalizer->normalize($violations, 'internal_api', $this->userContext->toArray() + ['product' => $variantGroup->getProductTemplate()]); return new JsonResponse($errors, 400); } $this->saver->save($variantGroup, ['copy_values_to_products' => true]); return new JsonResponse($this->normalizer->normalize($variantGroup, 'internal_api', $this->userContext->toArray() + ['with_variant_group_values' => true])); }
/** * 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; }
/** * Update the group group fields * * @param GroupInterface $group * @param array $convertedItem */ protected function updateGroup(GroupInterface $group, array $convertedItem) { $this->groupUpdater->update($group, $convertedItem); }
/** * @param ProductInterface $product * @param array $filteredItem * * @throws \InvalidArgumentException */ protected function updateProduct(ProductInterface $product, array $filteredItem) { $this->updater->update($product, $filteredItem); }
/** * Update the category fields * * @param CategoryInterface $category * @param array $convertedItem */ protected function updateCategory(CategoryInterface $category, array $convertedItem) { $this->categoryUpdater->update($category, $convertedItem); }
/** * @param AssociationTypeInterface $associationType * @param array $convertedItem * * @throws \InvalidArgumentException */ protected function updateAssociationType(AssociationTypeInterface $associationType, array $convertedItem) { $this->updater->update($associationType, $convertedItem); }
/** * @param FamilyInterface $family * @param array $convertedItem * * @throws \InvalidArgumentException */ protected function updateFamily(FamilyInterface $family, array $convertedItem) { $this->updater->update($family, $convertedItem); }
/** * @param ProductInterface $product * @param array $convertItems * * @throws \InvalidArgumentException */ protected function updateProduct(ProductInterface $product, array $convertItems) { $this->updater->update($product, $convertItems); }
/** * @param AttributeInterface $attribute * @param array $convertedItem * * @throws \InvalidArgumentException */ protected function updateAttribute(AttributeInterface $attribute, array $convertedItem) { $this->updater->update($attribute, $convertedItem); }