Passed
Push — master ( 4e94c9...4bfcda )
by Michael
02:19
created

AttributeHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 35.05 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 34
loc 97
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toResource() 0 9 2
A processAttributeToResource() 15 15 3
A fromResource() 0 13 3
A processResourceToAttribute() 19 19 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Handler;
5
6
use Mikemirten\Component\JsonApi\Document\ResourceObject;
7
use Mikemirten\Component\JsonApi\Mapper\Definition\Attribute;
8
use Mikemirten\Component\JsonApi\Mapper\MappingContext;
9
10
/**
11
 * Attribute handler
12
 *
13
 * @package Mikemirten\Component\JsonApi\Mapper\Handler
14
 */
15
class AttributeHandler implements HandlerInterface
16
{
17
    /**
18
     * @var DataTypeManager
19
     */
20
    protected $typeManager;
21
22
    /**
23
     * AttributeHandler constructor.
24
     *
25
     * @param DataTypeManager $typeManager
26
     */
27 6
    public function __construct(DataTypeManager $typeManager)
28
    {
29 6
        $this->typeManager = $typeManager;
30 6
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 3
    public function toResource($object, ResourceObject $resource, MappingContext $context)
36
    {
37 3
        $definitions = $context->getDefinition()->getAttributes();
38
39 3
        foreach ($definitions as $definition)
40
        {
41 3
            $this->processAttributeToResource($object, $resource, $definition);
42
        }
43 3
    }
44
45
    /**
46
     * Process attribute to resource mapping
47
     *
48
     * @param mixed          $object
49
     * @param ResourceObject $resource
50
     * @param Attribute      $definition
51
     */
52 3 View Code Duplication
    protected function processAttributeToResource($object, ResourceObject $resource, Attribute $definition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 3
        $name   = $definition->getName();
55 3
        $getter = $definition->getGetter();
56
57 3
        $value = $object->$getter();
58
59 3
        if ($value === null && ! $definition->getProcessNull()) {
60 1
            return;
61
        }
62
63 2
        $value = $this->typeManager->toResource($definition, $value);
64
65 2
        $resource->setAttribute($name, $value);
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 3
    public function fromResource($object, ResourceObject $resource, MappingContext $context)
72
    {
73 3
        $definitions = $context->getDefinition()->getAttributes();
74
75 3
        foreach ($definitions as $definition)
76
        {
77 3
            if (! $definition->hasSetter()) {
78
                continue;
79
            }
80
81 3
            $this->processResourceToAttribute($object, $resource, $definition);
82
        }
83 3
    }
84
85
    /**
86
     * Process resource to attribute mapping
87
     *
88
     * @param mixed          $object
89
     * @param ResourceObject $resource
90
     * @param Attribute      $definition
91
     */
92 3 View Code Duplication
    protected function processResourceToAttribute($object, ResourceObject $resource, Attribute $definition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94 3
        $name = $definition->getName();
95
96 3
        if (! $resource->hasAttribute($name)) {
97
            return;
98
        }
99
100 3
        $value = $resource->getAttribute($name);
101
102 3
        if ($value === null && ! $definition->getProcessNull()) {
103 1
            return;
104
        }
105
106 2
        $value  = $this->typeManager->fromResource($definition, $value);
107 2
        $setter = $definition->getSetter();
108
109 2
        $object->$setter($value);
110
    }
111
}