Passed
Pull Request — develop (#234)
by Mikaël
49:21 queued 46:19
created

AbstractParser::parse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 4
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Parser\Wsdl;
6
7
use WsdlToPhp\PackageGenerator\Model\Schema;
8
use WsdlToPhp\PackageGenerator\Model\Wsdl;
9
use WsdlToPhp\PackageGenerator\Parser\AbstractParser as Parser;
10
use WsdlToPhp\WsdlHandler\Tag\AbstractTag;
11
12
abstract class AbstractParser extends Parser
13
{
14
    /**
15
     * @var AbstractTag[]
16
     */
17
    protected array $tags;
18
19
    /**
20
     * List of Wsdl parsed for the current tag.
21
     */
22
    protected array $parsedWsdls = [];
23
24
    /**
25
     * List of Schema parsed for the current tag.
26
     */
27
    protected array $parsedSchemas = [];
28
29
    /**
30
     * The method takes care of looping among WSDLS as much time as it is needed.
31
     */
32 114
    final public function parse(): void
33
    {
34 114
        $wsdl = $this->generator->getWsdl();
35
36 114
        if (!$this->isWsdlParsed($wsdl)) {
37
            $this
38 114
                ->setWsdlAsParsed($wsdl)
39 114
                ->setTags($wsdl->getContent()->getElementsByName($this->parsingTag()))
40 114
                ->parseWsdl($wsdl)
41
            ;
42
        }
43
44
        /** @var Schema $schema */
45 114
        foreach ($wsdl->getSchemas() as $schema) {
46 66
            if ($this->isSchemaParsed($wsdl, $schema)) {
47 58
                continue;
48
            }
49
50 66
            $this->setSchemaAsParsed($wsdl, $schema);
51
52
            $this
53 66
                ->setTags($schema->getContent()->getElementsByName($this->parsingTag()))
54 66
                ->parseSchema($wsdl, $schema)
55
            ;
56
        }
57 114
    }
58
59 114
    public function getTags(): array
60
    {
61 114
        return $this->tags;
62
    }
63
64 114
    public function isWsdlParsed(Wsdl $wsdl): bool
65
    {
66 114
        return array_key_exists($wsdl->getName(), $this->parsedWsdls) && is_array($this->parsedWsdls[$wsdl->getName()]) && in_array($this->parsingTag(), $this->parsedWsdls[$wsdl->getName()]);
67
    }
68
69 66
    public function isSchemaParsed(Wsdl $wsdl, Schema $schema): bool
70
    {
71 66
        $key = $wsdl->getName().$schema->getName();
72
73 66
        return array_key_exists($key, $this->parsedSchemas) && is_array($this->parsedSchemas[$key]) && in_array($this->parsingTag(), $this->parsedSchemas[$key]);
74
    }
75
76
    abstract protected function parseWsdl(Wsdl $wsdl): void;
77
78
    abstract protected function parseSchema(Wsdl $wsdl, Schema $schema): void;
79
80
    abstract protected function parsingTag(): string;
81
82 114
    protected function setTags(array $tags): self
83
    {
84 114
        $this->tags = $tags;
85
86 114
        return $this;
87
    }
88
89 114
    protected function setWsdlAsParsed(Wsdl $wsdl): self
90
    {
91 114
        if (!array_key_exists($wsdl->getName(), $this->parsedWsdls)) {
92 114
            $this->parsedWsdls[$wsdl->getName()] = [];
93
        }
94 114
        $this->parsedWsdls[$wsdl->getName()][] = $this->parsingTag();
95
96 114
        return $this;
97
    }
98
99 66
    protected function setSchemaAsParsed(Wsdl $wsdl, Schema $schema): self
100
    {
101 66
        $key = $wsdl->getName().$schema->getName();
102 66
        if (!array_key_exists($key, $this->parsedSchemas)) {
103 66
            $this->parsedSchemas[$key] = [];
104
        }
105 66
        $this->parsedSchemas[$key][] = $this->parsingTag();
106
107 66
        return $this;
108
    }
109
}
110