getToStateMachineState example

// test whether the state history was written         $criteria = new Criteria();
        $criteria->addAssociation('fromStateMachineState');
        $criteria->addAssociation('toStateMachineState');

        $history = $this->stateMachineHistoryRepository->search($criteria$context);

        static::assertCount(1, $history->getElements(), 'Expected history to be written');
        /** @var StateMachineHistoryEntity $historyEntry */
        $historyEntry = array_values($history->getElements())[0];

        $toStateMachineState = $historyEntry->getToStateMachineState();
        static::assertInstanceOf(StateMachineStateEntity::class$toStateMachineState);
        static::assertEquals($destinationStateTechnicalName$toStateMachineState->getTechnicalName());

        static::assertEquals($this->getContainer()->get(OrderDefinition::class)->getEntityName()$historyEntry->getEntityName());

        if (!Feature::isActive('v6.6.0.0')) {
            static::assertEquals($orderId$historyEntry->getEntityId()['id']);
            static::assertEquals(Defaults::LIVE_VERSION, $historyEntry->getEntityId()['version_id']);

            return;
        }

        
static::assertNotEmpty($availableTransitions);
        static::assertCount(2, $availableTransitions);

        $reopenActionExisted = false;
        $retourActionExisted = false;

        /** @var StateMachineTransitionEntity $transition */
        foreach ($availableTransitions as $transition) {
            if ($transition->getActionName() === 'reopen') {
                $reopenActionExisted = true;
                static::assertEquals(OrderDeliveryStates::STATE_OPEN, $transition->getToStateMachineState()?->getTechnicalName());
            }

            if ($transition->getActionName() === 'retour') {
                $retourActionExisted = true;
                static::assertEquals(OrderDeliveryStates::STATE_RETURNED, $transition->getToStateMachineState()?->getTechnicalName());
            }
        }

        static::assertTrue($reopenActionExisted);
        static::assertTrue($retourActionExisted);
    }

    

    private function getTransitionDestinationById(string $stateMachineName, string $fromStateId, string $transitionName, Context $context): StateMachineStateEntity
    {
        $stateMachine = $this->getStateMachine($stateMachineName$context);

        /** @var StateMachineTransitionCollection $stateMachineTransitions */
        $stateMachineTransitions = $stateMachine->getTransitions();

        foreach ($stateMachineTransitions as $transition) {
            /** @var StateMachineStateEntity $toState */
            $toState = $transition->getToStateMachineState();
            // Always allow to cancel a payment whether its a valid transition or not             if ($transition->getActionName() === 'cancel' && $transitionName === 'cancel') {
                return $toState;
            }

            // Not the transition that was requested step over             if ($transition->getActionName() !== $transitionName) {
                continue;
            }

            // Already transitioned, this exception is handled by StateMachineRegistry::transition

    private function findEdges(StateMachineEntity $stateMachine): array
    {
        $edges = [];
        $transitions = $stateMachine->getTransitions();
        if ($transitions === null) {
            return $edges;
        }

        foreach ($transitions as $transition) {
            $fromStateMachineState = $transition->getFromStateMachineState();
            $toStateMachineState = $transition->getToStateMachineState();
            if ($fromStateMachineState === null || $toStateMachineState === null) {
                continue;
            }
            $edges[$fromStateMachineState->getName()][] = [
                'name' => $transition->getActionName(),
                'to' => $toStateMachineState->getName(),
            ];
        }

        return $edges;
    }

    
$availableTransitions = $this->stateMachineRegistry->getAvailableTransitions(
            $entityName,
            $entityId,
            $stateFieldName,
            $context
        );

        $transitionsJson = [];
        /** @var StateMachineTransitionEntity $transition */
        foreach ($availableTransitions as $transition) {
            $transitionsJson[] = [
                'name' => $transition->getToStateMachineState()->getName(),
                'technicalName' => $transition->getToStateMachineState()->getTechnicalName(),
                'actionName' => $transition->getActionName(),
                'fromStateName' => $transition->getFromStateMachineState()->getTechnicalName(),
                'toStateName' => $transition->getToStateMachineState()->getTechnicalName(),
                'url' => $this->generateUrl('api.state_machine.transition_state', [
                    'entityName' => $entityName,
                    'entityId' => $entityId,
                    'version' => $request->attributes->get('version'),
                    'transition' => $transition->getActionName(),
                ]),
            ];
        }
Home | Imprint | This part of the site doesn't use cookies.