Passed
Pull Request — master (#10)
by Mattia
03:27
created

DataHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 60
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A configureOptions() 0 3 1
A canHandle() 0 3 1
A handle() 0 10 2
A useMagicCall() 0 3 1
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 Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
11
class DataHandler implements PatchOperationHandler
12
{
13
    protected bool $magicCall = false;
0 ignored issues
show
Coding Style introduced by
Protected member variable "magicCall" must contain a leading underscore
Loading history...
14
15
    /**
16
     * @param bool $magicCall
17
     */
18 2
    public function useMagicCall(bool $magicCall): void
19
    {
20 2
        $this->magicCall = $magicCall;
21 2
    }
22
23
    /**
24
     * @param Patchable $subject
25
     * @param OperationData $operationData
26
     */
27 2
    public function handle(Patchable $subject, OperationData $operationData): void
28
    {
29 2
        $propertyAccessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
30 2
        $propertyAccessorBuilder = $this->magicCall ? $propertyAccessorBuilder->enableMagicCall() : $propertyAccessorBuilder;
31
32 2
        $propertyAccessor = $propertyAccessorBuilder->getPropertyAccessor();
33 2
        $propertyAccessor->setValue(
34 2
            $subject,
35 2
            $operationData->get('property')->get(),
36 2
            $operationData->get('value')->get()
37
        );
38 2
    }
39
40
    /**
41
     * the operation name
42
     *
43
     * @return string
44
     */
45 3
    public function getName(): string
46
    {
47 3
        return 'data';
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->setRequired(['property', 'value']);
60 1
    }
61
62
    /**
63
     * whether the handler is able to handle the given subject
64
     *
65
     * @param Patchable $subject
66
     * @return bool
67
     */
68
    public function canHandle(Patchable $subject): bool
69
    {
70
        return true;
71
    }
72
}
73