function it_sets_invalid_values_to_attributes($validator, $productUpdater, AttributeInterface $attribute, AttributeRepositoryInterface $attributeRepository, ProductInterface $product, ConstraintViolationListInterface $violations, StepExecution $stepExecution, JobConfigurationRepositoryInterface $jobConfigurationRepo, JobExecution $jobExecution, JobConfigurationInterface $jobConfiguration)
 {
     $stepExecution->getJobExecution()->willReturn($jobExecution);
     $jobConfigurationRepo->findOneBy(['jobExecution' => $jobExecution])->willReturn($jobConfiguration);
     $values = ['categories' => [['scope' => null, 'locale' => null, 'data' => ['office', 'bedroom']]]];
     $normalizedValues = addslashes(json_encode($values));
     $jobConfiguration->getConfiguration()->willReturn(json_encode(['filters' => [], 'actions' => ['normalized_values' => $normalizedValues, 'ui_locale' => 'fr_FR', 'attribute_locale' => 'en_US']]));
     $validator->validate($product)->willReturn($violations);
     $violation = new ConstraintViolation('error2', 'spec', [], '', '', $product);
     $violations = new ConstraintViolationList([$violation, $violation]);
     $validator->validate($product)->willReturn($violations);
     $attributeRepository->findOneByIdentifier('categories')->willReturn($attribute);
     $product->isAttributeEditable($attribute)->willReturn(true);
     $productUpdater->update($product, $values)->shouldBeCalled();
     $this->setStepExecution($stepExecution);
     $stepExecution->addWarning(Argument::cetera())->shouldBeCalled();
     $stepExecution->incrementSummaryInfo('skipped_products')->shouldBeCalled();
     $this->process($product);
 }
 /**
  * Get a list of available attributes
  *
  * @param string $typeCode
  * @return AttributeInterface[]
  */
 public function getAttributes($typeCode)
 {
     $attributeCodes = $this->repository->getAttributeCodesByType($typeCode);
     $attributeList = [];
     foreach ($attributeCodes as $attributeCode) {
         $attributeList[] = $this->repository->findOneByIdentifier($attributeCode);
     }
     return $attributeList;
 }
 /**
  * {@inheritdoc}
  */
 public function getPresenterByAttributeCode($code)
 {
     $attribute = $this->attributeRepository->findOneByIdentifier($code);
     if (null === $attribute) {
         return null;
     }
     $attributeType = $attribute->getAttributeType();
     if (null === $attributeType) {
         return null;
     }
     return $this->getPresenter($attributeType, self::TYPE_PRODUCT_VALUE);
 }
 /**
  * {@inheritdoc}
  */
 public function process($family)
 {
     $actions = $this->getConfiguredActions();
     foreach ($actions as $action) {
         $attribute = $this->attributeRepository->findOneByIdentifier($action['attribute_code']);
         $channel = $this->channelRepository->findOneByIdentifier($action['channel_code']);
         $isRequired = $action['is_required'];
         $family->addAttribute($attribute);
         $family->addAttributeRequirement($this->factory->createAttributeRequirement($attribute, $channel, $isRequired));
     }
     return $family;
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $values = new ArrayCollection();
     foreach ($data as $attributeCode => $valuesData) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         foreach ($valuesData as $valueData) {
             $value = $this->denormalizer->denormalize($valueData, $this->valueClass, 'json', ['attribute' => $attribute] + $context);
             $values->add($value);
         }
     }
     return $values;
 }
 function it_denormalizes_product_values_from_json($denormalizer, $registry, AttributeRepositoryInterface $attributeRepository, ProductValueInterface $nameValue, ProductValueInterface $colorValue, AttributeInterface $name, AttributeInterface $color)
 {
     $data = ['name' => [['locale' => null, 'scope' => null, 'value' => 'foo']], 'color' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'value' => 'red']]];
     $attributeRepository->findOneByIdentifier('name')->willReturn($name);
     $attributeRepository->findOneByIdentifier('color')->willReturn($color);
     $registry->getRepository('Attribute')->willReturn($attributeRepository);
     $denormalizer->denormalize($data['name'][0], 'ProductValue', 'json', ['attribute' => $name])->shouldBeCalled()->willReturn($nameValue);
     $denormalizer->denormalize($data['color'][0], 'ProductValue', 'json', ['attribute' => $color])->shouldBeCalled()->willReturn($colorValue);
     $values = $this->denormalize($data, 'ProductValue[]', 'json');
     $values->shouldHaveCount(2);
     $values[0]->shouldBe($nameValue);
     $values[1]->shouldBe($colorValue);
 }
 /**
  * {@inheritdoc}
  */
 public function validate($attributes, Constraint $constraint)
 {
     if (null === $attributes || !count($attributes)) {
         return;
     }
     $errorCount = 0;
     foreach ($attributes as $attributeCode) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         if (null === $attribute) {
             $this->context->buildViolation($constraint->message)->setParameter('%attributeCode%', $attributeCode)->atPath(sprintf('[%d]', $errorCount))->addViolation();
             $errorCount++;
         }
     }
 }
 /**
  * @param GroupInterface $group
  * @param array          $data
  */
 protected function setAxis(GroupInterface $group, $data)
 {
     if (isset($data['axis']) && !empty($data['axis'])) {
         $axisCodes = explode(',', $data['axis']);
         $attributes = [];
         foreach ($axisCodes as $code) {
             $attribute = $this->attributeRepository->findOneByIdentifier($code);
             if (!$attribute) {
                 throw new \LogicException(sprintf('Attribute with identifier "%s" not found', $code));
             }
             $attributes[] = $attribute;
         }
         $group->setAxisAttributes($attributes);
     }
 }
 /**
  * @param string $code
  *
  * @return AttributeInterface
  */
 protected function getAttribute($code)
 {
     if (!array_key_exists($code, $this->attributes)) {
         $this->attributes[$code] = $this->attributeRepository->findOneByIdentifier($code);
     }
     return $this->attributes[$code];
 }
 /**
  * {@inheritdoc}
  */
 public function process($family)
 {
     $configuration = $this->getJobConfiguration();
     if (!array_key_exists('actions', $configuration)) {
         throw new InvalidArgumentException('Missing configuration for \'actions\'.');
     }
     $actions = $configuration['actions'];
     foreach ($actions as $action) {
         $attribute = $this->attributeRepository->findOneByIdentifier($action['attribute_code']);
         $channel = $this->channelRepository->findOneByIdentifier($action['channel_code']);
         $isRequired = $action['is_required'];
         $family->addAttribute($attribute);
         $family->addAttributeRequirement($this->factory->createAttributeRequirement($attribute, $channel, $isRequired));
     }
     return $family;
 }
 /**
  * Extract attribute field name information with attribute code, locale code, scope code
  * and optionally price currency
  *
  * Returned array like:
  * [
  *     "attribute"   => AttributeInterface,
  *     "locale_code" => <locale_code>|null,
  *     "scope_code"  => <scope_code>|null,
  *     "price_currency" => <currency_code> // this key is optional
  * ]
  *
  * Return null if the field name does not match an attribute.
  *
  * @param string $fieldName
  *
  * @return array|null
  */
 public function extractColumnInfo($fieldName)
 {
     if (!isset($this->fieldNameInfoCache[$fieldName]) && !in_array($fieldName, $this->excludedFieldNames)) {
         $explodedFieldName = explode(self::FIELD_SEPARATOR, $fieldName);
         $attributeCode = $explodedFieldName[0];
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         if (null !== $attribute) {
             $this->checkFieldNameTokens($attribute, $fieldName, $explodedFieldName);
             $attributeInfo = $this->extractAttributeInfo($attribute, $explodedFieldName);
             $this->checkFieldNameLocaleByChannel($attribute, $fieldName, $attributeInfo);
             $this->fieldNameInfoCache[$fieldName] = $attributeInfo;
         } else {
             $this->excludedFieldNames[] = $fieldName;
         }
     }
     return isset($this->fieldNameInfoCache[$fieldName]) ? $this->fieldNameInfoCache[$fieldName] : null;
 }
 /**
  * @param AttributeRepositoryInterface $attributeRepository
  * @param AttributeOptionRepositoryInterface $attributeOptionRepository
  * @param string $attributeCode
  * @param FileLoader $loader
  * @param string $mappingFile
  */
 public function __construct(AttributeRepositoryInterface $attributeRepository, AttributeOptionRepositoryInterface $attributeOptionRepository, $attributeCode, FileLoader $loader = null, $mappingFile = null)
 {
     $this->attributeRepository = $attributeRepository;
     $this->attributeOptionRepository = $attributeOptionRepository;
     $this->mapping = [];
     $this->attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
     if ($this->attribute) {
         $this->attributeOptions = $this->attribute->getOptions();
     } else {
         $this->attributeOptions = new ArrayCollection();
     }
     if ($mappingFile !== null && $loader !== null) {
         foreach ($loader->load($mappingFile) as $rawValue => $optionCode) {
             $this->mapTo($rawValue, $optionCode);
         }
     }
 }
 /**
  * Get non eligible attributes to a product template
  *
  * @param GroupInterface $variantGroup
  *
  * @return AttributeInterface[]
  */
 public function getNonEligibleAttributes(GroupInterface $variantGroup)
 {
     $attributes = $variantGroup->getAxisAttributes()->toArray();
     $template = $variantGroup->getProductTemplate();
     if (null !== $template) {
         foreach (array_keys($template->getValuesData()) as $attributeCode) {
             $attributes[] = $this->attributeRepository->findOneByIdentifier($attributeCode);
         }
     }
     $uniqueAttributes = $this->attributeRepository->findBy(['unique' => true]);
     foreach ($uniqueAttributes as $attribute) {
         if (!in_array($attribute, $attributes)) {
             $attributes[] = $attribute;
         }
     }
     return $attributes;
 }
 /**
  * @param FamilyInterface $family
  * @param string          $data
  *
  * @throws \InvalidArgumentException
  */
 protected function setAttributeAsLabel(FamilyInterface $family, $data)
 {
     if (null !== ($attribute = $this->attributeRepository->findOneByIdentifier($data))) {
         $family->setAttributeAsLabel($attribute);
     } else {
         throw new \InvalidArgumentException(sprintf('Attribute with "%s" code does not exist', $data));
     }
 }
 /**
  * Get attribute by identifier
  *
  * @param string $identifier
  *
  * @return JsonResponse
  */
 public function getAction($identifier)
 {
     $attribute = $this->attributeRepository->findOneByIdentifier($identifier);
     $attribute = $this->attributeFilter->filterObject($attribute, 'pim.internal_api.attribute.view') ? null : $attribute;
     if (null === $attribute) {
         throw new NotFoundHttpException(sprintf('Attribute with code "%s" not found', $identifier));
     }
     return new JsonResponse($this->normalizer->normalize($attribute, 'internal_api'));
 }
 /**
  * @param string $code
  *
  * @throws ObjectNotFoundException
  *
  * @return AttributeInterface
  */
 protected function getAttribute($code)
 {
     if (!array_key_exists($code, $this->attributes)) {
         $attribute = $this->attributeRepository->findOneByIdentifier($code);
         if (!$attribute) {
             throw new ObjectNotFoundException(sprintf('Attribute with code "%s" was not found.', $code));
         }
         $this->attributes[$code] = $attribute;
     }
     return $this->attributes[$code];
 }
 /**
  * Prepare product values
  *
  * @param ProductInterface $product
  * @param array            $actions
  *
  * @return array
  */
 protected function prepareProductValues(ProductInterface $product, array $actions)
 {
     $normalizedValues = json_decode($actions['normalized_values'], true);
     $filteredValues = [];
     foreach ($normalizedValues as $attributeCode => $values) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         if ($product->isAttributeEditable($attribute)) {
             $filteredValues[$attributeCode] = $values;
         }
     }
     return $filteredValues;
 }
 /**
  * @param GroupInterface $group
  * @param string[]       $attributeCodes
  *
  * @throws \InvalidArgumentException
  */
 protected function setAxis(GroupInterface $group, array $attributeCodes)
 {
     $attributes = [];
     foreach ($attributeCodes as $attributeCode) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         if (null === $attribute) {
             throw new \InvalidArgumentException(sprintf('Attribute "%s" does not exist', $attributeCode));
         }
         $attributes[] = $attribute;
     }
     $group->setAxisAttributes($attributes);
 }
 /**
  * Change users' data (example: "12,45") into storable data (example: "12.45").
  *
  * @param array  $data
  * @param string $uiLocaleCode
  *
  * @return array
  */
 protected function delocalizeData(array $data, $uiLocaleCode)
 {
     foreach ($data as $code => $values) {
         $attribute = $this->attributeRepository->findOneByIdentifier($code);
         $localizer = $this->localizerRegistry->getLocalizer($attribute->getAttributeType());
         if (null !== $localizer) {
             $values = array_map(function ($value) use($localizer, $uiLocaleCode) {
                 $value['data'] = $localizer->delocalize($value['data'], ['locale' => $uiLocaleCode]);
                 return $value;
             }, $values);
             $data[$code] = $values;
         }
     }
     return $data;
 }
 /**
  * @param GroupInterface $variantGroup
  * @param array          $axes
  *
  * @throws \InvalidArgumentException
  */
 protected function setAxes(GroupInterface $variantGroup, array $axes)
 {
     if (null !== $variantGroup->getId()) {
         if (array_diff($this->getOriginalAxes($variantGroup->getAxisAttributes()), array_values($axes))) {
             throw new \InvalidArgumentException('Attributes: This property cannot be changed.');
         }
     }
     foreach ($axes as $axis) {
         $attribute = $this->attributeRepository->findOneByIdentifier($axis);
         if (null !== $attribute) {
             $variantGroup->addAxisAttribute($attribute);
         } else {
             throw new \InvalidArgumentException(sprintf('Attribute "%s" does not exist', $axis));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function addFilter($field, $operator, $value, array $context = [])
 {
     $attribute = $this->attributeRepository->findOneByIdentifier(FieldFilterHelper::getCode($field));
     if (null !== $attribute) {
         $filter = $this->filterRegistry->getAttributeFilter($attribute, $operator);
     } else {
         $filter = $this->filterRegistry->getFieldFilter($field, $operator);
     }
     if (null === $filter) {
         throw new \LogicException(sprintf('Filter on property "%s" is not supported or does not support operator "%s"', $field, $operator));
     }
     $context = $this->getFinalContext($context);
     if (null !== $attribute) {
         $context['field'] = $field;
         $this->addAttributeFilter($filter, $attribute, $operator, $value, $context);
     } else {
         $this->addFieldFilter($filter, $field, $operator, $value, $context);
     }
     $this->rawFilters[] = ['field' => $field, 'operator' => $operator, 'value' => $value, 'context' => $context];
     return $this;
 }
 /**
  * Prepare product values
  *
  * @param ProductInterface $product
  * @param array            $actions
  *
  * @return array
  */
 protected function prepareProductValues(ProductInterface $product, array $actions)
 {
     $normalizedValues = json_decode($actions['normalized_values'], true);
     $attributeLocale = $actions['attribute_locale'];
     $filteredValues = [];
     foreach ($normalizedValues as $attributeCode => $values) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         if ($product->isAttributeEditable($attribute)) {
             $values = array_filter($values, function ($value) use($attributeLocale) {
                 return $attributeLocale === $value['locale'] || null === $value['locale'];
             });
             $localizer = $this->localizerRegistry->getLocalizer($attribute->getAttributeType());
             if (null !== $localizer) {
                 $locale = $actions['ui_locale'];
                 $values = array_map(function ($value) use($localizer, $locale) {
                     $value['data'] = $localizer->delocalize($value['data'], ['locale' => $locale]);
                     return $value;
                 }, $values);
             }
             $filteredValues[$attributeCode] = $values;
         }
     }
     return $filteredValues;
 }
 /**
  * @param string $code
  *
  * @return AttributeInterface|null
  */
 protected function findAttribute($code)
 {
     $attribute = $this->attributeRepository->findOneByIdentifier($code);
     return $attribute;
 }
 public function it_should_not_remove_identifier_requirements_when_other_requirements_are_provided($attrRequiFactory, $channelRepository, FamilyInterface $family, AttributeRepositoryInterface $attributeRepository, AttributeInterface $skuAttribute, AttributeInterface $nameAttribute, AttributeInterface $descAttribute, AttributeRequirementInterface $skuMobileRqrmt, AttributeRequirementInterface $skuPrintRqrmt, AttributeRequirementInterface $namePrintRqrmt, AttributeRequirementInterface $descPrintRqrmt, ChannelInterface $mobileChannel, ChannelInterface $printChannel)
 {
     $values = ['code' => 'mycode', 'requirements' => ['print' => ['name', 'description']]];
     $family->getAttributeRequirements()->willReturn([$skuMobileRqrmt, $skuPrintRqrmt]);
     $family->getId()->willReturn(42);
     $skuMobileRqrmt->getAttribute()->willReturn($skuAttribute);
     $skuPrintRqrmt->getAttribute()->willReturn($skuAttribute);
     $skuMobileRqrmt->getChannelCode()->willReturn('mobile');
     $skuPrintRqrmt->getChannelCode()->willReturn('print');
     $skuAttribute->getAttributeType()->willReturn(AttributeTypes::IDENTIFIER);
     $channelRepository->findOneByIdentifier('print')->willReturn($printChannel);
     $attributeRepository->findOneByIdentifier('name')->willReturn($nameAttribute);
     $attributeRepository->findOneByIdentifier('description')->willReturn($descAttribute);
     $attrRequiFactory->createAttributeRequirement($nameAttribute, $printChannel, true)->willReturn($namePrintRqrmt);
     $attrRequiFactory->createAttributeRequirement($descAttribute, $printChannel, true)->willReturn($descPrintRqrmt);
     $namePrintRqrmt->getAttribute()->willReturn($nameAttribute);
     $descPrintRqrmt->getAttribute()->willReturn($descAttribute);
     $channelRepository->getChannelCodes()->willReturn(['mobile', 'print']);
     $channelRepository->findOneByIdentifier('mobile')->willReturn($mobileChannel);
     $channelRepository->findOneByIdentifier('print')->willReturn($printChannel);
     $attributeRepository->getIdentifier()->willReturn($skuAttribute);
     $family->setCode('mycode')->shouldBeCalled();
     $family->setAttributeRequirements([$skuMobileRqrmt, $skuPrintRqrmt, $namePrintRqrmt, $descPrintRqrmt])->shouldBeCalled();
     $this->update($family, $values, []);
 }