MetaHydrator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A hydrateObject() 0 6 1
A hydrateNewObject() 0 5 1
B parse() 0 27 6
1
<?php
2
namespace MetaHydrator;
3
4
use MetaHydrator\Exception\HydratingException;
5
use MetaHydrator\Exception\ValidationException;
6
use MetaHydrator\Handler\HydratingHandlerInterface;
7
use MetaHydrator\Validator\ValidatorInterface;
8
use Mouf\Hydrator\Hydrator;
9
use Mouf\Hydrator\TdbmHydrator;
10
11
/**
12
 * An implementation of Hydrator interface, designed to be configured at will per instance.
13
 *
14
 * Class MetaHydrator
15
 * @package MetaHhydrator
16
 */
17
class MetaHydrator implements Hydrator
18
{
19
    /** @var HydratingHandlerInterface[] */
20
    private $handlers = [];
21
22
    /** @var ValidatorInterface[] */
23
    private $validators = [];
24
25
    /** @var Hydrator */
26
    private $simpleHydrator;
27
28
    /**
29
     * MetaHydrator constructor.
30
     * @param HydratingHandlerInterface[] $handlers
31
     * @param ValidatorInterface[] $validators
32
     * @param Hydrator $simpleHydrator
33
     */
34
    public function __construct($handlers = [], $validators = [], $simpleHydrator = null)
35
    {
36
        $this->handlers = $handlers;
37
        $this->validators = $validators;
38
        $this->simpleHydrator = $simpleHydrator ?? new TdbmHydrator();
39
    }
40
41
    /**
42
     * @param array $data
43
     * @param $object
44
     * @return object
45
     *
46
     * @throws HydratingException
47
     */
48
    public function hydrateObject(array $data, $object)
49
    {
50
        $parsedData = $this->parse($data, $object);
51
        $this->simpleHydrator->hydrateObject($parsedData, $object);
52
        return $object;
53
    }
54
55
    /**
56
     * @param array $data
57
     * @param string $class
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
58
     * @return object
59
     *
60
     * @throws HydratingException
61
     */
62
    public function hydrateNewObject(array $data, string $className)
63
    {
64
        $parsedData = $this->parse($data);
65
        return $this->simpleHydrator->hydrateNewObject($parsedData, $className);
66
    }
67
68
    /**
69
     * @param $data
70
     * @param $contextObject
71
     * @return array
72
     *
73
     * @throws HydratingException
74
     */
75
    private function parse($data, $contextObject = null)
76
    {
77
        $parsedData = [];
78
        $errorsMap = [];
79
80
        foreach ($this->handlers as $handler) {
81
            try {
82
                $handler->handle($data, $parsedData, $contextObject);
83
            } catch (HydratingException $e) {
84
                $errorsMap = array_merge($e->getErrorsMap(), $errorsMap);
85
            }
86
        }
87
88
        foreach ($this->validators as $validator) {
89
            try {
90
                $validator->validate($parsedData, $contextObject);
91
            } catch (ValidationException $e) {
92
                $errorsMap = array_merge($e->getInnerError(), $errorsMap);
93
            }
94
        }
95
96
        if ($errorsMap) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errorsMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
            throw new HydratingException($errorsMap);
98
        }
99
100
        return $parsedData;
101
    }
102
}
103