示例#1
0
 /**
  * {@inheritdoc}
  */
 public function resolve(OrderInterface $order)
 {
     $stateMachine = $this->stateMachineFactory->get($order, OrderTransitions::GRAPH);
     if ($this->canOrderBeFulfilled($order) && $stateMachine->can(OrderTransitions::TRANSITION_FULFILL)) {
         $stateMachine->apply(OrderTransitions::TRANSITION_FULFILL);
     }
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function apply(RequestConfiguration $configuration, ResourceInterface $resource)
 {
     if (!$configuration->hasStateMachine()) {
         throw new \InvalidArgumentException('State machine must be configured to apply transition, check your routing.');
     }
     $this->stateMachineFactory->get($resource, $configuration->getStateMachineGraph())->apply($configuration->getStateMachineTransition());
 }
 protected function updatePaymentState($payment, $nextState)
 {
     $stateMachine = $this->factory->get($payment, PaymentTransitions::GRAPH);
     if (null !== ($transition = $stateMachine->getTransitionToState($nextState))) {
         $stateMachine->apply($transition);
     }
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function release(\DateTime $expiresAt)
 {
     $orders = $this->repository->findExpired($expiresAt);
     foreach ($orders as $order) {
         $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_RELEASE, true);
     }
     $this->manager->flush();
 }
 /**
  * {@inheritdoc}
  */
 public function resolve(OrderInterface $order)
 {
     $stateMachine = $this->stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH);
     $targetTransition = $this->getTargetTransition($order);
     if (null !== $targetTransition) {
         $this->applyTransition($stateMachine, $targetTransition);
     }
 }
示例#6
0
 /**
  * @param OrderInterface $order
  */
 public function updateOrderShippingState(OrderInterface $order)
 {
     foreach ($order->getShipments() as $shipment) {
         if ($this->factory->get($shipment, ShipmentTransitions::GRAPH)->can(ShipmentTransitions::SYLIUS_SHIP)) {
             return;
         }
     }
     $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_SHIP, true);
 }
示例#7
0
 /**
  * Update order shipping state.
  *
  * @param OrderInterface $order
  */
 public function updateOrderShippingState(OrderInterface $order)
 {
     // Check if all shipments are shipped (= transition "ship" cannot be applied)
     foreach ($order->getShipments() as $shipment) {
         if ($this->factory->get($shipment, ShipmentTransitions::GRAPH)->can(ShipmentTransitions::SYLIUS_SHIP)) {
             return;
         }
     }
     $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_SHIP, true);
 }
示例#8
0
 /**
  * {@inheritdoc}
  */
 public function updateItemStates($items, $transition)
 {
     if (!is_array($items) && !$items instanceof Collection) {
         throw new \InvalidArgumentException('Inventory items value must be array or instance of "Doctrine\\Common\\Collections\\Collection".');
     }
     foreach ($items as $item) {
         if (!$item instanceof ShipmentItemInterface) {
             throw new UnexpectedTypeException($item, 'Sylius\\Component\\Shipping\\Model\\ShipmentItemInterface');
         }
         $this->factory->get($item, ShipmentItemTransitions::GRAPH)->apply($transition, true);
     }
 }
示例#9
0
 /**
  * @param Collection $units
  * @param string $transition
  *
  * @return int
  */
 protected function applyTransition(Collection $units, $transition)
 {
     $quantity = 0;
     foreach ($units as $unit) {
         $stateMachine = $this->stateMachineFactory->get($unit, InventoryUnitTransitions::GRAPH);
         if ($stateMachine->can($transition)) {
             $stateMachine->apply($transition);
             ++$quantity;
         }
     }
     return $quantity;
 }
示例#10
0
 /**
  * {@inheritdoc}
  */
 public function updateUnitStates($units, $transition)
 {
     if (!is_array($units) && !$units instanceof Collection) {
         throw new \InvalidArgumentException('Shipping units value must be array or instance of "Doctrine\\Common\\Collections\\Collection".');
     }
     foreach ($units as $unit) {
         if (!$unit instanceof ShipmentUnitInterface) {
             throw new UnexpectedTypeException($unit, 'Sylius\\Component\\Shipping\\Model\\ShipmentUnitInterface');
         }
         $this->factory->get($unit, ShipmentUnitTransitions::GRAPH)->apply($transition, true);
     }
 }
