/** * Create and configure a family intance * * @return Family */ public function createFamily() { $family = new Family(); $identifier = $this->productManager->getIdentifierAttribute(); $family->addAttribute($identifier); $family->setAttributeAsLabel($identifier); foreach ($this->getChannels() as $channel) { $family->addAttributeRequirement($this->factory->createAttributeRequirement($identifier, $channel, true)); } return $family; }
/** * @return FamilyInterface */ public function createFamily() { $family = new $this->familyClass(); $identifier = $this->attributeRepository->getIdentifier(); $family->addAttribute($identifier); $family->setAttributeAsLabel($identifier); foreach ($this->getChannels() as $channel) { $requirement = $this->factory->createAttributeRequirement($identifier, $channel, true); $family->addAttributeRequirement($requirement); } return $family; }
/** * {@inheritdoc} */ protected function doPerform(FamilyInterface $family) { foreach ($this->attRequirements as $attributeRequirement) { $family->addAttribute($attributeRequirement->getAttribute()); $family->addAttributeRequirement($this->factory->createAttributeRequirement($attributeRequirement->getAttribute(), $attributeRequirement->getChannel(), $attributeRequirement->isRequired())); } }
/** * Create requirements for each families' attributes for the newly created channel * * @param LifecycleEventArgs $event */ public function prePersist(LifecycleEventArgs $event) { $entity = $event->getEntity(); if (!$entity instanceof ChannelInterface) { return; } $entityManager = $event->getEntityManager(); $families = $entityManager->getRepository('PimCatalogBundle:Family')->findAll(); foreach ($families as $family) { foreach ($family->getAttributes() as $attribute) { $requirement = $this->requirementFactory->createAttributeRequirement($attribute, $entity, AttributeTypes::IDENTIFIER === $attribute->getAttributeType()); $requirement->setFamily($family); $entityManager->persist($requirement); } } }
/** * {@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; }
/** * @param AttributeInterface $attribute * @param string $channelCode * * @throws \InvalidArgumentException * * @return AttributeRequirementInterface */ protected function createAttributeRequirement(AttributeInterface $attribute, $channelCode) { $channel = $this->channelRepository->findOneByIdentifier($channelCode); if (null === $channel) { throw new \InvalidArgumentException(sprintf('Channel with "%s" code does not exist', $channelCode)); } return $this->attrRequiFactory->createAttributeRequirement($attribute, $channel, true); }
function it_initializes_attribute_requirements_with_all_channels_and_attributes_in_the_PIM(ChannelRepositoryInterface $channelRepository, ChannelInterface $ecommerceChannel, ChannelInterface $mobileChannel, AttributeRepositoryInterface $attributeRepository, AttributeInterface $nameAttribute, AttributeInterface $descriptionAttribute, AttributeRequirementFactory $factory, AttributeRequirementInterface $nameECommerceRequirement, AttributeRequirementInterface $nameMobileRequirement, AttributeRequirementInterface $descriptionECommerceRequirement, AttributeRequirementInterface $descriptionMobileRequirement) { $channelRepository->findAll()->willReturn([$ecommerceChannel, $mobileChannel]); $attributeRepository->getNonIdentifierAttributes()->willReturn([$nameAttribute, $descriptionAttribute]); $factory->createAttributeRequirement($nameAttribute, $ecommerceChannel, false)->willReturn($nameECommerceRequirement); $nameECommerceRequirement->getAttributeCode()->willReturn('name'); $nameECommerceRequirement->getChannelCode()->willReturn('ecommerce'); $factory->createAttributeRequirement($nameAttribute, $mobileChannel, false)->willReturn($nameMobileRequirement); $nameMobileRequirement->getAttributeCode()->willReturn('name'); $nameMobileRequirement->getChannelCode()->willReturn('mobile'); $factory->createAttributeRequirement($descriptionAttribute, $ecommerceChannel, false)->willReturn($descriptionECommerceRequirement); $descriptionECommerceRequirement->getAttributeCode()->willReturn('description'); $descriptionECommerceRequirement->getChannelCode()->willReturn('ecommerce'); $factory->createAttributeRequirement($descriptionAttribute, $mobileChannel, false)->willReturn($descriptionMobileRequirement); $descriptionMobileRequirement->getAttributeCode()->willReturn('description'); $descriptionMobileRequirement->getChannelCode()->willReturn('mobile'); $this->initialize(); $this->getAttributeRequirements()->toArray()->shouldReturn(['name_ecommerce' => $nameECommerceRequirement, 'name_mobile' => $nameMobileRequirement, 'description_ecommerce' => $descriptionECommerceRequirement, 'description_mobile' => $descriptionMobileRequirement]); }
/** * {@inheritdoc} */ public function initialize() { $this->channels = $this->channelRepository->findAll(); foreach ($this->attributeRepository->getNonIdentifierAttributes() as $attribute) { $this->attributes[(string) $attribute->getGroup()][] = $attribute; foreach ($this->channels as $channel) { $this->addAttributeRequirement($this->factory->createAttributeRequirement($attribute, $channel, false)); } } }
function it_initializes_attribute_requirements_with_all_channels_and_attributes_in_the_PIM(ChannelRepository $channelRepository, Channel $ecommerce, Channel $mobile, AttributeRepository $attributeRepository, AbstractAttribute $name, AbstractAttribute $description, AttributeRequirementFactory $factory, AttributeRequirement $r1, AttributeRequirement $r2, AttributeRequirement $r3, AttributeRequirement $r4) { $channelRepository->findAll()->willReturn([$ecommerce, $mobile]); $attributeRepository->getNonIdentifierAttributes()->willReturn([$name, $description]); $factory->createAttributeRequirement($name, $ecommerce, false)->willReturn($r1); $r1->getAttributeCode()->willReturn('name'); $r1->getChannelCode()->willReturn('ecommerce'); $factory->createAttributeRequirement($name, $mobile, false)->willReturn($r2); $r2->getAttributeCode()->willReturn('name'); $r2->getChannelCode()->willReturn('mobile'); $factory->createAttributeRequirement($description, $ecommerce, false)->willReturn($r3); $r3->getAttributeCode()->willReturn('description'); $r3->getChannelCode()->willReturn('ecommerce'); $factory->createAttributeRequirement($description, $mobile, false)->willReturn($r4); $r4->getAttributeCode()->willReturn('description'); $r4->getChannelCode()->willReturn('mobile'); $this->initialize(); $this->getAttributeRequirements()->toArray()->shouldReturn(['name_ecommerce' => $r1, 'name_mobile' => $r2, 'description_ecommerce' => $r3, 'description_mobile' => $r4]); }
/** * @param FamilyInterface $family * @param AttributeInterface $attribute * @param string $channelCode * * @return AttributeRequirementInterface */ protected function createAttributeRequirement(FamilyInterface $family, AttributeInterface $attribute, $channelCode) { $channel = $this->channelRepository->findOneByIdentifier($channelCode); if (null === $channel) { throw new \InvalidArgumentException(sprintf('Channel with "%s" code does not exist', $channelCode)); } $requirement = $this->requirementRepo->findOneBy(['attribute' => $attribute->getId(), 'channel' => $channel->getId(), 'family' => $family->getId()]); if (null === $requirement) { $requirement = $this->attrRequiFactory->createAttributeRequirement($attribute, $channel, true); } return $requirement; }
/** * @param FamilyInterface $family * @param array $data */ protected function setAttributeRequirements(FamilyInterface $family, array $data) { $requirements = []; foreach ($data as $channelCode => $attributeCodes) { foreach ($attributeCodes as $attributeCode) { if (null !== ($attribute = $this->attributeRepository->findOneByIdentifier($attributeCode))) { if (null !== ($channel = $this->channelRepository->findOneByIdentifier($channelCode))) { $requirements[] = $this->attrRequiFactory->createAttributeRequirement($attribute, $channel, true); } else { throw new \InvalidArgumentException(sprintf('Channel with "%s" code does not exist', $channelCode)); } } else { throw new \InvalidArgumentException(sprintf('Attribute with "%s" code does not exist', $attributeCode)); } } } $family->setAttributeRequirements($requirements); }