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

SubHydratingHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 91
rs 10
c 0
b 0
f 0

4 Methods

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