function it_builds_form_with_proper_fields(FormBuilderInterface $builder, TaxonRepositoryInterface $taxonRepository) { $taxonRepository->getClassName()->willReturn('taxon'); $builder->add('taxon', 'sylius_taxon_from_identifier', Argument::type('array'))->shouldBeCalled()->willReturn($builder); $builder->add('count', 'integer', Argument::type('array'))->shouldBeCalled()->willReturn($builder); $this->buildForm($builder, []); }
/** * @Transform /^classified as "([^"]+)"$/ * @Transform /^belongs to "([^"]+)"$/ */ public function getTaxonByName($taxonName) { $taxon = $this->taxonRepository->findOneByName($taxonName); if (null === $taxon) { throw new \InvalidArgumentException(sprintf('Taxon with name "%s" does not exist.', $taxonName)); } return $taxon; }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $taxonomies = $this->taxonomyRepository->findAll(); $builder->addModelTransformer(new $options['model_transformer']['class']($taxonomies, $options['model_transformer']['save_objects'])); foreach ($taxonomies as $taxonomy) { /* @var $taxonomy TaxonomyInterface */ $builder->add($taxonomy->getId(), 'choice', array('choice_list' => new ObjectChoiceList($this->taxonRepository->getTaxonsAsList($taxonomy), null, array(), null, 'id'), 'multiple' => $options['multiple'], 'label' => $taxonomy->getName())); } }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['choices' => function (Options $options) { $taxons = $this->taxonRepository->findNodesTreeSorted(); if (null !== $options['filter']) { $taxons = array_filter($taxons, $options['filter']); } return $taxons; }, 'choice_value' => 'id', 'choice_label' => 'name', 'choice_translation_domain' => false, 'root' => null, 'filter' => null])->setAllowedTypes('root', [TaxonInterface::class, 'string', 'null'])->setAllowedTypes('filter', ['callable', 'null']); }
/** * {@inheritdoc} */ public function transform($value) { if (!is_array($value)) { throw new UnexpectedTypeException($value, 'array'); } if (empty($value)) { return new ArrayCollection(); } return new ArrayCollection($this->taxonRepository->findBy(['code' => $value['taxons']])); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $choiceList = function (Options $options) { $taxons = $this->taxonRepository->getTaxonsAsList($options['taxonomy']); if (null !== $options['filter']) { $taxons = array_filter($taxons, $options['filter']); } return new ObjectChoiceList($taxons, null, array(), null, 'id'); }; $resolver->setDefaults(array('choice_list' => $choiceList))->setRequired(array('taxonomy', 'filter'))->setAllowedTypes('taxonomy', TaxonomyInterface::class)->setAllowedTypes('filter', ['callable', 'null']); }
/** * {@inheritdoc} */ public function generate($name, $parentId = null) { $taxonSlug = Transliterator::transliterate($name); if (null === $parentId) { return $taxonSlug; } /** @var TaxonInterface $parent */ $parent = $this->taxonRepository->find($parentId); Assert::notNull($parent, sprintf('There is no parent taxon with id %d.', $parentId)); return $parent->getSlug() . self::SLUG_SEPARATOR . $taxonSlug; }
/** * {@inheritdoc} */ public function create(array $options = []) { $options = $this->optionsResolver->resolve($options); /** @var TaxonInterface $taxon */ $taxon = $this->taxonRepository->findOneBy(['code' => $options['code']]); if (null === $taxon) { $taxon = $this->taxonFactory->createNew(); } $taxon->setCode($options['code']); foreach ($this->getLocales() as $localeCode) { $taxon->setCurrentLocale($localeCode); $taxon->setFallbackLocale($localeCode); $taxon->setName($options['name']); $taxon->setDescription($options['description']); } foreach ($options['children'] as $childOptions) { $taxon->addChild($this->create($childOptions)); } return $taxon; }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { if (!$subject instanceof OrderInterface) { throw new UnexpectedTypeException($subject, OrderInterface::class); } if (!isset($configuration['taxon']) || !isset($configuration['count'])) { return false; } $targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]); if (null === $targetTaxon) { return false; } $validProducts = 0; foreach ($subject->getItems() as $item) { if (!$item->getProduct()->hasTaxon($targetTaxon)) { continue; } $validProducts += $item->getQuantity(); } return $validProducts >= $configuration['count']; }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { if (!$subject instanceof OrderInterface) { throw new UnexpectedTypeException($subject, OrderInterface::class); } if (!isset($configuration['taxon']) || !isset($configuration['amount'])) { return false; } $targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]); if (null === $targetTaxon) { return false; } $itemsWithTaxonTotal = 0; /** @var OrderItemInterface $item */ foreach ($subject->getItems() as $item) { if ($item->getProduct()->hasTaxon($targetTaxon)) { $itemsWithTaxonTotal += $item->getTotal(); } } return $itemsWithTaxonTotal >= $configuration['amount']; }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { Assert::isInstanceOf($subject, OrderInterface::class); $channelCode = $subject->getChannel()->getCode(); if (!isset($configuration[$channelCode])) { return false; } $configuration = $configuration[$channelCode]; if (!isset($configuration['taxon']) || !isset($configuration['amount'])) { return false; } $targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]); if (null === $targetTaxon) { return false; } $itemsWithTaxonTotal = 0; /** @var OrderItemInterface $item */ foreach ($subject->getItems() as $item) { if (!$item->getProduct()->filterProductTaxonsByTaxon($targetTaxon)->isEmpty()) { $itemsWithTaxonTotal += $item->getTotal(); } } return $itemsWithTaxonTotal >= $configuration['amount']; }
function it_transforms_only_existing_taxons(TaxonRepositoryInterface $taxonRepository, TaxonInterface $bows) { $taxonRepository->findBy(['code' => ['bows', 'swords']])->willReturn([$bows]); $taxons = new ArrayCollection([$bows->getWrappedObject()]); $this->transform(['taxons' => ['bows', 'swords']])->shouldBeCollection($taxons); }
function it_throws_exception_if_parent_taxon_with_given_id_does_not_exist(TaxonRepositoryInterface $taxonRepository) { $taxonRepository->find(1)->willReturn(null); $this->shouldThrow(new \InvalidArgumentException('There is no parent taxon with id 1.'))->during('generate', ['Battle games', 1]); }
function it_returns_false_if_taxon_with_configured_code_does_not_exist(OrderInterface $order, TaxonRepositoryInterface $taxonRepository) { $taxonRepository->findOneBy(['code' => 'bows'])->willReturn(null); $this->isEligible($order, ['taxon' => 'bows', 'count' => 10])->shouldReturn(false); }
function it_returns_false_if_taxon_with_configured_code_cannot_be_found(TaxonRepositoryInterface $taxonRepository, OrderInterface $order) { $taxonRepository->findOneBy(['code' => 'sniper_rifles'])->willReturn(null); $this->isEligible($order, ['taxon' => 'sniper_rifles', 'amount' => 1000])->shouldReturn(false); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('taxon', 'sylius_taxon_from_identifier', ['label' => 'sylius.form.promotion_rule.contains_taxon.taxon', 'class' => $this->taxonRepository->getClassName(), 'query_builder' => function () { return $this->taxonRepository->getFormQueryBuilder(); }, 'identifier' => 'code'])->add('count', 'integer', ['label' => 'sylius.form.promotion_rule.contains_taxon.count']); }
function it_returns_false_if_taxon_with_configured_code_cannot_be_found(ChannelInterface $channel, OrderInterface $order, TaxonRepositoryInterface $taxonRepository) { $order->getChannel()->willReturn($channel); $channel->getCode()->willReturn('WEB_US'); $taxonRepository->findOneBy(['code' => 'sniper_rifles'])->willReturn(null); $this->isEligible($order, ['WEB_US' => ['taxon' => 'sniper_rifles', 'amount' => 1000]])->shouldReturn(false); }
/** * @Transform /^taxon with "([^"]+)" code$/ */ public function getTaxonByCode($code) { $taxon = $this->taxonRepository->findOneBy(['code' => $code]); Assert::notNull($taxon, sprintf('Taxon with code "%s" does not exist.', $code)); return $taxon; }