|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cypress\PatchManager\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Cypress\PatchManager\OperationData; |
|
6
|
|
|
use Cypress\PatchManager\Patchable; |
|
7
|
|
|
use Cypress\PatchManager\PatchOperationHandler; |
|
8
|
|
|
use Finite\Factory\FactoryInterface; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
10
|
|
|
|
|
11
|
|
|
class FiniteHandler implements PatchOperationHandler |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var FactoryInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private FactoryInterface $factoryInterface; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param FactoryInterface $factoryInterface |
|
20
|
|
|
*/ |
|
21
|
5 |
|
public function __construct(FactoryInterface $factoryInterface) |
|
22
|
|
|
{ |
|
23
|
5 |
|
$this->factoryInterface = $factoryInterface; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Patchable $subject |
|
28
|
|
|
* @param OperationData $operationData |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function handle(Patchable $subject, OperationData $operationData): void |
|
31
|
|
|
{ |
|
32
|
3 |
|
$sm = $this->factoryInterface->get($subject); |
|
33
|
3 |
|
$transition = $operationData->get('transition')->get(); |
|
34
|
3 |
|
if ($operationData->get('check')->get() && !$sm->can($transition)) { |
|
35
|
1 |
|
return; |
|
36
|
|
|
} |
|
37
|
2 |
|
$sm->apply($transition); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* the operation name |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getName(): string |
|
46
|
|
|
{ |
|
47
|
1 |
|
return 'sm'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* use the OptionResolver instance to configure the required and optional fields that needs to be passed |
|
52
|
|
|
* with the request body. See http://symfony.com/doc/current/components/options_resolver.html to check all |
|
53
|
|
|
* possible options |
|
54
|
|
|
* |
|
55
|
|
|
* @param OptionsResolver $optionsResolver |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function configureOptions(OptionsResolver $optionsResolver): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
$optionsResolver |
|
60
|
1 |
|
->setRequired(['transition']) |
|
61
|
1 |
|
->setDefined(['check']) |
|
62
|
1 |
|
->setDefaults(['check' => false]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* whether the handler is able to handle the given subject |
|
67
|
|
|
* |
|
68
|
|
|
* @param Patchable $subject |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function canHandle(Patchable $subject): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|