示例#11
0
 /**
  * {@inheritdoc}
  */
 public function resolveShippingState(OrderInterface $order)
 {
     if (OrderShippingStates::STATE_SHIPPED === $order->getShippingState()) {
         return;
     }
     /** @var StateMachineInterface $stateMachine */
     $stateMachine = $this->stateMachineFactory->get($order, OrderShippingTransitions::GRAPH);
     if ($this->allShipmentsInStateButOrderStateNotUpdated($order, ShipmentInterface::STATE_SHIPPED, OrderShippingStates::STATE_SHIPPED)) {
         $stateMachine->apply(OrderShippingTransitions::TRANSITION_SHIP);
     }
     if ($this->isPartiallyShippedButOrderStateNotUpdated($order)) {
         $stateMachine->apply(OrderShippingTransitions::TRANSITION_PARTIALLY_SHIP);
     }
 }
 /**
  * Apply a transition to the object that has just undergone a transition
  *
  * @param \Traversable|array $objects    Object or array|traversable of objects to apply the transition on
  * @param TransitionEvent    $event      Transition event
  * @param string|null        $transition Transition that is to be applied (if null, same as the trigger)
  * @param string|null        $graph      Graph on which the new transition will apply (if null, same as the trigger)
  * @param bool               $soft       If true, check if it can apply the transition first (no Exception thrown)
  */
 public function apply($objects, TransitionEvent $event, $transition = null, $graph = null, $soft = true)
 {
     if (!is_array($objects) && !$objects instanceof \Traversable) {
         $objects = array($objects);
     }
     if (null === $transition) {
         $transition = $event->getTransition();
     }
     if (null === $graph) {
         $graph = $event->getStateMachine()->getGraph();
     }
     foreach ($objects as $object) {
         $this->factory->get($object, $graph)->apply($transition, $soft);
     }
 }
示例#13
0
 public function updateOrderOnPayment(PaymentInterface $payment)
 {
     $order = $payment->getOrder();
     if (null === $order) {
         throw new \RuntimeException(sprintf('Cannot retrieve Order from Payment with id %s', $payment->getId()));
     }
     $total = 0;
     foreach ($order->getPayments() as $payment) {
         if ($payment->getState() === PaymentInterface::STATE_COMPLETED) {
             $total += $payment->getAmount();
         }
     }
     if ($total === $order->getTotal()) {
         $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_CONFIRM, true);
     }
 }
