Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
71.43% covered (warning)
71.43%
5 / 7
CRAP
88.24% covered (warning)
88.24%
15 / 17
TransitionBlocker
0.00% covered (danger)
0.00%
0 / 1
71.43% covered (warning)
71.43%
5 / 7
9.13
88.24% covered (warning)
88.24%
15 / 17
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 createBlockedByMarking
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 createBlockedByExpressionGuardListener
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 createUnknown
0.00% covered (danger)
0.00%
0 / 1
3.04
83.33% covered (warning)
83.33%
5 / 6
 getMessage
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCode
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getParameters
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Workflow;
13
14/**
15 * A reason why a transition cannot be performed for a subject.
16 */
17final class TransitionBlocker
18{
19    public const BLOCKED_BY_MARKING = '19beefc8-6b1e-4716-9d07-a39bd6d16e34';
20    public const BLOCKED_BY_EXPRESSION_GUARD_LISTENER = '326a1e9c-0c12-11e8-ba89-0ed5f89f718b';
21    public const UNKNOWN = 'e8b5bbb9-5913-4b98-bfa6-65dbd228a82a';
22
23    private $message;
24    private $code;
25    private $parameters;
26
27    /**
28     * @param string $code       Code is a machine-readable string, usually an UUID
29     * @param array  $parameters This is useful if you would like to pass around the condition values, that
30     *                           blocked the transition. E.g. for a condition "distance must be larger than
31     *                           5 miles", you might want to pass around the value of 5.
32     */
33    public function __construct(string $message, string $code, array $parameters = [])
34    {
35        $this->message = $message;
36        $this->code = $code;
37        $this->parameters = $parameters;
38    }
39
40    /**
41     * Create a blocker that says the transition cannot be made because it is
42     * not enabled.
43     *
44     * It means the subject is in wrong place (i.e. status):
45     * * If the workflow is a state machine: the subject is not in the previous place of the transition.
46     * * If the workflow is a workflow: the subject is not in all previous places of the transition.
47     */
48    public static function createBlockedByMarking(Marking $marking): self
49    {
50        return new static('The marking does not enable the transition.', self::BLOCKED_BY_MARKING, [
51            'marking' => $marking,
52        ]);
53    }
54
55    /**
56     * Creates a blocker that says the transition cannot be made because it has
57     * been blocked by the expression guard listener.
58     */
59    public static function createBlockedByExpressionGuardListener(string $expression): self
60    {
61        return new static('The expression blocks the transition.', self::BLOCKED_BY_EXPRESSION_GUARD_LISTENER, [
62            'expression' => $expression,
63        ]);
64    }
65
66    /**
67     * Creates a blocker that says the transition cannot be made because of an
68     * unknown reason.
69     */
70    public static function createUnknown(string $message = null, int $backtraceFrame = 2): self
71    {
72        if (null !== $message) {
73            return new static($message, self::UNKNOWN);
74        }
75
76        $caller = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $backtraceFrame + 1)[$backtraceFrame]['class'] ?? null;
77
78        if (null !== $caller) {
79            return new static("The transition has been blocked by a guard ($caller).", self::UNKNOWN);
80        }
81
82        return new static('The transition has been blocked by a guard.', self::UNKNOWN);
83    }
84
85    public function getMessage(): string
86    {
87        return $this->message;
88    }
89
90    public function getCode(): string
91    {
92        return $this->code;
93    }
94
95    public function getParameters(): array
96    {
97        return $this->parameters;
98    }
99}