/**
  * @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;
 }
 /**
  * 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 Request $request
  *
  * @return JsonResponse
  */
 public function createAction(Request $request)
 {
     $product = $this->productBuilder->createProduct($request->request->get('identifier'), $request->request->get('family', null));
     $violations = $this->validator->validate($product);
     if (0 === $violations->count()) {
         $this->productSaver->save($product);
         $normalizationContext = $this->userContext->toArray() + ['filter_types' => ['pim.internal_api.product_value.view'], 'disable_grouping_separator' => true];
         return new JsonResponse($this->normalizer->normalize($product, 'internal_api', $normalizationContext));
     }
     $errors = ['values' => $this->normalizer->normalize($violations, 'internal_api', ['product' => $product])];
     return new JsonResponse($errors, 400);
 }
 /**
  * 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();
 }
 /**
  * Create product
  *
  * @param Request $request
  * @param string  $dataLocale
  *
  * @Template
  * @AclAncestor("pim_enrich_product_create")
  *
  * @return array
  */
 public function createAction(Request $request, $dataLocale)
 {
     if (!$request->isXmlHttpRequest()) {
         return $this->redirectToRoute('pim_enrich_product_index');
     }
     $product = $this->productBuilder->createProduct();
     $form = $this->createForm('pim_product_create', $product, $this->getCreateFormOptions($product));
     if ($request->isMethod('POST')) {
         $form->submit($request);
         if ($form->isValid()) {
             $this->productSaver->save($product);
             $this->addFlash('success', 'flash.product.created');
             if ($dataLocale === null) {
                 $dataLocale = $this->getDataLocaleCode();
             }
             $url = $this->generateUrl('pim_enrich_product_edit', ['id' => $product->getId(), 'dataLocale' => $dataLocale]);
             $response = ['status' => 1, 'url' => $url];
             return new Response(json_encode($response));
         }
     }
     return ['form' => $form->createView(), 'dataLocale' => $this->getDataLocaleCode()];
 }
 /**
  * {@inheritdoc}
  */
 protected function createEntity($class, array $data)
 {
     return $this->productBuilder->createProduct();
 }
 function it_merges_original_and_new_values(GroupInterface $variantGroup, ProductTemplateInterface $template, ProductBuilderInterface $productBuilder, ProductInterface $product, ProductValueInterface $identifier, ArrayCollection $values, \Iterator $valuesIterator)
 {
     $originalValues = ['description' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'data' => 'original description en_US'], ['locale' => 'de_DE', 'scope' => 'ecommerce', 'data' => 'original description de_DE']]];
     $newValues = ['description' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'data' => 'new description en_US'], ['locale' => 'fr_FR', 'scope' => 'ecommerce', 'data' => 'new description fr_FR']]];
     $expectedValues = ['description' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'data' => 'new description en_US'], ['locale' => 'de_DE', 'scope' => 'ecommerce', 'data' => 'original description de_DE'], ['locale' => 'fr_FR', 'scope' => 'ecommerce', 'data' => 'new description fr_FR']]];
     $variantGroup->getProductTemplate()->willReturn($template);
     $template->getValuesData()->willReturn($originalValues);
     $productBuilder->createProduct()->willReturn($product);
     $product->getValues()->willReturn($values);
     $product->getIdentifier()->willReturn($identifier);
     $values->removeElement($identifier)->shouldBeCalled();
     $values->getIterator()->willReturn($valuesIterator);
     $template->setValues($values)->shouldBeCalled();
     $template->setValuesData($expectedValues)->shouldBeCalled();
     $variantGroup->setProductTemplate($template)->shouldBeCalled();
     $this->setValues($variantGroup, $newValues);
 }