Skip to main content
Version: 4.1

State Machine - Callbacks

It's really simple to create a custom state machine callback.

After Callbacks

In this example we want to register a simple listener which gets triggered after a customer successfully placed a order:

core_shop_workflow:
state_machine:
coreshop_order:
callbacks:
after:
do_something_special:
on: ['confirm']
do: ['@App\CoreShop\EventListener\SpecialListener', 'doSomething']
args: ['object']
priority: -10 # fire action early!
NameDescription
ontransition name
doservice and method to dispatch
argsobject or event. Object type depends on state machine type.
priorityset priority. default is 0

And your Service:

<?php

namespace App\CoreShop\EventListener;

use CoreShop\Component\Core\Model\CustomerInterface;
use CoreShop\Component\Core\Model\OrderInterface;

final class SpecialListener
{
/**
* @param OrderInterface $order
*/
public function doSomething(OrderInterface $order)
{
/** @var CustomerInterface $customer */
$customer = $order->getCustomer();

/** @var string $locale */
$locale = $order->getLocaleCode();

// your very special code.
}
}

Before Callbacks

In this example we want to register a simple listener which gets triggered before a the shipment transaction ready gets applied:

core_shop_workflow:
state_machine:
coreshop_shipment:
callbacks:
before:
check_something:
on: ['create']
do: ['@App\CoreShop\EventListener\SpecialListener', 'checkSomething']
args: ['object']
priority: 0
NameDescription
ontransition name
doservice and method to dispatch
argsobject or event. Object type depends on state machine type.
priorityset priority. default is 0

As you can see in the class below, the checkSomething() method throws an exception. This prevents the state machine from switching to the ready state. Just remove the exception and the transition gets applied as expected.

<?php

namespace App\CoreShop\EventListener;

use CoreShop\Component\Core\Model\OrderShipmentInterface;

final class SpecialListener
{
/**
* @param OrderShipmentInterface $shipment
*/
public function checkSomething(OrderShipmentInterface $shipment)
{
// check something and throw an exeption
throw new \Exception('do something here...');
}
}