Completed
Push — 1.x ( 257780...f31d1d )
by Mikaël
42:33 queued 33:38
created

Tutorial   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 12
dl 0
loc 254
ccs 140
cts 140
cp 1
rs 9.2
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A writeFile() 0 10 1
A addMainAnnotationBlock() 0 5 1
B getAnnotationBlock() 0 24 2
A addChild() 0 5 1
A addAutoload() 0 10 2
A addContent() 0 12 2
A addOptionsAnnotationBlock() 0 7 1
A addOptions() 0 11 1
A addAnnotationBlockFromService() 0 6 1
A addContentFromService() 0 9 2
A addServiceDeclaration() 0 8 1
A addServiceSoapHeadersDefinitions() 0 8 2
A addServiceSoapHeadersDefinition() 0 15 3
A addAnnotationBlockFromMethod() 0 6 1
A addContentFromMethod() 0 12 1
A getMethodParameters() 0 12 3
B getMethodParameter() 0 9 8
A addAnnotationBlock() 0 8 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader;
6
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
7
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
8
use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel;
9
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
10
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
11
12
class Tutorial extends AbstractFile
13
{
14
    /**
15
     * @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile()
16
     * @return int|bool
17
     */
18 44
    public function writeFile()
19
    {
20 44
        $this
21 44
            ->addMainAnnotationBlock()
22 44
            ->addAutoload()
23 44
            ->addOptionsAnnotationBlock()
24 44
            ->addOptions()
25 44
            ->addContent();
26 44
        return parent::writeFile();
27
    }
28
    /**
29
     * @return Tutorial
30
     */
31 44
    public function addMainAnnotationBlock()
32
    {
33 44
        $this->getFile()->addAnnotationBlockElement($this->getAnnotationBlock());
34 44
        return $this;
35
    }
36
    /**
37
     * @return PhpAnnotationBlock
38
     */
39 44
    protected function getAnnotationBlock()
40
    {
41 44
        $block = new PhpAnnotationBlock();
42 44
        $this
43 44
            ->addChild($block, 'This file aims to show you how to use this generated package.')
44 44
            ->addChild($block, 'In addition, the goal is to show which methods are available and the fist needed parameter(s)')
45 44
            ->addChild($block, 'You have to use an associative array such as:')
46 44
            ->addChild($block, '- the key must be a constant beginning with WSDL_ from AbstractSoapClientbase class each generated ServiceType class extends this class')
47 44
            ->addChild($block, '- the value must be the corresponding key value (each option matches a {@link http://www.php.net/manual/en/soapclient.soapclient.php} option)')
48 44
            ->addChild($block, '$options = array(')
49 44
            ->addChild($block, sprintf('\WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => \'%s\',', $this->getGenerator()->getWsdl()->getName()))
50 44
            ->addChild($block, '\WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_TRACE => true,')
51 44
            ->addChild($block, '\WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_LOGIN => \'you_secret_login\',')
52 44
            ->addChild($block, '\WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_PASSWORD => \'you_secret_password\',')
53 44
            ->addChild($block, ');')
54 44
            ->addChild($block, 'etc....');
55 44
        if ($this->getGenerator()->getOptionStandalone() === false) {
56 8
            $this
57 8
                ->addChild($block, '################################################################################')
58 8
                ->addChild($block, 'Don\'t forget to add wsdltophp/packagebase:dev-master to your main composer.json.')
59 8
                ->addChild($block, '################################################################################');
60 8
        }
61 44
        return $block;
62
    }
63
    /**
64
     * @param PhpAnnotationBlock $block
65
     * @param string $content
66
     * @return Tutorial
67
     */
68 44
    public function addChild(PhpAnnotationBlock $block, $content)
69
    {
70 44
        $block->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, $content, AbstractModelFile::ANNOTATION_LONG_LENGTH));
71 44
        return $this;
72
    }
73
    /**
74
     * @return Tutorial
75
     */
76 44
    public function addAutoload()
77
    {
78 44
        if ($this->getGenerator()->getOptionStandalone() === true) {
79 36
            $this
80 36
                ->getFile()
81 36
                ->getMainElement()
82 36
                ->addChild(sprintf('require_once __DIR__ . \'/vendor/autoload.php\';'));
83 36
        }
84 44
        return $this;
85
    }
86
    /**
87
     * @return Tutorial
88
     */
89 44
    public function addContent()
90
    {
91 44
        foreach ($this->getGenerator()->getServices() as $service) {
92 44
            $serviceVariableName = lcfirst($service->getName());
93 44
            $this
94 44
                ->addAnnotationBlockFromService($service)
95 44
                ->addServiceDeclaration($serviceVariableName, $service)
96 44
                ->addServiceSoapHeadersDefinitions($serviceVariableName, $service)
97 44
                ->addContentFromService($serviceVariableName, $service);
98 44
        }
99 44
        return $this;
100
    }
101
    /**
102
     * @return Tutorial
103
     */
104 44
    protected function addOptionsAnnotationBlock()
105
    {
106 44
        $this->addAnnotationBlock(array(
107 44
            'Minimal options',
108 44
        ));
109 44
        return $this;
110
    }
111
    /**
112
     * @return Tutorial
113
     */
114 44
    protected function addOptions()
115
    {
116 44
        $this
117 44
            ->getFile()
118 44
            ->getMainElement()
119 44
            ->addChild('$options = array(')
120 44
                ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('\WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => \'%s\',', $this->getGenerator()->getWsdl()->getName()), 1))
121 44
                ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('\WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => %s::%s(),', $this->getGenerator()->getFiles()->getClassmapFile()->getModel()->getPackagedName(true), ClassMap::METHOD_NAME), 1))
