Completed
Pull Request — 1.x (#1)
by Dorian
12:28
created

SubHydratingHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 9.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 10
loc 108
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C handle() 0 32 7
A validate() 10 10 3
A getSubObject() 0 7 2

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
namespace MetaHydrator\Handler;
3
4
use MetaHydrator\Exception\HydratingException;
5
use MetaHydrator\Exception\ValidationException;
6
use MetaHydrator\MetaHydrator;
7
use MetaHydrator\Reflection\Getter;
8
use MetaHydrator\Reflection\GetterInterface;
9
use MetaHydrator\Validator\ValidatorInterface;
10
use Mouf\Hydrator\Hydrator;
11
12
/**
13
 * An implementation of HydratingHandlerInterface aiming to manage partial edition of sub-objects
14
 *
15
 * Class SubHydratingHandler
16
 * @package MetaHydrator\Handler
17
 */
18
class SubHydratingHandler implements HydratingHandlerInterface
19
{
20
    /** @var string */
21
    protected $key;
22
23
    /** @var string */
24
    protected $className;
25
26
    /** @var Hydrator */
27
    protected $hydrator;
28
29
    /** @var ValidatorInterface[] */
30
    protected $validators;
31
32
    /** @var mixed */
33
    protected $errorMessage;
34
35
    /** @var GetterInterface */
36
    protected $getter;
37
38
    /**
39
     * SubHydratingHandler constructor.
40
     * @param string $key
41
     * @param string $className
42
     * @param Hydrator $hydrator
43
     * @param ValidatorInterface[] $validators
44
     * @param string $errorMessage
45
     * @param GetterInterface $getter
46
     */
47
    public function __construct(string $key, string $className, Hydrator $hydrator, array $validators = [], string $errorMessage = "", GetterInterface $getter = null)
48
    {
49
        $this->key = $key;
50
        $this->className = $className;
51
        $this->hydrator = $hydrator;
52
        $this->validators = $validators;
53
        $this->errorMessage = $errorMessage;
54
        $this->getter = $getter ?? new Getter(false);
55
    }
56
57
    /**
58
     * @param array $data
59
     * @param array $targetData
60
     * @param $object
61
     *
62
     * @throws HydratingException
63
     */
64
    public function handle(array $data, array &$targetData, $object = null)
65
    {
66
        if (!array_key_exists($this->key, $data)) {
67
            if ($object !== null) {
68
                return;
69
            } else {
70
                $subObject = null;
71
                $targetData[$this->key] = $subObject;
72
            }
73
        } elseif ($data[$this->key] === null) {
74
            $subObject = null;
75
            $targetData[$this->key] = null;
76
        } elseif (!is_array($data[$this->key])) {
77
            throw new HydratingException([$this->key => $this->errorMessage]);
78
        } else {
79
            $subData = $data[$this->key];
80
81
            try {
82
                $subObject = $this->getSubObject($object);
83
                if ($subObject !== null) {
84
                    $this->hydrator->hydrateObject($subData, $subObject);
85
                } else {
86
                    $subObject = $this->hydrator->hydrateNewObject($subData, $this->className);
87
                    $targetData[$this->key] = $subObject;
88
                }
89
            } catch (HydratingException $exception) {
90
                throw new HydratingException([$this->key => $exception->getErrorsMap()]);
91
            }
92
        }
93
94
        $this->validate($subObject, $object);
95
    }
96
97
    /**
98
     * @param mixed $parsedValue
99
     * @param mixed $contextObject
100
     *
101
     * @throws HydratingException
102
     */
103 View Code Duplication
    private function validate($parsedValue, $contextObject = null)
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...
104
    {
105
        try {
106
            foreach ($this->validators as $validator) {
107
                $validator->validate($parsedValue, $contextObject);
108
            }
109
        } catch (ValidationException $exception) {
110
            throw new HydratingException([ $this->key => $exception->getInnerError() ]);
111
        }
112
    }
113
114
    /**
115
     * @param $object
116
     * @return mixed
117
     */
118
    protected function getSubObject($object)
119
    {
120
        if ($object === null) {
121
            return null;
122
        }
123
        return $this->getter->get($object, $this->key);
124
    }
125
}
126