Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Finite State Machine #1426

Merged
merged 7 commits into from
Apr 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: @SyliusCoreBundle/Resources/config/app/main.yml }
- { resource: @SyliusPaymentBundle/Resources/config/finite.yml }
- { resource: @SyliusShippingBundle/Resources/config/finite.yml }
- { resource: @SyliusOrderBundle/Resources/config/finite.yml }
- { resource: @SyliusInventoryBundle/Resources/config/finite.yml }
- { resource: @SyliusCoreBundle/Resources/config/finite.yml }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be moved to main.yml file in CoreBundle.


framework:
translator: { fallback: %sylius.locale% }
Expand Down
75 changes: 75 additions & 0 deletions app/migrations/Version20140424094036.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Sylius\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Sylius\Component\Order\Model\OrderInterface;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20140424094036 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

$this->addSql("ALTER TABLE sylius_order CHANGE state state VARCHAR(255) NOT NULL");
}

public function postUp(Schema $schema)
{
$this->addSql(sprintf("UPDATE sylius_order SET state = CASE state
WHEN 1 THEN '%s'
WHEN 2 THEN '%s'
WHEN 3 THEN '%s'
WHEN 4 THEN '%s'
WHEN 5 THEN '%s'
WHEN 6 THEN '%s'
WHEN 7 THEN '%s'
WHEN 8 THEN '%s' END",

OrderInterface::STATE_CART,
OrderInterface::STATE_CART_LOCKED,
OrderInterface::STATE_PENDING,
OrderInterface::STATE_CONFIRMED,
OrderInterface::STATE_SHIPPED,
OrderInterface::STATE_ABANDONED,
OrderInterface::STATE_CANCELLED,
OrderInterface::STATE_RETURNED
));
}

public function preDown(Schema $schema)
{
$this->addSql(sprintf("UPDATE sylius_order SET state = CASE state
WHEN '%s' THEN 1
WHEN '%s' THEN 2
WHEN '%s' THEN 3
WHEN '%s' THEN 4
WHEN '%s' THEN 5
WHEN '%s' THEN 6
WHEN '%s' THEN 7
WHEN '%s' THEN 8 END",

OrderInterface::STATE_CART,
OrderInterface::STATE_CART_LOCKED,
OrderInterface::STATE_PENDING,
OrderInterface::STATE_CONFIRMED,
OrderInterface::STATE_SHIPPED,
OrderInterface::STATE_ABANDONED,
OrderInterface::STATE_CANCELLED,
OrderInterface::STATE_RETURNED
));
}

public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

$this->addSql("ALTER TABLE sylius_order CHANGE state state INT NOT NULL");
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 For the migration. :)

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"symfony/swiftmailer-bundle": "2.3.*",
"symfony/symfony": "~2.3",
"twig/extensions": "1.0.*",
"white-october/pagerfanta-bundle": "1.0.*@dev"
"white-october/pagerfanta-bundle": "1.0.*@dev",
"yohang/finite": "~1.1@dev"
},
"require-dev": {
"behat/behat": "~2.4",
Expand Down
77 changes: 74 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/Sylius/Bundle/CoreBundle/Checkout/Step/PurchaseStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sylius\Bundle\PayumBundle\Payum\Request\StatusRequest;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\SyliusCheckoutEvents;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Payment\SyliusPaymentEvents;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -65,12 +66,18 @@ public function forwardAction(ProcessContextInterface $context)
}

$payment = $order->getPayment();
$previousState = $order->getPayment()->getState();
$payment->setState($status->getStatus());
$previousState = $payment->getState();
$nextState = $status->getStatus();

$stateMachine = $this->get('finite.factory')->get($payment, PaymentTransitions::GRAPH);

if (null !== $transition = $stateMachine->getTransitionToState($nextState)) {
$stateMachine->apply($transition);
}

$this->dispatchCheckoutEvent(SyliusCheckoutEvents::PURCHASE_PRE_COMPLETE, $order);

