Completed
Push — master ( aa6cd0...7372cb )
by Randy
02:05
created

Hydrator::assemble()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Dgame\Soap\Hydrator\Dom;
4
5
use Dgame\Soap\Dom\Translator;
6
use Dgame\Soap\Element;
7
use Dgame\Soap\Hydrator\ClassMapper;
8
use Dgame\Soap\Hydrator\HydrateProcedure;
9
use DOMDocument;
10
use DOMNode;
11
12
/**
13
 * Class Hydrator
14
 * @package Dgame\Soap\Hydrator\Dom
15
 */
16
final class Hydrator
17
{
18
    /**
19
     * @var ClassMapper
20
     */
21
    private $mapper;
22
23
    /**
24
     * Hydrator constructor.
25
     *
26
     * @param ClassMapper $mapper
27
     */
28
    public function __construct(ClassMapper $mapper)
29
    {
30
        $this->mapper = $mapper;
31
    }
32
33
    /**
34
     * @param DOMDocument $document
35
     *
36
     * @return array
37
     */
38
    public function hydrateDocument(DOMDocument $document): array
39
    {
40
        $translator = new Translator();
41
        $elements   = $translator->translateDocument($document);
42
43
        $output = [];
44
        foreach ($elements as $element) {
45
            $hydrate = $this->hydrate($element);
46
            if ($hydrate->isValid()) {
47
                $output[] = $hydrate->getHydratable();
48
            }
49
        }
50
51
        return $output;
52
    }
53
54
    /**
55
     * @param DOMNode $node
56
     *
57
     * @return HydrateProcedure
58
     */
59
    public function hydrateNode(DOMNode $node): HydrateProcedure
60
    {
61
        $translator = new Translator();
62
        $element    = $translator->translateNode($node);
63
64
        return $this->hydrate($element);
0 ignored issues
show
Bug introduced by
It seems like $element defined by $translator->translateNode($node) on line 62 can be null; however, Dgame\Soap\Hydrator\Dom\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...
65
    }
66
67
    /**
68
     * @param AssemblableInterface $assemblable
69
     * @param DOMNode|null         $node
70
     *
71
     * @return DOMNode
72
     */
73
    public function assemble(AssemblableInterface $assemblable, DOMNode $node = null): DOMNode
74
    {
75
        return $this->dehydrate($assemblable->assemble(), $node);
76
    }
77
78
    /**
79
     * @param Element      $element
80
     * @param DOMNode|null $node
81
     *
82
     * @return DOMNode
83
     */
84
    public function dehydrate(Element $element, DOMNode $node = null): DOMNode
85
    {
86
        $node      = $node ?? new DOMDocument('1.0', 'utf-8');
87
        $assembler = new Assembler($node);
88
89
        $element->accept($assembler);
90
91
        return $node;
92
    }
93
94
    /**
95
     * @param Element $element
96
     *
97
     * @return HydrateProcedure
98
     */
99
    private function hydrate(Element $element): HydrateProcedure
100
    {
101
        $hydrate = new HydrateProcedure($this->mapper);
102
        $element->accept($hydrate);
103
104
        return $hydrate;
105
    }
106
}