public function onStateChange(OrderEvent $event)
    {
        $order = $event->getOrder();

        // If new state is Accepted or Completed and type is not order
        if (in_array($order->getState(), [OrderStates::STATE_ACCEPTED, OrderStates::STATE_COMPLETED])
            && $order->getType() != OrderTypes::TYPE_ORDER
        ) {
            // Transform to an order
            $order
                ->setType(OrderTypes::TYPE_ORDER)
                ->setCreatedAt(new \DateTime())
                ->setNumber(null)
                ->setKey(null)
            ;

            $this->generateNumberAndKey($order);
            $this->handleAddresses($order);

            $errorList = $this->validator->validate($order, ['Default', 'Order']);
            if ($errorList->count() > 0) {
                $messages = [];
                foreach ($errorList as $error) {
                    $messages[] = $error->getMessage();
                }
                throw new OrderException('Invalid order : ' . implode('<br>', $messages));
            }
        }

        // Set completed at
        if ($order->getState() === OrderStates::STATE_COMPLETED && null === $order->getCompletedAt()) {
            $order->setCompletedAt(new \DateTime());
        }
    }
public function onStateChange(OrderEvent $event)
    {
        $order = $event->getOrder();

        // If new state is Accepted or Completed and type is not order
        if (in_array($order->getState(), [OrderStates::STATE_ACCEPTED, OrderStates::STATE_COMPLETED])
            && $order->getType() != OrderTypes::TYPE_ORDER
        ) {
            // Transform to an order
            $order
                ->setType(OrderTypes::TYPE_ORDER)
                ->setCreatedAt(new \DateTime())
                ->setNumber(null)
                ->setKey(null)
            ;

            $this->generateNumberAndKey($order);
            $this->handleAddresses($order);

            $errorList = $this->validator->validate($order, ['Default', 'Order']);
            if ($errorList->count() > 0) {
                $messages = [];
                foreach ($errorList as $error) {
                    $messages[] = $error->getMessage();
                }
                throw new OrderException('Invalid order : ' . implode('<br>', $messages));
            }
        }

        // Set completed at
        if ($order->getState() === OrderStates::STATE_COMPLETED && null === $order->getCompletedAt()) {
            $order->setCompletedAt(new \DateTime());
        }
    }
public function onStateChange ( OrderEvent $ event ) { $ order = $ event -> getOrder ( ) ; // If new state is Accepted or Completed and type is not order if ( in_array ( $ order -> getState ( ) , [ OrderStates :: STATE_ACCEPTED , OrderStates :: STATE_COMPLETED ] ) && $ order -> getType ( ) != OrderTypes :: TYPE_ORDER ) { // Transform to an order $ order -> setType ( OrderTypes :: TYPE_ORDER ) -> setCreatedAt ( new \ DateTime ( ) ) -> setNumber ( null ) -> setKey ( null ) ; $ this -> generateNumberAndKey ( $ order ) ; $ this -> handleAddresses ( $ order ) ; $ errorList = $ this -> validator -> validate ( $ order , [ 'Default' , 'Order' ] ) ; if ( $ errorList -> count ( ) > 0 ) { $ messages = [ ] ; foreach ( $ errorList as $ error ) { $ messages [ ] = $ error -> getMessage ( ) ; } throw new OrderException ( 'Invalid order : ' . implode ( '<br>' , $ messages ) ) ; } } // Set completed at if ( $ order -> getState ( ) === OrderStates :: STATE_COMPLETED && null === $ order -> getCompletedAt ( ) ) { $ order -> setCompletedAt ( new \ DateTime ( ) ) ; } }
State change event handler.

@param OrderEvent $event
@throws OrderException
State change event handler .