Completed
Pull Request — 1.x (#1)
by Dorian
02:37
created

SubHydratingHandler::handle()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 9
nop 3
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 = null, 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
        } else {
74
            $subData = $data[$this->key];
75
            if (!is_array($subData)) {
76
                throw new HydratingException([$this->key => $this->errorMessage]);
77
            }
78
            try {
79
                $subObject = $this->getSubObject($object);
80
                if ($subObject !== null) {
81
                    $this->hydrator->hydrateObject($subData, $subObject);
82
                } else {
83
                    $subObject = $this->hydrator->hydrateNewObject($subData, $this->className);
84
                    $targetData[$this->key] = $subObject;
85
                }
86
            } catch (HydratingException $exception) {
87
                throw new HydratingException([$this->key => $exception->getErrorsMap()]);
88
            }
89
        }
90
91
        $this->validate($subObject, $object);
92
    }
93
94
    /**
95
     * @param mixed $parsedValue
96
     * @param mixed $contextObject
97
     *
98
     * @throws HydratingException
99
     */
100 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...
101
    {
102
        try {
103
            foreach ($this->validators as $validator) {
104
                $validator->validate($parsedValue, $contextObject);
105
            }
106
        } catch (ValidationException $exception) {
107
            throw new HydratingException([ $this->key => $exception->getInnerError() ]);
108
        }
109
    }
110
111
    /**
112
     * @param $object
113
     * @return mixed
114
     */
115
    protected function getSubObject($object)
116
    {
117
        if ($object === null) {
118
            return null;
119
        }
120
        return $this->getter->get($object, $this->key);
121
    }
122
}
123