Passed
Push — trunk ( d8cd01...b6c024 )
by Christian
13:44 queued 12s
created

stateMachineInvalidEntityId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\StateMachine;
4
5
use Shopware\Core\Framework\Feature;
6
use Shopware\Core\Framework\HttpException;
7
use Shopware\Core\Framework\Log\Package;
8
use Shopware\Core\System\StateMachine\Exception\IllegalTransitionException;
9
use Shopware\Core\System\StateMachine\Exception\StateMachineInvalidEntityIdException;
10
use Shopware\Core\System\StateMachine\Exception\StateMachineInvalidStateFieldException;
11
use Shopware\Core\System\StateMachine\Exception\StateMachineNotFoundException;
12
use Shopware\Core\System\StateMachine\Exception\StateMachineStateNotFoundException;
13
use Shopware\Core\System\StateMachine\Exception\StateMachineWithoutInitialStateException;
14
use Shopware\Core\System\StateMachine\Exception\UnnecessaryTransitionException;
15
use Symfony\Component\HttpFoundation\Response;
16
17
#[Package('checkout')]
18
class StateMachineException extends HttpException
19
{
20
    public const ILLEGAL_STATE_TRANSITION = 'SYSTEM__ILLEGAL_STATE_TRANSITION';
21
    public const STATE_MACHINE_INVALID_ENTITY_ID = 'SYSTEM__STATE_MACHINE_INVALID_ENTITY_ID';
22
    public const STATE_MACHINE_INVALID_STATE_FIELD = 'SYSTEM__STATE_MACHINE_INVALID_STATE_FIELD';
23
    public const STATE_MACHINE_NOT_FOUND = 'SYSTEM__STATE_MACHINE_NOT_FOUND';
24
    public const STATE_MACHINE_STATE_NOT_FOUND = 'SYSTEM__STATE_MACHINE_STATE_NOT_FOUND';
25
    public const STATE_MACHINE_WITHOUT_INITIAL_STATE = 'SYSTEM__STATE_MACHINE_WITHOUT_INITIAL_STATE';
26
    public const UNNECESSARY_TRANSITION = 'SYSTEM__UNNECESSARY_TRANSITION';
27
28
    /**
29
     * @param array<mixed> $possibleTransitions
30
     */
31
    public static function illegalStateTransition(string $currentState, string $transition, array $possibleTransitions): IllegalTransitionException
32
    {
33
        return new IllegalTransitionException($currentState, $transition, $possibleTransitions);
34
    }
35
36
    public static function stateMachineInvalidEntityId(string $entityName, string $entityId): self
37
    {
38
        if (!Feature::isActive('v6.6.0.0')) {
39
            return new StateMachineInvalidEntityIdException($entityName, $entityId);
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\System\Sta...nvalidEntityIdException has been deprecated: tag:v6.6.0 - will be removed, use StateMachineException::stateMachineInvalidEntityId instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
            return /** @scrutinizer ignore-deprecated */ new StateMachineInvalidEntityIdException($entityName, $entityId);
Loading history...
40
        }
41
42
        return new self(
43
            Response::HTTP_BAD_REQUEST,
44
            self::STATE_MACHINE_INVALID_ENTITY_ID,
45
            'Unable to read entity "{{ entityName }}" with id "{{ entityId }}".',
46
            [
47
                'entityName' => $entityName,
48
                'entityId' => $entityId,
49
            ]
50
        );
51
    }
52
53
    public static function stateMachineInvalidStateField(string $fieldName): self
54
    {
55
        if (!Feature::isActive('v6.6.0.0')) {
56
            return new StateMachineInvalidStateFieldException($fieldName);
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\System\Sta...alidStateFieldException has been deprecated: tag:v6.6.0 - will be removed, use StateMachineException::stateMachineInvalidStateField instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
            return /** @scrutinizer ignore-deprecated */ new StateMachineInvalidStateFieldException($fieldName);
Loading history...
57
        }
58
59
        return new self(
60
            Response::HTTP_BAD_REQUEST,
61
            self::STATE_MACHINE_INVALID_STATE_FIELD,
62
            'Field "{{ fieldName }}" does not exists or isn\'t of type StateMachineStateField.',
63
            [
64
                'fieldName' => $fieldName,
65
            ]
66
        );
67
    }
68
69
    public static function stateMachineNotFound(string $stateMachineName): self
70
    {
71
        if (!Feature::isActive('v6.6.0.0')) {
72
            return new StateMachineNotFoundException($stateMachineName);
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\System\Sta...achineNotFoundException has been deprecated: tag:v6.6.0 - will be removed, use StateMachineException::stateMachineNotFound instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

72
            return /** @scrutinizer ignore-deprecated */ new StateMachineNotFoundException($stateMachineName);
Loading history...
73
        }
74
75
        return new self(
76
            Response::HTTP_BAD_REQUEST,
77
            self::STATE_MACHINE_NOT_FOUND,
78
            'The StateMachine named "{{ name }}" was not found.',
79
            ['name' => $stateMachineName]
80
        );
81
    }
82
83
    public static function stateMachineStateNotFound(string $stateMachineName, string $technicalPlaceName): self
84
    {
85
        if (!Feature::isActive('v6.6.0.0')) {
86
            return new StateMachineStateNotFoundException($stateMachineName, $technicalPlaceName);
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\System\Sta...eStateNotFoundException has been deprecated: tag:v6.6.0 - will be removed, use StateMachineException::stateMachineStateNotFound instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

86
            return /** @scrutinizer ignore-deprecated */ new StateMachineStateNotFoundException($stateMachineName, $technicalPlaceName);
Loading history...
87
        }
88
89
        return new self(
90
            Response::HTTP_BAD_REQUEST,
91
            self::STATE_MACHINE_STATE_NOT_FOUND,
92
            'The place "{{ place }}" for state machine named "{{ stateMachine }}" was not found.',
93
            [
94
                'place' => $technicalPlaceName,
95
                'stateMachine' => $stateMachineName,
96
            ]
97
        );
98
    }
99
100
    public static function stateMachineWithoutInitialState(string $stateMachineName): self
101
    {
102
        if (!Feature::isActive('v6.6.0.0')) {
103
            return new StateMachineWithoutInitialStateException($stateMachineName);
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\System\Sta...utInitialStateException has been deprecated: tag:v6.6.0 - will be removed, use StateMachineException::stateMachineWithoutInitialState instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

103
            return /** @scrutinizer ignore-deprecated */ new StateMachineWithoutInitialStateException($stateMachineName);
Loading history...
104
        }
105
106
        return new self(
107
            Response::HTTP_BAD_REQUEST,
108
            self::STATE_MACHINE_WITHOUT_INITIAL_STATE,
109
            'The StateMachine named "{{ name }}" has no initial state.',
110
            ['name' => $stateMachineName]
111
        );
112
    }
113
114
    public static function unnecessaryTransition(string $transition): UnnecessaryTransitionException
115
    {
116
        return new UnnecessaryTransitionException($transition);
117
    }
118
}
119