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

SubHydratingHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

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