/**
  * Find a product by its id or return a 404 response
  *
  * @param int $id the product id
  *
  * @throws NotFoundHttpException
  *
  * @return ProductInterface
  */
 protected function findProductOr404($id)
 {
     $product = $this->productRepository->findOneById($id);
     if (!$product) {
         throw new NotFoundHttpException(sprintf('Product with id %s could not be found.', $id));
     }
     $this->productBuilder->addMissingAssociations($product);
     return $product;
 }
 /**
  * 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;
 }
 /**
  * @param string      $identifier
  * @param string|null $familyCode
  *
  * @return ProductInterface
  */
 protected function findOrCreateProduct($identifier, $familyCode)
 {
     $product = $this->repository->findOneByIdentifier($identifier);
     if (!$product) {
         $product = $this->builder->createProduct($identifier, $familyCode);
     }
     return $product;
 }
 /**
  * Find a product by its id or return a 404 response
  *
  * @param int $id the product id
  *
  * @throws NotFoundHttpException
  *
  * @return ProductInterface
  */
 protected function findProductOr404($id)
 {
     $product = $this->productRepository->findOneByWithValues($id);
     if (!$product) {
         throw $this->createNotFoundException(sprintf('Product with id %s could not be found.', (string) $id));
     }
     // With this version of the form we need to add missing values from family
     $this->productBuilder->addMissingProductValues($product);
     $this->productBuilder->addMissingAssociations($product);
     return $product;
 }
 /**
  * Returns a ProductValue
  *
  * @param ProductInterface    $product
  * @param ColumnInfoInterface $columnInfo
  *
  * @return ProductValueInterface
  */
 protected function getProductValue(ProductInterface $product, ColumnInfoInterface $columnInfo)
 {
     $productValue = $product->getValue($columnInfo->getName(), $columnInfo->getLocale(), $columnInfo->getScope());
     if (null === $productValue) {
         $attribute = $columnInfo->getAttribute();
         $locale = $columnInfo->getLocale();
         $scope = $columnInfo->getScope();
         $productValue = $this->productBuilder->createProductValue($attribute, $locale, $scope);
         $product->addValue($productValue);
     }
     return $productValue;
 }
 /**
  * Remove an optional attribute form a product
  *
  * @param int $productId   The product id
  * @param int $attributeId The attribute id
  *
  * @AclAncestor("pim_enrich_product_remove_attribute")
  *
  * @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
  * @throws BadRequestHttpException   If the attribute is not removable
  *
  * @return JsonResponse
  */
 public function removeAttributeAction($productId, $attributeId)
 {
     $product = $this->findProductOr404($productId);
     if ($this->objectFilter->filterObject($product, 'pim.internal_api.product.edit')) {
         throw new AccessDeniedHttpException();
     }
     $attribute = $this->findAttributeOr404($attributeId);
     if (!$product->isAttributeRemovable($attribute)) {
         throw new BadRequestHttpException();
     }
     $this->productBuilder->removeAttributeFromProduct($product, $attribute);
     $this->productSaver->save($product, ['recalculate' => false, 'schedule' => false]);
     return new JsonResponse();
 }
 /**
  * Add all the values required by the given attribute
  *
  * @param AttributeInterface $attribute
  * @param LocaleInterface    $locale
  */
 protected function addValues(AttributeInterface $attribute, $locale)
 {
     if ($attribute->isScopable()) {
         foreach ($locale->getChannels() as $channel) {
             $key = $attribute->getCode() . '_' . $channel->getCode();
             $value = $this->productBuilder->createProductValue($attribute, $locale->getCode(), $channel->getCode());
             $this->productBuilder->addMissingPrices($value);
             $this->values[$key] = $value;
         }
     } else {
         $value = $this->productBuilder->createProductValue($attribute, $locale->getCode());
         $this->productBuilder->addMissingPrices($value);
         $this->values[$attribute->getCode()] = $value;
     }
 }
 /**
  * Add missing associations (if association type has been added after the last processing)
  *
  * @param ProductInterface $product
  */
 protected function addMissingAssociations(ProductInterface $product)
 {
     $this->productBuilder->addMissingAssociations($product);
 }