Passed
Push — bump-dependencies ( 3eb1c7...418b80 )
by Mattia
15:26 queued 12:02
created

FiniteHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 5
    public function __construct(FactoryInterface $factoryInterface)
18
    {
19 5
        $this->factoryInterface = $factoryInterface;
20
    }
21
22 3
    public function handle(Patchable $subject, OperationData $operationData): void
23
    {
24 3
        $sm = $this->factoryInterface->get($subject);
25 3
        $transition = $operationData->get('transition')->get();
26 3
        if ($operationData->get('check')->get() && !$sm->can($transition)) {
27 1
            return;
28
        }
29 2
        $sm->apply($transition);
30
    }
31
32
    /**
33
     * the operation name
34
     */
35 1
    public function getName(): string
36
    {
37 1
        return 'sm';
38
    }
39
40
    /**
41
     * use the OptionResolver instance to configure the required and optional fields that needs to be passed
42
     * with the request body. See http://symfony.com/doc/current/components/options_resolver.html to check all
43
     * possible options
44
     */
45 1
    public function configureOptions(OptionsResolver $optionsResolver): void
46
    {
47 1
        $optionsResolver
48 1
            ->setRequired(['transition'])
49 1
            ->setDefined(['check'])
50 1
            ->setDefaults(['check' => false]);
51
    }
52
53
    /**
54
     * whether the handler is able to handle the given subject
55
     */
56
    public function canHandle(Patchable $subject): bool
57
    {
58
        return true;
59
    }
60
}
61