if ($previousState !== $payment->getState()) {
if ($previousState !== $nextState) {
$this->dispatchEvent(
SyliusPaymentEvents::PRE_STATE_CHANGE,
new GenericEvent($order->getPayment(), array('previous_state' => $previousState))
Expand All @@ -79,7 +86,7 @@ public function forwardAction(ProcessContextInterface $context)

$this->getDoctrine()->getManager()->flush();

if ($previousState !== $payment->getState()) {
if ($previousState !== $nextState) {
$this->dispatchEvent(
SyliusPaymentEvents::POST_STATE_CHANGE,
new GenericEvent($order->getPayment(), array('previous_state' => $previousState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@

namespace Sylius\Bundle\CoreBundle\EventListener;

use Finite\Factory\FactoryInterface;
use Sylius\Bundle\ResourceBundle\Exception\UnexpectedTypeException;
use Sylius\Component\Order\Model\OrderInterface;
use Sylius\Component\Order\OrderTransitions;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* @author Alexandre Bacco <alexandre.bacco@gmail.com>
*/
class ConfirmOrderListener
{
protected $finiteFactory;

public function __construct(FactoryInterface $finiteFactory)
{
$this->finiteFactory = $finiteFactory;
}

/**
* Set an Order as completed
*
Expand All @@ -28,16 +37,20 @@ class ConfirmOrderListener
* @throws UnexpectedTypeException
*/
public function confirmOrder(GenericEvent $event)
{
$order = $this->getOrder($event);

$this->finiteFactory->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_CONFIRM);
}

protected function getOrder(GenericEvent $event)
{
$order = $event->getSubject();

if (!$order instanceof OrderInterface) {
throw new UnexpectedTypeException(
$order,
'Sylius\Component\Order\Model\OrderInterface'
);
throw new UnexpectedTypeException($order, 'Sylius\Component\Order\Model\OrderInterface');
}

$order->setState(OrderInterface::STATE_CONFIRMED);
return $order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Sylius\Bundle\CoreBundle\EventListener;

use Finite\Factory\FactoryInterface;
use Sylius\Component\Core\Model\InventoryUnitInterface;
use Sylius\Component\Order\InventoryUnitTransitions;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Component\Shipping\Model\ShipmentInterface;
use Sylius\Component\Shipping\ShipmentItemTransitions;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
Expand All @@ -23,6 +26,13 @@
*/
class InventoryUnitListener
{
protected $factory;

public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}

/**
* Update inventory unit state.
*
Expand All @@ -34,37 +44,18 @@ public function updateState(GenericEvent $event)
{
$unit = $this->getInventoryUnit($event);

$state = $event->getArgument('state');

$unit->setInventoryState($state);
$transitionName = $event->getArgument('transition');

switch ($state) {
case $unit::STATE_BACKORDERED:
$unit->setShippingState(ShipmentInterface::STATE_ONHOLD);
break;

case $unit::STATE_SOLD:
$unit->setShippingState(ShipmentInterface::STATE_READY);
break;

case $unit::STATE_RETURNED:
$unit->setShippingState(ShipmentInterface::STATE_RETURNED);
break;

default:
throw new \InvalidArgumentException(sprintf('Unexpected inventory state "%s".', $state));
}
$this->factory->get($unit, InventoryUnitTransitions::GRAPH)->apply($transitionName);
$this->factory->get($unit, ShipmentItemTransitions::GRAPH)->apply($transitionName);
}

private function getInventoryUnit(GenericEvent $event)
{
$unit = $event->getSubject();

if (!$unit instanceof InventoryUnitInterface) {
throw new UnexpectedTypeException(
$unit,
'Sylius\Component\Core\Model\InventoryUnitInterface'
);
throw new UnexpectedTypeException($unit, 'Sylius\Component\Core\Model\InventoryUnitInterface');
}

return $unit;
Expand Down
Loading