Completed
Push — feature/issue-47 ( cdce8d...8413b2 )
by Mikaël
24:42
created

AbstractParser::parse()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 25
cts 25
cp 1
rs 6.7272
cc 7
eloc 18
nc 10
nop 0
crap 7
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Parser\Wsdl;
4
5
use WsdlToPhp\PackageGenerator\Model\Wsdl;
6
use WsdlToPhp\PackageGenerator\Model\Schema;
7
use WsdlToPhp\PackageGenerator\Parser\AbstractParser as Parser;
8
use WsdlToPhp\PackageGenerator\Generator\Generator;
9
use WsdlToPhp\PackageGenerator\DomHandler\Wsdl\Wsdl as WsdlDocument;
10
use WsdlToPhp\PackageGenerator\DomHandler\Wsdl\Schema as SchemaDocument;
11
12
abstract class AbstractParser extends Parser
13
{
14
    /**
15
     * @var Generator
16
     */
17
    protected $generator;
18
    /**
19
     * @var AbstractTag[]
20
     */
21
    protected $tags;
22
    /**
23
     * List of Wsdl parsed for the current tag
24
     * @var array
25
     */
26
    private $parsedWsdls;
27
    /**
28
     * List of Schema parsed for the current tag
29
     * @var array
30
     */
31
    private $parsedSchemas;
32
    /**
33
     *
34
     * @param Generator $generator
35
     */
36 452
    public function __construct(Generator $generator)
37
    {
38 452
        $this->generator = $generator;
39 452
        $this->parsedWsdls = array();
40 452
        $this->parsedSchemas = array();
41 452
    }
42
    /**
43
     * The method takes care of looping among WSDLS as much time as it is needed
44
     * @see \WsdlToPhp\PackageGenerator\Generator\ParserInterface::parse()
45
     */
46 392
    final public function parse()
47
    {
48 392
        $wsdl = $this->generator->getWsdl();
49 392
        if ($wsdl instanceof Wsdl) {
50 392
            $content = $wsdl->getContent();
51 392
            if ($content instanceof WsdlDocument) {
52 392
                if ($this->isWsdlParsed($wsdl) === false) {
53 294
                    $this
54 392
                        ->setWsdlAsParsed($wsdl)
55 392
                        ->setTags($content->getElementsByName($this->parsingTag()))
56 392
                        ->parseWsdl($wsdl);
57 294
                }
58 392
                foreach ($content->getExternalSchemas() as $schema) {
59 144
                    if ($this->isSchemaParsed($wsdl, $schema) === false) {
60 144
                        $this->setSchemaAsParsed($wsdl, $schema);
61 144
                        $schemaContent = $schema->getContent();
62 144
                        if ($schemaContent instanceof SchemaDocument) {
63 108
                            $this
64 144
                                ->setTags($schemaContent->getElementsByName($this->parsingTag()))
65 144
                                ->parseSchema($wsdl, $schema);
66 108
                        }
67 108
                    }
68 294
                }
69 294
            }
70 294
        }
71 392
    }
72
    /**
73
     * Actual parsing of the Wsdl
74
     * @param Wsdl $wsdl
75
     */
76
    abstract protected function parseWsdl(Wsdl $wsdl);
77
    /**
78
     * Actual parsing of the Schema
79
     * @param Wsdl $wsdl
80
     * @param Schema $schema
81
     */
82
    abstract protected function parseSchema(Wsdl $wsdl, Schema $schema);
83
    /**
84
     * Must return the tag that will be parsed
85
     * @return string
86
     */
87
    abstract protected function parsingTag();
88
    /**
89
     * @param array $tags
90
     * @return \WsdlToPhp\PackageGenerator\Parser\Wsdl\AbstractParser
91
     */
92 392
    private function setTags(array $tags)
93
    {
94 392
        $this->tags = $tags;
95 392
        return $this;
96
    }
97
    /**
98
     * @return AbstractTag[]
99
     */
100 392
    public function getTags()
101
    {
102 392
        return $this->tags;
103
    }
104
    /**
105
     * @param Wsdl $wsdl
106
     * @return AbstractParser
107
     */
108 392
    private function setWsdlAsParsed(Wsdl $wsdl)
109
    {
110 392
        if (!array_key_exists($wsdl->getName(), $this->parsedWsdls)) {
111 392
            $this->parsedWsdls[$wsdl->getName()] = array();
112 294
        }
113 392
        $this->parsedWsdls[$wsdl->getName()][] = $this->parsingTag();
114 392
        return $this;
115
    }
116
    /**
117
     * @param Wsdl $wsdl
118
     * @return boolean
119
     */
120 392
    public function isWsdlParsed(Wsdl $wsdl)
121
    {
122
        return
123 392
            array_key_exists($wsdl->getName(), $this->parsedWsdls) &&
124 392
            is_array($this->parsedWsdls[$wsdl->getName()]) &&
125 392
            in_array($this->parsingTag(), $this->parsedWsdls[$wsdl->getName()]);
126
    }
127
    /**
128
     * @param Wsdl $wsdl
129
     * @param Schema $schema
130
     * @return AbstractParser
131
     */
132 144
    private function setSchemaAsParsed(Wsdl $wsdl, Schema $schema)
133
    {
134 144
        $key = $wsdl->getName() . $schema->getName();
135 144
        if (!array_key_exists($key, $this->parsedSchemas)) {
136 144
            $this->parsedSchemas[$key] = array();
137 108
        }
138 144
        $this->parsedSchemas[$key][] = $this->parsingTag();
139 144
        return $this;
140
    }
141
    /**
142
     * @param Wsdl $wsdl
143
     * @param Schema $schema
144
     * @return boolean
145
     */
146 144
    public function isSchemaParsed(Wsdl $wsdl, Schema $schema)
147
    {
148 144
        $key = $wsdl->getName() . $schema->getName();
149
        return
150 144
            array_key_exists($key, $this->parsedSchemas) &&
151 144
            is_array($this->parsedSchemas[$key]) &&
152 144
            in_array($this->parsingTag(), $this->parsedSchemas[$key]);
153
    }
154
}
155