示例#14
0
 function it_applies_configured_state_machine_transition(RequestConfiguration $requestConfiguration, ResourceInterface $resource, FactoryInterface $stateMachineFactory, StateMachineInterface $stateMachine)
 {
     $requestConfiguration->hasStateMachine()->willReturn(true);
     $requestConfiguration->getStateMachineGraph()->willReturn('sylius_product_review_state');
     $requestConfiguration->getStateMachineTransition()->willReturn('reject');
     $stateMachineFactory->get($resource, 'sylius_product_review_state')->willReturn($stateMachine);
     $stateMachine->apply('reject')->shouldBeCalled();
     $this->apply($requestConfiguration, $resource);
 }
 function it_does_not_mark_order_as_fulfilled_when_it_has_been_shipped_but_not_paid(FactoryInterface $stateMachineFactory, OrderInterface $order, StateMachineInterface $stateMachine)
 {
     $order->getShippingState()->willReturn(OrderShippingStates::STATE_SHIPPED);
     $order->getPaymentState()->willReturn(Argument::not(OrderPaymentStates::STATE_PAID));
     $stateMachineFactory->get($order, OrderTransitions::GRAPH)->willReturn($stateMachine);
     $stateMachine->can(OrderTransitions::TRANSITION_FULFILL)->willReturn(true);
     $stateMachine->apply(OrderTransitions::TRANSITION_FULFILL)->shouldNotBeCalled();
     $this->resolve($order);
 }
 /**
  * @param ReviewInterface $productReview
  * @param string $targetState
  */
 private function applyReviewTransition(ReviewInterface $productReview, $targetState)
 {
     /** @var StateMachineInterface $stateMachine */
     $stateMachine = $this->stateMachineFactory->get($productReview, 'sylius_product_review');
     $transition = $stateMachine->getTransitionToState($targetState);
     if (null !== $transition) {
         $stateMachine->apply($transition);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function resolve(OrderInterface $order)
 {
     $stateMachine = $this->stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH);
     $completedPaymentTotal = 0;
     $payments = $order->getPayments()->filter(function (PaymentInterface $payment) {
         return PaymentInterface::STATE_COMPLETED === $payment->getState();
     });
     foreach ($payments as $payment) {
         $completedPaymentTotal += $payment->getAmount();
     }
     if (0 < $payments->count() && $completedPaymentTotal >= $order->getTotal()) {
         $this->applyTransition($stateMachine, OrderPaymentTransitions::TRANSITION_PAY);
         return;
     }
     if ($completedPaymentTotal < $order->getTotal() && 0 < $completedPaymentTotal) {
         $this->applyTransition($stateMachine, OrderPaymentTransitions::TRANSITION_PARTIALLY_PAY);
         return;
     }
 }
 /**
  * Applies the state to the subscriptions.
  *
  * @param array|SubscriptionInterface[] $subscriptions
  * @param string $transition
  */
 protected function applyTransition(array $subscriptions, $transition)
 {
     foreach ($subscriptions as $subscription) {
         $stateMachine = $this->factory->get($subscription);
         if ($stateMachine->can($transition)) {
             $stateMachine->apply($transition);
             $this->manager->persist($subscription);
         }
     }
     $this->manager->flush();
 }
 /**
  * @param PaymentInterface $payment
  * @param string $targetState
  */
 private function applyRequiredTransition(PaymentInterface $payment, $targetState)
 {
     if ($targetState === $payment->getState()) {
         return;
     }
     /** @var StateMachineInterface $stateMachine */
     $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
     $targetTransition = $stateMachine->getTransitionToState($targetState);
     if (null !== $targetTransition) {
         $stateMachine->apply($targetTransition);
     }
 }
 function it_does_not_mark_an_order_if_it_is_already_in_this_shipping_state(FactoryInterface $stateMachineFactory, OrderInterface $order, ShipmentInterface $shipment1, ShipmentInterface $shipment2, StateMachineInterface $orderStateMachine)
 {
     $shipments = new ArrayCollection();
     $shipments->add($shipment1->getWrappedObject());
     $shipments->add($shipment2->getWrappedObject());
     $order->getShipments()->willReturn($shipments);
     $order->getShippingState()->willReturn(OrderShippingStates::STATE_SHIPPED);
     $stateMachineFactory->get($order, OrderShippingTransitions::GRAPH)->willReturn($orderStateMachine);
     $shipment1->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
     $shipment2->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
     $orderStateMachine->apply(OrderShippingTransitions::TRANSITION_SHIP)->shouldNotBeCalled();
     $this->resolve($order);
 }
示例#21
0
 public function updateOrderOnPayment(PaymentInterface $payment)
 {
     /** @var $order OrderInterface */
     $order = $payment->getOrder();
     if (null === $order) {
         throw new \RuntimeException(sprintf('Cannot retrieve Order from Payment with id %s', $payment->getId()));
     }
     $total = 0;
     if (PaymentInterface::STATE_COMPLETED === $payment->getState()) {
         $payments = $order->getPayments()->filter(function (PaymentInterface $payment) {
             return PaymentInterface::STATE_COMPLETED === $payment->getState();
         });
         if ($payments->count() === $order->getPayments()->count()) {
             $order->setPaymentState(PaymentInterface::STATE_COMPLETED);
         }
         $total += $payment->getAmount();
     } else {
         $order->setPaymentState($payment->getState());
     }
     if ($total >= $order->getTotal()) {
         $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_CONFIRM, true);
     }
 }
