HeadersIncoming::isMustUnderstandHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GoetasWebservices\SoapServices\Metadata\Headers;
6
7
class HeadersIncoming
8
{
9
    /**
10
     * @var array
11
     */
12
    private $headers = [];
13
14
    /**
15
     * @var bool[]
16
     */
17
    private $understood = [];
18
19
    /**
20
     * @var object[]
21
     */
22
    private $mapping = [];
23
24
    public function addHeader(\SimpleXMLElement $header, ?object $mappedObject = null): void
25
    {
26
        $id = $this->findId($header);
27
        $this->headers[$id] = [$header, !empty($this->headers[$id][1])];
28
        $this->mapping[$id] = $this->mapping[$id] ?? ($mappedObject ? spl_object_id($mappedObject) : null);
29
    }
30
31
    public function addMustUnderstandHeader(\SimpleXMLElement $header, ?object $mappedObject = null): void
32
    {
33
        $id = $this->findId($header);
34
        $this->headers[$id] = [$header, true];
35
        $this->mapping[$id] = $this->mapping[$id] ?? ($mappedObject ? spl_object_id($mappedObject) : null);
36
    }
37
38
    /**
39
     * @return \SimpleXMLElement[]
40
     */
41
    public function headersNotUnderstood(): array
42
    {
43
        $headers = [];
44
        foreach ($this->headers as $id => $header) {
45
            if (!empty($header[1]) && empty($this->understood[$id])) {
46
                $headers[] = $header[0];
47
            }
48
        }
49
50
        return $headers;
51
    }
52
53
    /**
54
     * @param object|\SimpleXMLElement $header
55
     */
56
    public function isMustUnderstandHeader(object $header): bool
57
    {
58
        $id = $this->findId($header);
59
60
        return isset($this->headers[$id]) && !empty($this->headers[$id][1]);
61
    }
62
63
    /**
64
     * @param object|\SimpleXMLElement $header
65
     */
66
    public function understoodHeader(object $header): void
67
    {
68
        $id = $this->findId($header);
69
70
        $this->understood[$id] = true;
71
    }
72
73
    /**
74
     * @param object|\SimpleXMLElement $header
75
     */
76
    private function findId(object $header): string
77
    {
78
        if ($header instanceof \SimpleXMLElement) {
79
            return md5($header->asXML());
0 ignored issues
show
Bug introduced by
It seems like $header->asXML() can also be of type true; however, parameter $string of md5() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            return md5(/** @scrutinizer ignore-type */ $header->asXML());
Loading history...
80
        }
81
82
        return array_search(spl_object_id($header), $this->mapping) ?: '';
83
    }
84
85
    /**
86
     * @return \SimpleXMLElement[]
87
     */
88
    public function getRawHeaders(): array
89
    {
90
        return array_column($this->headers, 0);
91
    }
92
}
93