Passed
Push — bump-dependencies ( b4f034...4ebfc0 )
by Mattia
12:55 queued 09:49
created

FiniteHandler::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cypress\PatchManager\Handler;
6
7
use Cypress\PatchManager\OperationData;
8
use Cypress\PatchManager\Patchable;
9
use Cypress\PatchManager\PatchOperationHandler;
10
use Finite\Factory\FactoryInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class FiniteHandler implements PatchOperationHandler
14
{
15
    private FactoryInterface $factoryInterface;
0 ignored issues
show
Coding Style introduced by
Private member variable "factoryInterface" must contain a leading underscore
Loading history...
16
17
    /**
18
     * @param FactoryInterface $factoryInterface
19
     */
20 5
    public function __construct(FactoryInterface $factoryInterface)
21
    {
22 5
        $this->factoryInterface = $factoryInterface;
23
    }
24
25 3
    public function handle(Patchable $subject, OperationData $operationData): void
26
    {
27 3
        $sm = $this->factoryInterface->get($subject);
28 3
        $transition = $operationData->get('transition')->get();
29 3
        if ($operationData->get('check')->get() && !$sm->can($transition)) {
30 1
            return;
31
        }
32 2
        $sm->apply($transition);
33
    }
34
35
    /**
36
     * the operation name
37
     */
38 1
    public function getName(): string
39
    {
40 1
        return 'sm';
41
    }
42
43
    /**
44
     * use the OptionResolver instance to configure the required and optional fields that needs to be passed
45
     * with the request body. See http://symfony.com/doc/current/components/options_resolver.html to check all
46
     * possible options
47
     *
48
     * @param OptionsResolver $optionsResolver
49
     */
50 1
    public function configureOptions(OptionsResolver $optionsResolver): void
51
    {
52 1
        $optionsResolver
53 1
            ->setRequired(['transition'])
54 1
            ->setDefined(['check'])
55 1
            ->setDefaults(['check' => false]);
56
    }
57
58
    /**
59
     * whether the handler is able to handle the given subject
60
     *
61
     * @param Patchable $subject
62
     * @return bool
63
     */
64
    public function canHandle(Patchable $subject): bool
65
    {
66
        return true;
67
    }
68
}
69