122 44
            ->addChild(');');
123 44
        return $this;
124
    }
125
    /**
126
     * @param ServiceModel $service
127
     * @return Tutorial
128
     */
129 44
    protected function addAnnotationBlockFromService(ServiceModel $service)
130
    {
131 44
        return $this->addAnnotationBlock(array(
132 44
            sprintf('Samples for %s ServiceType', $service->getName()),
133 44
        ));
134
    }
135
    /**
136
     * @param string $serviceVariableName
137
     * @param Service $service
138
     * @return Tutorial
139
     */
140 44
    protected function addContentFromService($serviceVariableName, ServiceModel $service)
141
    {
142 44
        foreach ($service->getMethods() as $method) {
143 44
            $this
144 44
                ->addAnnotationBlockFromMethod($method)
145 44
                ->addContentFromMethod($serviceVariableName, $method);
146 44
        }
147 44
        return $this;
148
    }
149
    /**
150
     * @param string $serviceVariableName
151
     * @param ServiceModel $service
152
     * @return Tutorial
153
     */
154 44
    protected function addServiceDeclaration($serviceVariableName, ServiceModel $service)
155
    {
156 44
        $this
157 44
            ->getFile()
158 44
            ->getMainElement()
159 44
            ->addChild(sprintf('$%s = new %s($options);', $serviceVariableName, $service->getPackagedName(true)));
160 44
        return $this;
161
    }
162
    /**
163
     * @param string $serviceVariableName
164
     * @param ServiceModel $service
165
     * @return Tutorial
166
     */
167 44
    protected function addServiceSoapHeadersDefinitions($serviceVariableName, ServiceModel $service)
168
    {
169 44
        $added = array();
170 44
        foreach ($service->getMethods() as $method) {
171 44
            $added = array_merge($added, $this->addServiceSoapHeadersDefinition($serviceVariableName, $method, $added));
172 44
        }
173 44
        return $this;
174
    }
175
    /**
176
     * @param string $serviceVariableName
177
     * @param MethodModel $method
178
     * @param array $added
179
     * @return string[]
180
     */
181 44
    protected function addServiceSoapHeadersDefinition($serviceVariableName, MethodModel $method, array $added)
182
    {
183 44
        $addedNames = array();
184 44
        $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, array());
185 44
        foreach ($soapHeaderNames as $soapHeaderName) {
186 4
            if (!in_array($soapHeaderName, $added, true)) {
187 4
                $addedNames[] = $soapHeaderName;
188 4
                $this
189 4
                    ->getFile()
190 4
                    ->getMainElement()
191 4
                    ->addChild(sprintf('$%s->%s%s(%s);', $serviceVariableName, Service::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName), $this->getMethodParameter($soapHeaderName)));
192 4
            }
193 44
        }
194 44
        return $addedNames;
195
    }
196
    /**
197
     * @param MethodModel $method
198
     * @return Tutorial
199
     */
200 44
    protected function addAnnotationBlockFromMethod(MethodModel $method)
201
    {
202 44
        return $this->addAnnotationBlock(array(
203 44
            sprintf('Sample call for %s operation/method', $method->getMethodName()),
204 44
        ));
205
    }
206
    /**
207
     * @param string $serviceVariableName
208
     * @param MethodModel $method
209
     * @return Tutorial
210
     */
211 44
    protected function addContentFromMethod($serviceVariableName, MethodModel $method)
212
    {
213 44
        $this
214 44
            ->getFile()
215 44
            ->getMainElement()
216 44
            ->addChild(sprintf('if ($%s->%s(%s) !== false) {', $serviceVariableName, $method->getMethodName(), $this->getMethodParameters($method)))
217 44
                ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('print_r($%s->getResult());', $serviceVariableName), 1))
218 44
            ->addChild('} else {')
219 44
                ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('print_r($%s->getLastError());', $serviceVariableName), 1))
220 44
            ->addChild('}');
221 44
        return $this;
222
    }
223
    /**
224
     * @param MethodModel $method
225
     * @return string
226
     */
227 44
    protected function getMethodParameters(MethodModel $method)
228
    {
229 44
        $parameters = array();
230 44
        if (is_array($method->getParameterType())) {
231 40
            foreach ($method->getParameterType() as $parameterName => $parameterType) {
232 40
                $parameters[] = $this->getMethodParameter($parameterType, $parameterName);
233 40
            }
234 40
        } else {
235 16
            $parameters[] = $this->getMethodParameter($method->getParameterType());
236
        }
237 44
        return implode(', ', $parameters);
238
    }
239
    /**
240
     * @param string $parameterType
241
     * @param string $parameterName
242
     * @return string
243
     */
244 44
    protected function getMethodParameter($parameterType, $parameterName = null)
245
    {
246 44
        $parameter = sprintf('%1$s%2$s', (empty($parameterType) && empty($parameterName)) ? '' : '$', empty($parameterName) ? $parameterType : $parameterName);
247 44
        $model = $parameterType !== null ? $this->getGenerator()->getStruct($parameterType) : null;
248 44
        if ($model instanceof StructModel && $model->getIsStruct() && !$model->getIsRestriction()) {
249 44
            $parameter = sprintf('new %s()', $model->getPackagedName(true));
250 44
        }
251 44
        return $parameter;
252
    }
253
    /**
254
     * @param string[]|PhpAnnotation[] $content
255
     * @return Tutorial
256
     */
257 44
    protected function addAnnotationBlock($content)
258
    {
259 44
        $this
260 44
            ->getFile()
261 44
            ->getMainElement()
262 44
            ->addChild(new PhpAnnotationBlock($content));
263 44
        return $this;
264
    }
265
}
266