Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
100 / 100 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
Tutorial | |
100.00% |
100 / 100 |
|
100.00% |
18 / 18 |
34 | |
100.00% |
1 / 1 |
writeFile | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
addMainAnnotationBlock | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addChild | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addAutoload | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addContent | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getAnnotationBlock | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
2 | |||
addOptionsAnnotationBlock | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
addOptions | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
addAnnotationBlockFromService | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
addContentFromService | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addServiceDeclaration | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addServiceSoapHeadersDefinitions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
addServiceSoapHeadersDefinition | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
addAnnotationBlockFromMethod | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
addContentFromMethod | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
getMethodParameters | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getMethodParameter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
8 | |||
addAnnotationBlock | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace WsdlToPhp\PackageGenerator\File; |
6 | |
7 | use WsdlToPhp\PackageBase\AbstractSoapClientBase; |
8 | use WsdlToPhp\PackageGenerator\Model\Method as MethodModel; |
9 | use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel; |
10 | use WsdlToPhp\PackageGenerator\Model\Struct as StructModel; |
11 | use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader; |
12 | use WsdlToPhp\PhpGenerator\Element\PhpAnnotation; |
13 | use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock; |
14 | |
15 | final class Tutorial extends AbstractFile |
16 | { |
17 | public function writeFile(): void |
18 | { |
19 | $this |
20 | ->addMainAnnotationBlock() |
21 | ->addAutoload() |
22 | ->addOptionsAnnotationBlock() |
23 | ->addOptions() |
24 | ->addContent() |
25 | ; |
26 | parent::writeFile(); |
27 | } |
28 | |
29 | public function addMainAnnotationBlock(): self |
30 | { |
31 | $this->getFile()->addAnnotationBlockElement($this->getAnnotationBlock()); |
32 | |
33 | return $this; |
34 | } |
35 | |
36 | public function addChild(PhpAnnotationBlock $block, string $content): self |
37 | { |
38 | $block->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, $content, AbstractModelFile::ANNOTATION_LONG_LENGTH)); |
39 | |
40 | return $this; |
41 | } |
42 | |
43 | public function addAutoload(): self |
44 | { |
45 | if ($this->getGenerator()->getOptionStandalone()) { |
46 | $this->getFile()->getMainElement()->addChild(sprintf('require_once __DIR__ . \'/vendor/autoload.php\';')); |
47 | } |
48 | |
49 | return $this; |
50 | } |
51 | |
52 | public function addContent(): self |
53 | { |
54 | foreach ($this->getGenerator()->getServices(true) as $service) { |
55 | $serviceVariableName = lcfirst($service->getName()); |
56 | $this->addAnnotationBlockFromService($service)->addServiceDeclaration($serviceVariableName, $service)->addServiceSoapHeadersDefinitions($serviceVariableName, $service)->addContentFromService($serviceVariableName, $service); |
57 | } |
58 | |
59 | return $this; |
60 | } |
61 | |
62 | protected function getAnnotationBlock(): PhpAnnotationBlock |
63 | { |
64 | $block = new PhpAnnotationBlock(); |
65 | $this |
66 | ->addChild($block, 'This file aims to show you how to use this generated package.') |
67 | ->addChild($block, 'In addition, the goal is to show which methods are available and the first needed parameter(s)') |
68 | ->addChild($block, 'You have to use an associative array such as:') |
69 | ->addChild($block, '- the key must be a constant beginning with WSDL_ from AbstractSoapClientBase class (each generated ServiceType class extends this class)') |
70 | ->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)') |
71 | ->addChild($block, '$options = [') |
72 | ->addChild($block, sprintf('%s::WSDL_URL => \'%s\',', AbstractSoapClientBase::class, $this->getGenerator()->getWsdl()->getName())) |
73 | ->addChild($block, sprintf('%s::WSDL_TRACE => true,', AbstractSoapClientBase::class)) |
74 | ->addChild($block, sprintf('%s::WSDL_LOGIN => \'you_secret_login\',', AbstractSoapClientBase::class)) |
75 | ->addChild($block, sprintf('%s::WSDL_PASSWORD => \'you_secret_password\',', AbstractSoapClientBase::class)) |
76 | ->addChild($block, '];') |
77 | ->addChild($block, 'etc...') |
78 | ; |
79 | |
80 | if (!$this->getGenerator()->getOptionStandalone()) { |
81 | $this |
82 | ->addChild($block, '################################################################################') |
83 | ->addChild($block, 'Don\'t forget to add wsdltophp/packagebase:~5.0 to your main composer.json.') |
84 | ->addChild($block, '################################################################################') |
85 | ; |
86 | } |
87 | |
88 | return $block; |
89 | } |
90 | |
91 | protected function addOptionsAnnotationBlock(): self |
92 | { |
93 | $this->addAnnotationBlock([ |
94 | 'Minimal options', |
95 | ]); |
96 | |
97 | return $this; |
98 | } |
99 | |
100 | protected function addOptions(): self |
101 | { |
102 | $this |
103 | ->getFile() |
104 | ->getMainElement() |
105 | ->addChild('$options = [') |
106 | ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => \'%s\',', $this->getGenerator()->getWsdl()->getName()), 1)) |
107 | ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => %s::%s(),', $this->getGenerator()->getFiles()->getClassmapFile()->getModel()->getPackagedName(true), ClassMap::METHOD_NAME), 1)) |
108 | ->addChild('];') |
109 | ; |
110 | |
111 | return $this; |
112 | } |
113 | |
114 | protected function addAnnotationBlockFromService(ServiceModel $service): self |
115 | { |
116 | return $this->addAnnotationBlock([ |
117 | sprintf('Samples for %s ServiceType', $service->getName()), |
118 | ]); |
119 | } |
120 | |
121 | protected function addContentFromService(string $serviceVariableName, ServiceModel $service): self |
122 | { |
123 | foreach ($service->getMethods() as $method) { |
124 | $this->addAnnotationBlockFromMethod($method)->addContentFromMethod($serviceVariableName, $method); |
125 | } |
126 | |
127 | return $this; |
128 | } |
129 | |
130 | protected function addServiceDeclaration(string $serviceVariableName, ServiceModel $service): self |
131 | { |
132 | $this->getFile()->getMainElement()->addChild(sprintf('$%s = new %s($options);', $serviceVariableName, $service->getPackagedName(true))); |
133 | |
134 | return $this; |
135 | } |
136 | |
137 | protected function addServiceSoapHeadersDefinitions(string $serviceVariableName, ServiceModel $service): self |
138 | { |
139 | $added = []; |
140 | foreach ($service->getMethods() as $method) { |
141 | $added = array_merge($added, $this->addServiceSoapHeadersDefinition($serviceVariableName, $method, $added)); |
142 | } |
143 | |
144 | return $this; |
145 | } |
146 | |
147 | protected function addServiceSoapHeadersDefinition(string $serviceVariableName, MethodModel $method, array $added): array |
148 | { |
149 | $addedNames = []; |
150 | $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []); |
151 | foreach ($soapHeaderNames as $soapHeaderName) { |
152 | if (in_array($soapHeaderName, $added, true)) { |
153 | continue; |
154 | } |
155 | |
156 | $addedNames[] = $soapHeaderName; |
157 | $this->getFile()->getMainElement()->addChild(sprintf('$%s->%s%s(%s);', $serviceVariableName, Service::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName), $this->getMethodParameter($soapHeaderName))); |
158 | } |
159 | |
160 | return $addedNames; |
161 | } |
162 | |
163 | protected function addAnnotationBlockFromMethod(MethodModel $method): self |
164 | { |
165 | return $this->addAnnotationBlock([ |
166 | sprintf('Sample call for %s operation/method', $method->getMethodName()), |
167 | ]); |
168 | } |
169 | |
170 | protected function addContentFromMethod(string $serviceVariableName, MethodModel $method): self |
171 | { |
172 | $this |
173 | ->getFile() |
174 | ->getMainElement() |
175 | ->addChild(sprintf('if ($%s->%s(%s) !== false) {', $serviceVariableName, $method->getMethodName(), $this->getMethodParameters($method))) |
176 | ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('print_r($%s->getResult());', $serviceVariableName), 1)) |
177 | ->addChild('} else {') |
178 | ->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('print_r($%s->getLastError());', $serviceVariableName), 1)) |
179 | ->addChild('}') |
180 | ; |
181 | |
182 | return $this; |
183 | } |
184 | |
185 | protected function getMethodParameters(MethodModel $method): string |
186 | { |
187 | $parameters = []; |
188 | if (is_array($method->getParameterType())) { |
189 | foreach ($method->getParameterType() as $parameterName => $parameterType) { |
190 | $parameters[] = $this->getMethodParameter($parameterType, $parameterName); |
191 | } |
192 | } else { |
193 | $parameters[] = $this->getMethodParameter($method->getParameterType()); |
194 | } |
195 | |
196 | return implode(', ', $parameters); |
197 | } |
198 | |
199 | protected function getMethodParameter(?string $parameterType, ?string $parameterName = null): string |
200 | { |
201 | $parameter = sprintf('%1$s%2$s', (empty($parameterType) && empty($parameterName)) ? '' : '$', empty($parameterName) ? $parameterType : $parameterName); |
202 | $model = null !== $parameterType ? $this->getGenerator()->getStructByName($parameterType) : null; |
203 | if ($model instanceof StructModel && $model->isStruct() && !$model->isRestriction()) { |
204 | $parameter = sprintf('new %s()', $model->getPackagedName(true)); |
205 | } |
206 | |
207 | return $parameter; |
208 | } |
209 | |
210 | protected function addAnnotationBlock($content): self |
211 | { |
212 | $this->getFile()->getMainElement()->addChild(new PhpAnnotationBlock($content)); |
213 | |
214 | return $this; |
215 | } |
216 | } |