Completed
Push — master ( e960f4...d2d510 )
by Randy
02:01
created

ElementHydration   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHydrate() 0 4 1
A isHydrated() 0 4 2
A visitElement() 0 6 2
A visitXmlElement() 0 6 2
A visitXmlNode() 0 10 3
A hydrateChild() 0 11 2
A assign() 0 11 3
A hydrate() 0 14 2
A append() 0 8 2
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Soap\Element;
6
use Dgame\Soap\Visitor\ElementVisitorInterface;
7
use Dgame\Soap\XmlElement;
8
use Dgame\Soap\XmlNode;
9
use Monolog\Registry;
10
11
/**
12
 * Class Hydrate
13
 * @package Dgame\Soap\Hydrator
14
 */
15
final class ElementHydration implements ElementVisitorInterface
16
{
17
    /**
18
     * @var ClassMapper
19
     */
20
    private $mapper;
21
    /**
22
     * @var Hydrate
23
     */
24
    private $hydrate;
25
26
    /**
27
     * ElementHydration constructor.
28
     *
29
     * @param ClassMapper $mapper
30
     */
31
    public function __construct(ClassMapper $mapper)
32
    {
33
        $this->mapper = $mapper;
34
    }
35
36
    /**
37
     * @return Hydrate
38
     */
39
    public function getHydrate(): Hydrate
40
    {
41
        return $this->hydrate;
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function isHydrated(): bool
48
    {
49
        return $this->hydrate !== null && $this->hydrate->hasFacade();
50
    }
51
52
    /**
53
     * @param Element $element
54
     */
55
    public function visitElement(Element $element)
56
    {
57
        if (!$this->hydrate($element)) {
58
            $this->assign($element);
59
        }
60
    }
61
62
    /**
63
     * @param XmlElement $element
64
     */
65
    public function visitXmlElement(XmlElement $element)
66
    {
67
        if (!$this->hydrate($element)) {
68
            $this->assign($element);
69
        }
70
    }
71
72
    /**
73
     * @param XmlNode $node
74
     */
75
    public function visitXmlNode(XmlNode $node)
76
    {
77
        if (!$this->hydrate($node)) {
78
            $this->assign($node);
79
        }
80
81
        foreach ($node->getElements() as $element) {
82
            $this->hydrateChild($element);
83
        }
84
    }
85
86
    /**
87
     * @param Element $element
88
     */
89
    private function hydrateChild(Element $element)
90
    {
91
        $hydration = new self($this->mapper);
92
        $element->accept($hydration);
93
94
        if ($hydration->isHydrated()) {
95
            $this->append($hydration->getHydrate());
96
        } else {
97
            $this->assign($element);
98
        }
99
    }
100
101
    /**
102
     * @param Element $element
103
     */
104
    private function assign(Element $element)
105
    {
106
        if ($this->isHydrated() && $element->hasValue()) {
107
            $this->hydrate->assign($element->getName(), $element->getValue());
108
        } else {
109
            Registry::getInstance(Hydrator::class)->warning(
110
                'Could neither hydrate or assign {name}',
111
                ['name' => $element->getName()]
112
            );
113
        }
114
    }
115
116
    /**
117
     * @param Element $element
118
     *
119
     * @return bool
120
     */
121
    private function hydrate(Element $element): bool
122
    {
123
        $hydrate = new Hydrate($this->mapper, $element);
124
        if ($hydrate->hasFacade()) {
125
            $hydration = new AttributeHydration($hydrate);
126
            $hydration->hydrate($element);
127
128
            $this->append($hydrate);
129
130
            return true;
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * @param Hydrate $hydrate
138
     */
139
    private function append(Hydrate $hydrate)
140
    {
141
        if ($this->isHydrated()) {
142
            $this->hydrate->append($hydrate);
0 ignored issues
show
Documentation introduced by
$hydrate is of type object<Dgame\Soap\Hydrator\Hydrate>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
        } else {
144
            $this->hydrate = $hydrate;
145
        }
146
    }
147
}