1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Workflow library. |
5
|
|
|
* |
6
|
|
|
* @package workflow |
7
|
|
|
* @author David Molineus <[email protected]> |
8
|
|
|
* @copyright 2014-2017 netzmacht David Molineus |
9
|
|
|
* @license LGPL 3.0 https://github.com/netzmacht/workflow |
10
|
|
|
* @filesource |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Netzmacht\Workflow\Handler; |
16
|
|
|
|
17
|
|
|
use Netzmacht\Workflow\Data\EntityRepository; |
18
|
|
|
use Netzmacht\Workflow\Data\StateRepository; |
19
|
|
|
use Netzmacht\Workflow\Exception\WorkflowException; |
20
|
|
|
use Netzmacht\Workflow\Flow\Item; |
21
|
|
|
use Netzmacht\Workflow\Flow\State; |
22
|
|
|
use Netzmacht\Workflow\Flow\Workflow; |
23
|
|
|
use Netzmacht\Workflow\Transaction\TransactionHandler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class RepositoryBasedTransitionHandler handles the transition to another step in the workflow. |
27
|
|
|
* |
28
|
|
|
* It uses an collection repository approach to store entities. |
29
|
|
|
* |
30
|
|
|
* @package Netzmacht\Workflow |
31
|
|
|
*/ |
32
|
|
|
class RepositoryBasedTransitionHandler extends AbstractTransitionHandler |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The entity repository. |
36
|
|
|
* |
37
|
|
|
* @var EntityRepository |
38
|
|
|
*/ |
39
|
|
|
private $entityRepository; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The state repository. |
43
|
|
|
* |
44
|
|
|
* @var StateRepository |
45
|
|
|
*/ |
46
|
|
|
private $stateRepository; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Construct. |
50
|
|
|
* |
51
|
|
|
* @param Item $item The item. |
52
|
|
|
* @param Workflow $workflow The current workflow. |
53
|
|
|
* @param string|null $transitionName The transition to be handled. |
54
|
|
|
* @param EntityRepository $entityRepository EntityRepository which stores changes. |
55
|
|
|
* @param StateRepository $stateRepository StateRepository which stores new states. |
56
|
|
|
* @param TransactionHandler $transactionHandler TransactionHandler take care of transactions. |
57
|
|
|
* |
58
|
|
|
* @throws WorkflowException If invalid transition name is given. |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
Item $item, |
62
|
|
|
Workflow $workflow, |
63
|
|
|
string $transitionName = null, |
64
|
|
|
EntityRepository $entityRepository, |
65
|
|
|
StateRepository $stateRepository, |
66
|
|
|
TransactionHandler $transactionHandler |
67
|
|
|
) { |
68
|
|
|
parent::__construct($item, $workflow, $transitionName, $transactionHandler); |
69
|
|
|
|
70
|
|
|
$this->entityRepository = $entityRepository; |
71
|
|
|
$this->stateRepository = $stateRepository; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
* |
77
|
|
|
* @throws \Exception If something went wrong during action execution. |
78
|
|
|
*/ |
79
|
|
|
public function transit(): State |
80
|
|
|
{ |
81
|
|
|
$this->guardValidated(); |
82
|
|
|
|
83
|
|
|
$this->transactionHandler->begin(); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$state = $this->executeTransition(); |
87
|
|
|
|
88
|
|
|
$this->stateRepository->add($state); |
89
|
|
|
$this->entityRepository->add($this->getItem()->getEntity()); |
90
|
|
|
} catch (\Exception $e) { |
91
|
|
|
$this->transactionHandler->rollback(); |
92
|
|
|
|
93
|
|
|
throw $e; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->transactionHandler->commit(); |
97
|
|
|
|
98
|
|
|
return $state; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|