Completed
Push — 1.x ( 201d44...7242c0 )
by Dorian
10s
created

SubHydratingHandler   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 121
Duplicated Lines 8.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 1 1
A setKey() 0 1 1
A getClassName() 0 1 1
A setClassName() 0 1 1
A getHydrator() 0 1 1
A setHydrator() 0 1 1
A getValidators() 0 1 1
A setValidators() 0 1 1
A addValidator() 0 1 1
A getErrorMessage() 0 1 1
A setErrorMessage() 0 1 1
A getGetter() 0 1 1
A setGetter() 0 1 1
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
    public function getKey() { return $this->key; }
23
    public function setKey(string $key) { $this->key = $key; }
24
25
    /** @var string */
26
    protected $className;
27
    public function getClassName() { return $this->className; }
28
    public function setClassName(string $className) { $this->className = $className; }
29
30
    /** @var Hydrator */
31
    protected $hydrator;
32
    public function getHydrator() { return $this->hydrator; }
33
    public function setHydrator(Hydrator $hydrator) { $this->hydrator = $hydrator; }
34
35
    /** @var ValidatorInterface[] */
36
    protected $validators;
37
    public function getValidators() { return $this->validators; }
38
    public function setValidators(array $validators) { $this->validators = $validators; }
39
    public function addValidator(ValidatorInterface $validator) { $this->validators[] = $validator; }
40
41
    /** @var string */
42
    protected $errorMessage;
43
    public function getErrorMessage() { return $this->errorMessage; }
44
    public function setErrorMessage(string $errorMessage) { $this->errorMessage = $errorMessage; }
45
46
    /** @var GetterInterface */
47
    protected $getter;
48
    public function getGetter() { return $this->getter; }
49
    public function setGetter(GetterInterface $getter) { $this->getter = $getter; }
50
51
    /**
52
     * SubHydratingHandler constructor.
53
     * @param string $key
54
     * @param string $className
55
     * @param Hydrator $hydrator
56
     * @param ValidatorInterface[] $validators
57
     * @param string $errorMessage
58
     * @param GetterInterface $getter
59
     */
60
    public function __construct(string $key, string $className, Hydrator $hydrator, array $validators = [], string $errorMessage = "", GetterInterface $getter = null)
61
    {
62
        $this->key = $key;
63
        $this->className = $className;
64
        $this->hydrator = $hydrator;
65
        $this->validators = $validators;
66
        $this->errorMessage = $errorMessage;
67
        $this->getter = $getter ?? new Getter(false);
68
    }
69
70
    /**
71
     * @param array $data
72
     * @param array $targetData
73
     * @param $object
74
     *
75
     * @throws HydratingException
76
     */
77
    public function handle(array $data, array &$targetData, $object = null)
78
    {
79
        if (!array_key_exists($this->key, $data)) {
80
            if ($object !== null) {
81
                return;
82
            } else {
83
                $subObject = null;
84
                $targetData[$this->key] = $subObject;
85
            }
86
        } elseif ($data[$this->key] === null) {
87
            $subObject = null;
88
            $targetData[$this->key] = null;
89
        } elseif (!is_array($data[$this->key])) {
90
            throw new HydratingException([$this->key => $this->errorMessage]);
91
        } else {
92
            $subData = $data[$this->key];
93
94
            try {
95
                $subObject = $this->getSubObject($object);
96
                if ($subObject !== null) {
97
                    $this->hydrator->hydrateObject($subData, $subObject);
98
                } else {
99
                    $subObject = $this->hydrator->hydrateNewObject($subData, $this->className);
100
                    $targetData[$this->key] = $subObject;
101
                }
102
            } catch (HydratingException $exception) {
103
                throw new HydratingException([$this->key => $exception->getErrorsMap()]);
104
            }
105
        }
106
107
        $this->validate($subObject, $object);
108
    }
109
110
    /**
111
     * @param mixed $parsedValue
112
     * @param mixed $contextObject
113
     *
114
     * @throws HydratingException
115
     */
116 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...
117
    {
118
        try {
119
            foreach ($this->validators as $validator) {
120
                $validator->validate($parsedValue, $contextObject);
121
            }
122
        } catch (ValidationException $exception) {
123
            throw new HydratingException([ $this->key => $exception->getInnerError() ]);
124
        }
125
    }
126
127
    /**
128
     * @param $object
129
     * @return mixed
130
     */
131
    protected function getSubObject($object)
132
    {
133
        if ($object === null) {
134
            return null;
135
        }
136
        return $this->getter->get($object, $this->key);
137
    }
138
}
139