/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addModelTransformer(new TaggingTransformer($this->tagRepository, $options['delimiter'])); $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($options) { $tags = $event->getData(); $event->setData(is_array($tags) ? implode($options['delimiter'], $tags) : $tags); }); // TODO: BUG FIXME not work when object have no id (none persited object) $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { $object = $event->getForm()->getParent()->getData(); if (!$object instanceof TagsAwareInterface || !$object instanceof OriginContextInterface) { return; } $tags = $this->tagRepository->resolveWithString($event->getData()) ?: array(); $object->setTags(null); /** @var TaggingInterface[] $oldTaggings */ $oldTaggings = $this->taggingRepository->findBy(array('originAlias' => $object->getOriginalAlias(), 'originId' => $object->getId())); foreach ($oldTaggings as $oldTagging) { if (!in_array($oldTagging->getTag(), $tags)) { $this->taggingRepository->getManager()->remove($oldTagging); } } $this->taggingRepository->getManager()->flush(); foreach ($tags as $tag) { $tagging = $this->taggingRepository->findWithTagAndAlias($tag, $object, true); $this->originator->setOrigin($tagging, $object); $this->taggingRepository->add($tagging); $object->addTag($tag); } }); }
/** * @param PromotionInterface $promotion * * @return AdjustmentInterface */ protected function createAdjustment(PromotionInterface $promotion) { $adjustment = $this->adjustmentFactory->createNew(); $adjustment->setType(AdjustmentInterface::PROMOTION_ADJUSTMENT); $adjustment->setDescription($promotion->getDescription()); $this->originator->setOrigin($adjustment, $promotion); return $adjustment; }
/** * @param PromotionInterface $promotion * @param string $type * * @return AdjustmentInterface */ protected function createAdjustment(PromotionInterface $promotion, $type = AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) { $adjustment = $this->adjustmentFactory->createNew(); $adjustment->setType($type); $adjustment->setLabel($promotion->getName()); $this->originator->setOrigin($adjustment, $promotion); return $adjustment; }
/** * @param OrderItemUnitInterface $unit * @param PromotionInterface $promotion */ private function removeUnitOrderPromotionAdjustmentsByOrigin(OrderItemUnitInterface $unit, PromotionInterface $promotion) { foreach ($unit->getAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) { if ($promotion === $this->originator->getOrigin($adjustment)) { $unit->removeAdjustment($adjustment); } } }
function it_reverts_order_units_order_promotion_adjustments(AdjustmentInterface $firstAdjustment, AdjustmentInterface $secondAdjustment, OrderInterface $order, OrderItemInterface $item, OrderItemUnitInterface $unit, OriginatorInterface $originator, PromotionInterface $otherPromotion, PromotionInterface $promotion) { $order->countItems()->willReturn(1); $order->getItems()->willReturn(new \ArrayIterator([$item->getWrappedObject()])); $item->getUnits()->willReturn(new \ArrayIterator([$unit->getWrappedObject()])); $unit->getAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)->willReturn(new \ArrayIterator([$firstAdjustment->getWrappedObject(), $secondAdjustment->getWrappedObject()])); $originator->getOrigin($firstAdjustment)->willReturn($promotion); $originator->getOrigin($secondAdjustment)->willReturn($otherPromotion); $unit->removeAdjustment($firstAdjustment)->shouldBeCalled(); $unit->removeAdjustment($secondAdjustment)->shouldNotBeCalled(); $this->revert($order, [], $promotion); }
function it_does_not_distribute_0_amount_to_unit_even_if_its_middle_element(AdjustmentFactoryInterface $adjustmentFactory, AdjustmentInterface $adjustment, IntegerDistributorInterface $distributor, OrderInterface $order, OrderItemInterface $coltItem, OrderItemUnitInterface $firstColtUnit, OrderItemUnitInterface $secondColtUnit, OriginatorInterface $originator, PromotionInterface $promotion) { $order->countItems()->willReturn(1); $order->getItems()->willReturn(new ArrayCollection([$coltItem->getWrappedObject()])); $coltItem->getQuantity()->willReturn(2); $distributor->distribute(1, 2)->willReturn([1, 0]); $coltItem->getUnits()->willReturn(new ArrayCollection([$firstColtUnit->getWrappedObject(), $secondColtUnit->getWrappedObject()])); $promotion->getName()->willReturn('Winter guns promotion!'); $adjustmentFactory->createWithData(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT, 'Winter guns promotion!', 1)->willReturn($adjustment); $originator->setOrigin($adjustment, $promotion)->shouldBeCalled(); $firstColtUnit->addAdjustment($adjustment)->shouldBeCalled(); $secondColtUnit->addAdjustment(Argument::any())->shouldNotBeCalled(); $this->apply($order, $promotion, [1]); }
/** * @param PromotionInterface $promotion * @param OrderItemUnitInterface $unit * @param int $amount */ private function addAdjustment(PromotionInterface $promotion, OrderItemUnitInterface $unit, $amount) { $adjustment = $this->adjustmentFactory->createWithData(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT, $promotion->getName(), $amount); $this->originator->setOrigin($adjustment, $promotion); $unit->addAdjustment($adjustment); }