Completed
Push — master ( 56a459...0ee720 )
by Randy
03:34
created

Hydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hydrateDocument() 0 15 3
A hydrateNode() 0 7 1
A hydrate() 0 7 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Soap\Element;
6
use Dgame\Soap\XmlTranslator;
7
use DOMDocument;
8
use DOMNode;
9
10
/**
11
 * Class Hydrator
12
 * @package Dgame\Soap\Hydrator
13
 */
14
final class Hydrator
15
{
16
    /**
17
     * @var ClassMapper
18
     */
19
    private $mapper;
20
21
    /**
22
     * Hydrator constructor.
23
     *
24
     * @param ClassMapper $mapper
25
     */
26
    public function __construct(ClassMapper $mapper)
27
    {
28
        $this->mapper = $mapper;
29
    }
30
31
    /**
32
     * @param DOMDocument $document
33
     *
34
     * @return array
35
     */
36
    public function hydrateDocument(DOMDocument $document): array
37
    {
38
        $translator = new XmlTranslator();
39
        $elements   = $translator->translateDocument($document);
40
41
        $output = [];
42
        foreach ($elements as $element) {
43
            $hydrat = $this->hydrate($element);
44
            if ($hydrat->isValid()) {
45
                $output[] = $hydrat->getHydratable();
46
            }
47
        }
48
49
        return $output;
50
    }
51
52
    /**
53
     * @param DOMNode $node
54
     *
55
     * @return HydrateProcedure
56
     */
57
    public function hydrateNode(DOMNode $node): HydrateProcedure
58
    {
59
        $translator = new XmlTranslator();
60
        $element    = $translator->translateNode($node);
61
62
        return $this->hydrate($element);
0 ignored issues
show
Bug introduced by
It seems like $element defined by $translator->translateNode($node) on line 60 can be null; however, Dgame\Soap\Hydrator\Hydrator::hydrate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
63
    }
64
65
    /**
66
     * @param Element $element
67
     *
68
     * @return HydrateProcedure
69
     */
70
    private function hydrate(Element $element): HydrateProcedure
71
    {
72
        $hydrat = new HydrateProcedure($this->mapper);
73
        $element->hydration($hydrat);
74
75
        return $hydrat;
76
    }
77
}