|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) 2019, Wesley O. Nichols |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Wesnick\WorkflowBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
16
|
|
|
use Symfony\Component\Workflow\Exception\InvalidArgumentException; |
|
17
|
|
|
use Symfony\Component\Workflow\Registry; |
|
18
|
|
|
use Wesnick\WorkflowBundle\Model\WorkflowDTO; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class DefaultTransitionController. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Wesley O. Nichols <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class DefaultTransitionController |
|
26
|
|
|
{ |
|
27
|
|
|
private $registry; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(Registry $registry) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->registry = $registry; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param WorkflowDTO $data |
|
36
|
|
|
* @param mixed $subject |
|
37
|
|
|
* @param string $workflowName |
|
38
|
|
|
* @param string $transitionName |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __invoke($data, $subject, string $workflowName, string $transitionName) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!is_object($subject)) { |
|
45
|
|
|
throw new BadRequestHttpException( |
|
46
|
|
|
sprintf('Expected object for workflow "%s", got %s.', $workflowName, gettype($subject)) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$workflow = $this->registry->get($subject, $workflowName); |
|
52
|
|
|
|
|
53
|
|
|
if ($workflow->can($subject, $transitionName)) { |
|
54
|
|
|
// Symfony 4.3 added context to workflow transitions |
|
55
|
|
|
if (version_compare(Kernel::VERSION, '4.3.0', '<')) { |
|
56
|
|
|
$workflow->apply($subject, $transitionName); |
|
57
|
|
|
} else { |
|
58
|
|
|
$workflow->apply($subject, $transitionName, ['wesnick_workflow_dto' => $data]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $subject; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} catch (InvalidArgumentException $e) { |
|
65
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
throw new BadRequestHttpException( |
|
69
|
|
|
sprintf('Transition "%s" in Workflow "%s" is not available.', $transitionName, $workflowName) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|