/** * @param string $code * * @return string */ public function getAttributeLabelFromCode($code) { if (null !== ($attribute = $this->attributeRepository->findOneByIdentifier($code))) { return (string) $attribute; } return $code; }
function it_builds_form(FormBuilderInterface $builder, IdentifiableObjectRepositoryInterface $repository, DataTransformerInterface $transformer, $transformerFactory) { $options = ['repository' => $repository->getWrappedObject(), 'multiple' => false]; $transformerFactory->create($repository, ['multiple' => false])->willReturn($transformer); $builder->addViewTransformer($transformer, true)->shouldBeCalled(); $this->buildForm($builder, $options); }
function let(IdentifiableObjectRepositoryInterface $repository, \stdClass $entity) { $entity->code = 'foo'; $repository->getIdentifierProperties()->willReturn(['code']); $repository->findOneByIdentifier('foo')->willReturn($entity); $this->beConstructedWith($repository, ['multiple' => false]); }
/** * {@inheritdoc} */ public function transform($model) { if (null === $model) { return null; } if (count($this->repository->getIdentifierProperties()) > 1) { throw new \InvalidArgumentException('Cannot transform object with multiple identifiers'); } $identifierProperty = $this->repository->getIdentifierProperties()[0]; $propertyAccessor = PropertyAccess::createPropertyAccessor(); if ($this->multiple) { if (!is_array($model)) { throw new UnexpectedTypeException($model, 'array'); } $identifiers = []; foreach ($model as $object) { $identifiers[] = $propertyAccessor->getValue($object, $identifierProperty); } return $identifiers; } if (!is_object($model)) { throw new UnexpectedTypeException($model, 'object'); } return $propertyAccessor->getValue($model, $identifierProperty); }
/** * Find a variant group by its id or return a 404 response * * @param string $code * * @throws NotFoundHttpException * * @return GroupInterface */ protected function findVariantGroupOr404($code) { $group = $this->groupRepository->findOneByIdentifier($code); if (null === $group || false === $group->getType()->isVariant()) { throw new NotFoundHttpException(sprintf('Variant group with id %d could not be found.', $id)); } return $group; }
/** * Find an object according to its identifiers from a repository. * * @param IdentifiableObjectRepositoryInterface $repository the repository to search inside * @param array $data the data that is currently processed * * @throws MissingIdentifierException in case the processed data do not allow to retrieve an object * by its identifiers properly * * @return object|null */ protected function findObject(IdentifiableObjectRepositoryInterface $repository, array $data) { $properties = $repository->getIdentifierProperties(); $references = []; foreach ($properties as $property) { if (!isset($data[$property])) { throw new MissingIdentifierException(sprintf('Missing identifier column "%s". Columns found: %s.', $property, implode(', ', array_keys($data)))); } $references[] = $data[$property]; } return $repository->findOneByIdentifier(implode('.', $references)); }
/** * Find an object according to its identifiers from a repository. * * @param IdentifiableObjectRepositoryInterface $repository the repository to search inside * @param array $data the data that is currently processed * * @throws MissingIdentifierException in case the processed data do not allow to retrieve an object * by its identifiers properly * * @return object|null */ protected function findObject(IdentifiableObjectRepositoryInterface $repository, array $data) { $properties = $repository->getIdentifierProperties(); $references = []; foreach ($properties as $property) { if (!isset($data[$property])) { throw new MissingIdentifierException(); } $references[] = $data[$property]; } return $repository->findOneByIdentifier(implode('.', $references)); }
/** * {@inheritdoc} * * Expected data input format : "family_code" */ public function setFieldData(ProductInterface $product, $field, $data, array $options = []) { $this->checkData($field, $data); if (null !== $data) { $family = $this->familyRepository->findOneByIdentifier($data); if (null === $family) { throw InvalidArgumentException::expected($field, 'existing family code', 'setter', 'family', $data); } $product->setFamily($family); } else { $product->setFamily(null); } }
/** * {@inheritdoc} * * Expected data input format : ["category_code", "another_category_code"] */ public function removeFieldData(ProductInterface $product, $field, $data, array $options = []) { $this->checkData($field, $data); $categories = []; foreach ($data as $categoryCode) { $category = $this->categoryRepository->findOneByIdentifier($categoryCode); if (null === $category) { throw InvalidArgumentException::expected($field, 'existing category code', 'remover', 'category', $categoryCode); } $categories[] = $category; } foreach ($categories as $categoryToRemove) { $product->removeCategory($categoryToRemove); } }
/** * @param string $code * * @return Group|null */ protected function findGroup($code) { $group = $this->groupRepository->findOneByIdentifier($code); if (null === $group) { throw new \InvalidArgumentException(sprintf('Group %s was not found', $code)); } return $group; }
/** * {@inheritdoc} */ public function sort(array $columns, array $context = []) { $identifier = $this->productRepository->getIdentifierProperties()[0]; if (isset($context['filters']['structure']['attributes']) && !empty($context['filters']['structure']['attributes'])) { $rawColumns = array_merge([$identifier], $this->firstDefaultColumns, array_map(function ($associationType) { return $associationType->getCode(); }, $this->associationTypeRepository->findAll()), $context['filters']['structure']['attributes']); $sortedColumns = []; foreach ($rawColumns as $columnCode) { $sortedColumns = array_merge($sortedColumns, array_filter($columns, function ($columnCandidate) use($columnCode) { return 0 !== preg_match(sprintf('/^%s(-.*)*/', $columnCode), $columnCandidate); })); } return $sortedColumns; } array_unshift($this->firstDefaultColumns, $identifier); return parent::sort($columns); }
/** * {@inheritdoc} * * Expected data input format : ["group_code"] */ public function addFieldData(ProductInterface $product, $field, $data, array $options = []) { $this->checkData($field, $data); $groups = []; foreach ($data as $groupCode) { $group = $this->groupRepository->findOneByIdentifier($groupCode); if (null === $group) { throw InvalidArgumentException::expected($field, 'existing group code', 'adder', 'groups', $groupCode); } elseif ($group->getType()->isVariant()) { throw InvalidArgumentException::expected($field, 'non variant group code', 'adder', 'groups', $groupCode); } else { $groups[] = $group; } } foreach ($groups as $group) { $product->addGroup($group); } }
/** * @param array $configuration */ public function execute(array $configuration) { $actions = $configuration['actions']; $variantGroupCode = $actions['value']; $variantGroup = $this->groupRepository->findOneByIdentifier($variantGroupCode); $axisAttributeCodes = $this->getAxisAttributeCodes($variantGroup); $eligibleProductIds = $this->productRepository->getEligibleProductIdsForVariantGroup($variantGroup->getId()); $cursor = $this->getProductsCursor($configuration['filters']); $paginator = $this->paginatorFactory->createPaginator($cursor); list($productAttributeAxis, $acceptedIds) = $this->filterDuplicateAxisCombinations($paginator, $eligibleProductIds, $axisAttributeCodes); $excludedIds = $this->addSkippedMessageForDuplicatedProducts($productAttributeAxis); $acceptedIds = array_diff($acceptedIds, $excludedIds); $configuration['filters'] = [['field' => 'id', 'operator' => 'IN', 'value' => $acceptedIds]]; if (0 === count($acceptedIds)) { $configuration = null; } $this->setJobConfiguration(json_encode($configuration)); }
function it_returns_no_products_when_they_all_are_duplicated($productRepository, $paginatorFactory, $stepExecution, ProductInterface $product, $pqbFactory, ProductQueryBuilder $pqb, ProductQueryBuilder $pqb2, Cursor $cursor, Cursor $cursor2, JobParameters $jobParameters, IdentifiableObjectRepositoryInterface $groupRepository, GroupInterface $variantGroup, AttributeInterface $axe, PaginatorInterface $paginator) { $configuration = ['filters' => [['field' => 'id', 'operator' => 'IN', 'value' => [12, 13]]], 'actions' => ['value' => 'variantGroupCode']]; $stepExecution->getJobParameters()->willReturn($jobParameters); $jobParameters->get('filters')->willReturn($configuration['filters']); $jobParameters->get('actions')->willReturn($configuration['actions']); $groupRepository->findOneByIdentifier('variantGroupCode')->willReturn($variantGroup); $variantGroup->getAxisAttributes()->willReturn([$axe]); $variantGroup->getId()->willReturn(42); $axe->getCode()->willReturn('axe'); $productRepository->getEligibleProductIdsForVariantGroup(42)->willReturn([]); $pqbFactory->create(['filters' => $configuration['filters']])->willReturn($pqb); $pqb->execute()->willReturn($cursor); $paginatorFactory->createPaginator($cursor)->willReturn($paginator); $pqbFactory->create(['filters' => [['field' => 'id', 'operator' => 'IN', 'value' => ['']]]])->willReturn($pqb2); $pqb2->execute()->willReturn($cursor2); $this->initialize(); $this->read()->shouldReturn(null); }
/** * @param AssociationInterface $association * @param array $groupsCodes */ protected function setAssociatedGroups(AssociationInterface $association, $groupsCodes) { foreach ($groupsCodes as $groupCode) { $associatedGroup = $this->groupRepository->findOneByIdentifier($groupCode); if (!$associatedGroup) { throw InvalidArgumentException::expected('associations', 'existing group code', 'setter', 'association', $groupCode); } $association->addGroup($associatedGroup); } }
/** * {@inheritdoc} * * Expected data input format : ["group_code"] */ public function setFieldData(ProductInterface $product, $field, $data, array $options = []) { $this->checkData($field, $data); $groups = []; foreach ($data as $groupCode) { $group = $this->groupRepository->findOneByIdentifier($groupCode); if (null === $group) { throw InvalidArgumentException::expected($field, 'existing group code', 'setter', 'groups', $groupCode); } else { $groups[] = $group; } } $oldGroups = $product->getGroups(); foreach ($oldGroups as $group) { $product->removeGroup($group); } foreach ($groups as $group) { $product->addGroup($group); } }
/** * @param ChannelInterface $channel * @param array $localeCodes */ protected function setLocales(ChannelInterface $channel, array $localeCodes) { $locales = []; foreach ($localeCodes as $localeCode) { $locale = $this->localeRepository->findOneByIdentifier($localeCode); if (null === $locale) { throw new \InvalidArgumentException(sprintf('Locale with "%s" code does not exist', $localeCode)); } $locales[] = $locale; } $channel->setLocales($locales); }
/** * {@inheritdoc} * * Expected data input format : "variant_group_code" */ public function setFieldData(ProductInterface $product, $field, $data, array $options = []) { $this->checkData($field, $data); if (null !== $data) { $variantGroup = $this->groupRepository->findOneByIdentifier($data); if (null === $variantGroup) { throw InvalidArgumentException::expected($field, 'existing variant group code', 'setter', 'variant_group', $data); } if (!$variantGroup->getType()->isVariant()) { throw InvalidArgumentException::expected($field, 'variant group code', 'setter', 'variant_group', $data); } } $existingGroups = $product->getGroups(); foreach ($existingGroups as $group) { if ($group->getType()->isVariant()) { $product->removeGroup($group); } } if (null !== $data) { $product->addGroup($variantGroup); } }
/** * Clean the filters to keep only non duplicated. * This method send "skipped" message for every duplicated product for a variant group. * * When all the selected products are skipped, there is no remaining products to mass-edit. The standard behaviour * should return a single filter like "id IN ()", which is equivalent to "nothing", and it's it is very poorly * managed by Doctrine. * Instead of returning this filter, we return a "is IS NULL" filter, which in this case is completely * equivalent to "nothing" (there can not be null ids). * * @param StepExecution $stepExecution * @param array $filters * @param array $actions * * @return array */ public function clean(StepExecution $stepExecution, $filters, $actions) { $variantGroupCode = $actions['value']; $variantGroup = $this->groupRepository->findOneByIdentifier($variantGroupCode); $axisAttributeCodes = $this->getAxisAttributeCodes($variantGroup); $eligibleProductIds = $this->productRepository->getEligibleProductIdsForVariantGroup($variantGroup->getId()); $cursor = $this->getProductsCursor($filters); $paginator = $this->paginatorFactory->createPaginator($cursor); list($productAttributeAxis, $acceptedIds) = $this->filterDuplicateAxisCombinations($stepExecution, $paginator, $eligibleProductIds, $axisAttributeCodes); $excludedIds = $this->addSkippedMessageForDuplicatedProducts($stepExecution, $productAttributeAxis); $acceptedIds = array_diff($acceptedIds, $excludedIds); if (0 === count($acceptedIds)) { return [['field' => 'id', 'operator' => Operators::IN_LIST, 'value' => ['']]]; } return [['field' => 'id', 'operator' => Operators::IN_LIST, 'value' => $acceptedIds]]; }
/** * @param string $code * * @return CategoryInterface|null */ protected function findParent($code) { return $this->categoryRepository->findOneByIdentifier($code); }
/** * {@inheritdoc} */ public function sort(array $columns) { $identifier = $this->productRepository->getIdentifierProperties()[0]; array_unshift($this->firstDefaultColumns, $identifier); return parent::sort($columns); }
/** * @param AttributeInterface $attribute * @param string $optionCode * * @return AttributeOptionInterface|null */ protected function getOption(AttributeInterface $attribute, $optionCode) { $identifier = $attribute->getCode() . '.' . $optionCode; $option = $this->attrOptionRepository->findOneByIdentifier($identifier); return $option; }
/** * @param string $familyCode * * @return FamilyInterface */ protected function getFamily($familyCode) { $family = $this->familyRepository->findOneByIdentifier($familyCode); return $family; }
/** * @param string $code * * @return AttributeInterface|null */ protected function getAttribute($code) { return $this->attributeRepository->findOneByIdentifier($code); }
/** * @param string $channelCode * * @return bool */ protected function doesChannelExist($channelCode) { $channel = $this->channelRepository->findOneByIdentifier($channelCode); return null !== $channel; }
/** * @param string $categoryCode * * @return CategoryInterface */ protected function getCategory($categoryCode) { $category = $this->categoryRepository->findOneByIdentifier($categoryCode); return $category; }
/** * @param string $identifier * * @return ProductInterface|null */ public function findProduct($identifier) { return $this->repository->findOneByIdentifier($identifier); }
function it_transform_an_existing_product($guesser, $columnInfoTransformer, $attributeCache, $doctrine, $associationReader, $templateUpdater, $productRepository, AttributeInterface $attributeSku, AttributeInterface $attributeDesc, ColumnInfo $columnInfoSku, ColumnInfo $columnInfoFamily, ColumnInfo $columnInfoGroups, ColumnInfo $columnInfoCategories, ColumnInfo $columnInfoAssocGroups, ColumnInfo $columnInfoAssocProducts, ColumnInfo $columnInfoDesc, IdentifiableObjectRepositoryInterface $productRepository, ProductInterface $product, ObjectManager $objectManager, ClassMetadata $productMetadata, RelationTransformer $relationFamily, RelationTransformer $relationGroups, RelationTransformer $relationCategories, ClassMetadata $productValueMetadata, DefaultTransformer $defaultTransformer, ProductValueInterface $valueSku, ProductValueInterface $valueDesc, GroupInterface $variantGroup, ProductTemplateInterface $productTemplate) { // initialize attributes $columnInfoTransformer->transform('Pim\\Bundle\\CatalogBundle\\Model\\Product', ['sku', 'family', 'groups', 'categories', 'SUBSTITUTION-groups', 'SUBSTITUTION-products', 'description-en_US-mobile'])->shouldBeCalled()->willReturn([$columnInfoSku, $columnInfoFamily, $columnInfoGroups, $columnInfoCategories, $columnInfoAssocGroups, $columnInfoAssocProducts, $columnInfoDesc]); $columnInfoSku->getName()->willReturn('sku'); $columnInfoSku->getPropertyPath()->willReturn('sku'); $columnInfoSku->getLabel()->willReturn('sku'); $columnInfoSku->getSuffixes()->willReturn([]); $columnInfoFamily->getName()->willReturn('family'); $columnInfoFamily->getPropertyPath()->willReturn('family'); $columnInfoFamily->getLabel()->willReturn('family'); $columnInfoFamily->getSuffixes()->willReturn([]); // TODO : we should introduce a variant_group column after PIM-2458 $columnInfoGroups->getName()->willReturn('groups'); $columnInfoGroups->getPropertyPath()->willReturn('groups'); $columnInfoGroups->getLabel()->willReturn('groups'); $columnInfoGroups->getSuffixes()->willReturn([]); $columnInfoCategories->getName()->willReturn('categories'); $columnInfoCategories->getPropertyPath()->willReturn('categories'); $columnInfoCategories->getLabel()->willReturn('categories'); $columnInfoCategories->getSuffixes()->willReturn([]); $columnInfoAssocGroups->getName()->willReturn('SUBSTITUTION'); $columnInfoAssocGroups->getPropertyPath()->willReturn('sUBSTITUTION'); $columnInfoAssocGroups->getLabel()->willReturn('SUBSTITUTION-groups'); $columnInfoAssocGroups->getSuffixes()->willReturn(['groups']); $columnInfoAssocProducts->getName()->willReturn('SUBSTITUTION'); $columnInfoAssocProducts->getPropertyPath()->willReturn('sUBSTITUTION'); $columnInfoAssocProducts->getLabel()->willReturn('SUBSTITUTION-products'); $columnInfoAssocProducts->getSuffixes()->willReturn(['products']); $columnInfoDesc->getName()->willReturn('description'); $columnInfoDesc->getPropertyPath()->willReturn('description'); $columnInfoDesc->getLabel()->willReturn('description'); $columnInfoDesc->getSuffixes()->willReturn([]); $attributeCache->getAttributes([$columnInfoSku, $columnInfoFamily, $columnInfoGroups, $columnInfoCategories, $columnInfoAssocGroups, $columnInfoAssocProducts, $columnInfoDesc])->willReturn(['sku' => $attributeSku, 'description' => $attributeDesc]); $columnInfoSku->setAttribute($attributeSku)->shouldBeCalled(); $attributeSku->getAttributeType()->willReturn('pim_catalog_identifier'); $columnInfoFamily->setAttribute(null)->shouldBeCalled(); $columnInfoGroups->setAttribute(null)->shouldBeCalled(); $columnInfoCategories->setAttribute(null)->shouldBeCalled(); $columnInfoDesc->setAttribute($attributeDesc)->shouldBeCalled(); // find entity $attributeSku->getCode()->willReturn('sku'); $productRepository->findOneByIdentifier('AKNTS')->willReturn($product); // set product properties $doctrine->getManagerForClass('Pim\\Bundle\\CatalogBundle\\Model\\Product')->willReturn($objectManager); $objectManager->getClassMetadata('Pim\\Bundle\\CatalogBundle\\Model\\Product')->willReturn($productMetadata); $guesser->getTransformerInfo($columnInfoFamily, $productMetadata)->willReturn([$relationFamily, ['class' => 'Pim\\Bundle\\CatalogBundle\\Entity\\Family', 'multiple' => false]]); $guesser->getTransformerInfo($columnInfoGroups, $productMetadata)->willReturn([$relationGroups, ['class' => 'Pim\\Bundle\\CatalogBundle\\Entity\\Group', 'multiple' => true]]); $guesser->getTransformerInfo($columnInfoCategories, $productMetadata)->willReturn([$relationCategories, ['class' => 'Pim\\Bundle\\CatalogBundle\\Entity\\Category', 'multiple' => true]]); // set product values $attributeCache->getRequiredAttributeCodes($product)->willReturn(['sku', 'description']); $doctrine->getManagerForClass('Pim\\Bundle\\CatalogBundle\\Model\\ProductValue')->willReturn($objectManager); $objectManager->getClassMetadata('Pim\\Bundle\\CatalogBundle\\Model\\ProductValue')->willReturn($productValueMetadata); $guesser->getTransformerInfo($columnInfoSku, $productValueMetadata)->willReturn([$defaultTransformer, []]); $guesser->getTransformerInfo($columnInfoDesc, $productValueMetadata)->willReturn([$defaultTransformer, []]); $columnInfoSku->getLocale()->willReturn(null); $columnInfoSku->getScope()->willReturn(null); $columnInfoDesc->getLocale()->willReturn('en_US'); $columnInfoDesc->getScope()->willReturn('mobile'); $product->getValue('sku', null, null)->willReturn($valueSku); $product->getValue('description', 'de_DE', 'mobile')->willReturn($valueDesc); // set variant group values $product->getVariantGroup()->willReturn($variantGroup); $variantGroup->getProductTemplate()->willReturn($productTemplate); $templateUpdater->update($productTemplate, [$product])->shouldBeCalled(); // set associations $product->getReference()->willReturn('AKNTS'); $associationReader->addItem(['association_type' => 'SUBSTITUTION', 'owner' => 'AKNTS', 'products' => 'AKNTS_WPS,AKNTS_PBS,AKNTS_PWS'])->shouldBeCalled(); $this->transform('Pim\\Bundle\\CatalogBundle\\Model\\Product', ['sku' => 'AKNTS', 'family' => 'tshirts', 'groups' => 'akeneo_tshirt', 'categories' => 'tshirts,goodies', 'SUBSTITUTION-groups' => '', 'SUBSTITUTION-products' => 'AKNTS_WPS,AKNTS_PBS,AKNTS_PWS', 'description-en_US-mobile' => '<p>Akeneo T-Shirt</p>']); }
/** * @param string $localeCode * * @return bool */ protected function doesLocaleExist($localeCode) { $locale = $this->localeRepository->findOneByIdentifier($localeCode); return null !== $locale; }
/** * @param string $code * * @return CurrencyInterface|null */ protected function findCurrency($code) { return $this->currencyRepository->findOneByIdentifier($code); }