示例#22
0
 /**
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $request = $event->getRequest();
     if (!$this->requestMatcher->matches($request)) {
         return;
     }
     /** @var OrderInterface $order */
     $order = $this->cartContext->getCart();
     if ($order->isEmpty()) {
         $event->setResponse(new RedirectResponse($this->urlGenerator->generate('sylius_shop_cart_summary')));
     }
     $stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
     if ($stateMachine->can($this->getRequestedTransition($request))) {
         return;
     }
     if (null !== ($referer = $this->getReferer($request))) {
         $event->setResponse(new RedirectResponse($referer));
         return;
     }
     $event->setResponse(new RedirectResponse($this->urlGenerator->generateForOrderCheckoutState($order)));
 }
 function it_provides_payment_in_configured_state_with_default_payment_method(DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver, OrderInterface $order, PaymentFactoryInterface $paymentFactory, PaymentInterface $newPayment, PaymentMethodInterface $paymentMethod, StateMachineFactoryInterface $stateMachineFactory, StateMachineInterface $stateMachine)
 {
     $order->getTotal()->willReturn(1000);
     $order->getCurrencyCode()->willReturn('USD');
     $order->getLastPayment(PaymentInterface::STATE_CANCELLED)->willReturn(null);
     $order->getLastPayment(PaymentInterface::STATE_FAILED)->willReturn(null);
     $paymentFactory->createWithAmountAndCurrencyCode(1000, 'USD')->willReturn($newPayment);
     $newPayment->setOrder($order)->shouldBeCalled();
     $defaultPaymentMethodResolver->getDefaultPaymentMethod($newPayment)->willReturn($paymentMethod);
     $newPayment->setMethod($paymentMethod)->shouldBeCalled();
     $newPayment->getState()->willReturn(PaymentInterface::STATE_CART);
     $stateMachineFactory->get($newPayment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
     $stateMachine->getTransitionToState(PaymentInterface::STATE_NEW)->willReturn(PaymentTransitions::TRANSITION_CREATE);
     $stateMachine->apply(PaymentTransitions::TRANSITION_CREATE)->shouldBeCalled();
     $this->provideOrderPayment($order, PaymentInterface::STATE_NEW)->shouldReturn($newPayment);
 }
 function it_marks_order_as_partially_paid_if_one_of_the_payment_is_processing(FactoryInterface $stateMachineFactory, StateMachineInterface $stateMachine, OrderInterface $order, PaymentInterface $payment1, PaymentInterface $payment2)
 {
     $payment1->getAmount()->willReturn(6000);
     $payment1->getState()->willReturn(PaymentInterface::STATE_PROCESSING);
     $payment2->getAmount()->willReturn(4000);
     $payment2->getState()->willReturn(PaymentInterface::STATE_COMPLETED);
     $payments = new ArrayCollection([$payment1->getWrappedObject(), $payment2->getWrappedObject()]);
     $order->hasPayments()->willReturn(true);
     $order->getPayments()->willReturn($payments);
     $order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AWAITING_PAYMENT);
     $order->getTotal()->willReturn(10000);
     $stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
     $stateMachine->can(OrderPaymentTransitions::TRANSITION_PARTIALLY_PAY)->willReturn(true);
     $stateMachine->apply(OrderPaymentTransitions::TRANSITION_PARTIALLY_PAY)->shouldBeCalled();
     $this->resolve($order);
 }
示例#25
0
 /**
  * @param ProductInterface $product
  * @param string $title
  * @param int $rating
  * @param string $comment
  * @param CustomerInterface|null $customer
  * @param string $transition
  *
  * @return ReviewInterface
  */
 private function createProductReview(ProductInterface $product, $title, $rating, $comment, CustomerInterface $customer = null, $transition = ProductReviewTransitions::TRANSITION_ACCEPT)
 {
     /** @var ReviewInterface $review */
     $review = $this->productReviewFactory->createNew();
     $review->setTitle($title);
     $review->setRating($rating);
     $review->setComment($comment);
     $review->setReviewSubject($product);
     $review->setAuthor($customer);
     $product->addReview($review);
     if (null !== $transition) {
         $stateMachine = $this->stateMachineFactory->get($review, ProductReviewTransitions::GRAPH);
         $stateMachine->apply($transition);
     }
     $this->sharedStorage->set('product_review', $review);
     return $review;
 }
 function it_provide_sm_getPossibleTransitions_function(FactoryInterface $factory, StateMachineInterface $stateMachine)
 {
     $this->getPossibleTransitions($object = new DummyObject(), 'simple');
     $factory->get($object, 'simple')->shouldHaveBeenCalled();
     $stateMachine->getPossibleTransitions()->shouldHaveBeenCalled();
 }
示例#27
0
 /**
  * @param OrderInterface $order
  * @param string $transition
  */
 private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition)
 {
     $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH)->apply($transition);
 }
示例#28
0
 /**
  * @param OrderInterface $order
  * @param string $transition
  */
 private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition)
 {
     $shipment = $order->getShipments()->first();
     $this->stateMachineFactory->get($shipment, ShipmentTransitions::GRAPH)->apply($transition);
 }
示例#29
0
 /**
  * Purge an order.
  *
  * @param OrderInterface $order
  */
 protected function purgeOrder(OrderInterface $order)
 {
     $this->factory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_ABANDON);
     $this->manager->persist($order);
 }
示例#30
0
 /**
  * @param object $object
  * @param string $graph
  *
  * @return array
  */
 public function getPossibleTransitions($object, $graph = 'default')
 {
     return $this->factory->get($object, $graph)->getPossibleTransitions();
 }