Completed
Push — feature/issue-181 ( e82b4d...84d42b )
by Mikaël
25:08
created

Service::addAnnotationBlockForSoapHeaderMethod()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.5154

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 4
nop 2
dl 0
loc 25
ccs 15
cts 22
cp 0.6818
crap 4.5154
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader;
6
use WsdlToPhp\PackageGenerator\Generator\Generator;
7
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
9
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
10
use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel;
11
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
12
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
13
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
14
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
15
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
16
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
17
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
18
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
19
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter as PhpFunctionParameterBase;
20
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
21
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
22
use WsdlToPhp\PackageGenerator\File\Validation\Rules;
23
24
class Service extends AbstractModelFile
25
{
26
    /**
27
     * @var string
28
     */
29
    const METHOD_SET_HEADER_PREFIX = 'setSoapHeader';
30
    /**
31
     * @var string
32
     */
33
    const PARAM_SET_HEADER_NAMESPACE = 'nameSpace';
34
    /**
35
     * @var string
36
     */
37
    const PARAM_SET_HEADER_MUSTUNDERSTAND = 'mustUnderstand';
38
    /**
39
     * @var string
40
     */
41
    const PARAM_SET_HEADER_ACTOR = 'actor';
42
    /**
43
     * @var string
44
     */
45
    const METHOD_GET_RESULT = 'getResult';
46
    /**
47
     * Method model can't be found in case the original method's name is unclean:
48
     * - ex: my.operation.name becomes my_operation_name
49
     * thus the Model from Model\Service::getMethod() can't be found
50
     * So we store the generated name associated to the original method object
51
     * @var array
52
     */
53
    protected $methodNames = [];
54
    /**
55
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants()
56
     */
57 138
    protected function getClassConstants(ConstantContainer $constants)
58
    {
59 138
    }
60
    /**
61
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
62
     */
63
    protected function getConstantAnnotationBlock(PhpConstant $constant)
64
    {
65
    }
66
    /**
67
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassProperties()
68
     */
69 138
    protected function getClassProperties(PropertyContainer $properties)
70
    {
71 138
    }
72
    /**
73
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getPropertyAnnotationBlock()
74
     */
75
    protected function getPropertyAnnotationBlock(PhpProperty $property)
76
    {
77
    }
78
    /**
79
     * @return string
80
     */
81 138
    protected function getClassDeclarationLineText()
82
    {
83 138
        return $this->getGenerator()->getOptionGatherMethods() === GeneratorOptions::VALUE_NONE ? 'This class stands for all operations' : parent::getClassDeclarationLineText();
84
    }
85
    /**
86
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::fillClassMethods()
87
     */
88 138
    protected function fillClassMethods()
89
    {
90 69
        $this
91 138
            ->addSoapHeaderMethods()
92 138
            ->addOperationsMethods()
93 138
            ->addGetResultMethod();
94 138
    }
95
    /**
96
     * @return Service
97
     */
98 138
    protected function addSoapHeaderMethods()
99
    {
100 138
        foreach ($this->getModel()->getMethods() as $method) {
101 138
            $this->addSoapHeaderFromMethod($method);
102 69
        }
103 138
        return $this;
104
    }
105
    /**
106
     * @param MethodModel $method
107
     * @return Service
108
     */
109 138
    protected function addSoapHeaderFromMethod(MethodModel $method)
110
    {
111 138
        $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []);
112 138
        $soapHeaderNamespaces = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, []);
113 138
        $soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []);
114 138
        if (is_array($soapHeaderNames) && is_array($soapHeaderNamespaces) && is_array($soapHeaderTypes)) {
115 138
            foreach ($soapHeaderNames as $index => $soapHeaderName) {
116 36
                $methodName = $this->getSoapHeaderMethodName($soapHeaderName);
117 36
                if ($this->methods->get($methodName) === null) {
118 36
                    $soapHeaderNamespace = array_key_exists($index, $soapHeaderNamespaces) ? $soapHeaderNamespaces[$index] : null;
119 36
                    $soapHeaderType = array_key_exists($index, $soapHeaderTypes) ? $soapHeaderTypes[$index] : null;
120 36
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType));
121 18
                }
122 69
            }
123 69
        }
124 138
        return $this;
125
    }
126
    /**
127
     * @param string $methodName
128
     * @param string $soapHeaderName
129
     * @param string $soapHeaderNamespace
130
     * @param string $soapHeaderType
131
     * @return PhpMethod
132
     */
133 36
    protected function getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType)
134
    {
135
        try {
136 36
            $method = new PhpMethod($methodName, [
137 36
                $firstParameter = new PhpFunctionParameter(lcfirst($soapHeaderName), PhpFunctionParameterBase::NO_VALUE, $this->getTypeFromName($soapHeaderType)),
138 36
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderNamespace),
139 36
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_MUSTUNDERSTAND, false),
140 36
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_ACTOR, null),
141 18
            ]);
142 36
            $model = $this->getModelByName($soapHeaderType);
143 36
            if ($model instanceof StructModel) {
144 36
                $rules = new Rules($this, $method, new StructAttributeModel($model->getGenerator(), $soapHeaderType, $model->getName(), $model), $this->methods);
145 36
                $rules->applyRules(lcfirst($soapHeaderName));
146 36
                $firstParameter->setModel($model);
147 18
            }
148 36
            $method->addChild(sprintf('return $this->%s($%s, \'%s\', $%s, $%s, $%s);', self::METHOD_SET_HEADER_PREFIX, self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderName, lcfirst($soapHeaderName), self::PARAM_SET_HEADER_MUSTUNDERSTAND, self::PARAM_SET_HEADER_ACTOR));
149 18
        } catch (\InvalidArgumentException $exception) {
150
            throw new \InvalidArgumentException(sprintf('Unable to create function parameter for service "%s" with type "%s"', $this->getModel()->getName(), var_export($this->getTypeFromName($soapHeaderName), true)), __LINE__, $exception);
151
        }
152 36
        return $method;
153
    }
154
    /**
155
     * @param string $name
156
     * @return string
157
     */
158 36
    protected function getTypeFromName($name)
159
    {
160 36
        $type = $name;
161 36
        $model = $this->getModelByName($name);
162 36
        if ($model instanceof StructModel) {
163 36
            if ($model->isRestriction() || $model->isUnion()) {
164 6
                $type = self::TYPE_STRING;
165 3
            } else {
166 36
                $type = $model->getPackagedName(true);
167
            }
168 18
        }
169 36
        return self::getValidType($type, $this->getGenerator()->getOptionXsdTypesPath());
170
    }
171
    /**
172
     * @param string $soapHeaderName
173
     * @return string
174
     */
175 36
    protected function getSoapHeaderMethodName($soapHeaderName)
176
    {
177 36
        return sprintf('%s%s', self::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName));
178
    }
179
    /**
180
     * @return Service
181
     */
182 138
    protected function addOperationsMethods()
183
    {
184 138
        foreach ($this->getModel()->getMethods() as $method) {
185 138
            $this->addMainMethod($method);
186 69
        }
187 138
        return $this;
188
    }
189
    /**
190
     * @return Service
191
     */
192 138
    protected function addGetResultMethod()
193
    {
194 138
        $method = new PhpMethod(self::METHOD_GET_RESULT);
195 138
        $method->addChild('return parent::getResult();');
196 138
        $this->methods->add($method);
197 138
        return $this;
198
    }
199
    /**
200
     * @param MethodModel $method
201
     * @return Service
202
     */
203 138
    protected function addMainMethod(MethodModel $method)
204
    {
205 138
        $methodFile = new Operation($method, $this->getGenerator());
206 138
        $mainMethod = $methodFile->getMainMethod();
207 138
        $this->methods->add($mainMethod);
208 138
        $this->setModelFromMethod($mainMethod, $method);
209 138
        return $this;
210
    }
211
    /**
212
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getMethodAnnotationBlock()
213
     */
214 138
    protected function getMethodAnnotationBlock(PhpMethod $method)
215
    {
216 138
        $annotationBlock = new PhpAnnotationBlock();
217 138
        if (mb_stripos($method->getName(), self::METHOD_SET_HEADER_PREFIX) === 0) {
218 36
            $this->addAnnotationBlockForSoapHeaderMethod($annotationBlock, $method);
219 138
        } elseif ($method->getName() === self::METHOD_GET_RESULT) {
220 138
            $this->addAnnnotationBlockForgetResultMethod($annotationBlock);
221 69
        } else {
222 138
            $this->addAnnotationBlockForOperationMethod($annotationBlock, $method);
223
        }
224 138
        return $annotationBlock;
225
    }
226
    /**
227
     * @param PhpAnnotationBlock $annotationBlock
228
     * @param PhpMethod $method
229
     * @return Service
230
     */
231 36
    protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method)
232
    {
233 36
        $methodParameters = $method->getParameters();
234 36
        $firstParameter = array_shift($methodParameters);
235 36
        if ($firstParameter instanceof PhpFunctionParameter) {
236 36
            $annotationBlock->addChild(sprintf('Sets the %s SoapHeader param', ucfirst($firstParameter->getName())));
237 36
            $firstParameterType = $firstParameter->getType();
238 36
            if ($firstParameter->getModel() instanceof StructAttributeModel) {
239
                $firstParameterType = $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($firstParameter->getModel()->getGenerator(), $firstParameter->getName(), $firstParameter->getModel()->getName(), $firstParameter->getModel()));
0 ignored issues
show
Bug introduced by
$firstParameter->getModel() of type WsdlToPhp\PackageGenerator\Model\StructAttribute is incompatible with the type WsdlToPhp\PackageGenerator\Model\Struct|null expected by parameter $struct of WsdlToPhp\PackageGenerat...ttribute::__construct(). ( Ignorable by Annotation )

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

239
                $firstParameterType = $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($firstParameter->getModel()->getGenerator(), $firstParameter->getName(), $firstParameter->getModel()->getName(), /** @scrutinizer ignore-type */ $firstParameter->getModel()));
Loading history...
240
                if ($firstParameter->getModel()->isRestriction()) {
0 ignored issues
show
Bug introduced by
The method isRestriction() does not exist on WsdlToPhp\PackageGenerator\Model\StructAttribute. ( Ignorable by Annotation )

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

240
                if ($firstParameter->getModel()->/** @scrutinizer ignore-call */ isRestriction()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
                    $annotationBlock
242
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
243
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
244
                        ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
245
                }
246
            }
247
            $annotationBlock
248 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::setSoapHeader()', $this->getModel()->getExtends(true))))
249 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $firstParameterType, $firstParameter->getName())))
0 ignored issues
show
Bug introduced by
It seems like $firstParameterType can also be of type WsdlToPhp\PhpGenerator\Element\PhpClass; however, parameter $args of sprintf() 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

249
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', /** @scrutinizer ignore-type */ $firstParameterType, $firstParameter->getName())))
Loading history...
250 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('string $%s', self::PARAM_SET_HEADER_NAMESPACE)))
251 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('bool $%s', self::PARAM_SET_HEADER_MUSTUNDERSTAND)))
252 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('string $%s', self::PARAM_SET_HEADER_ACTOR)))
253 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'bool'));
254 18
        }
255 36
        return $this;
256
    }
257
    /**
258
     * @param PhpAnnotationBlock $annotationBlock
259
     * @param PhpMethod $method
260
     * @return Service
261
     */
262 138
    protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method)
263
    {
264 138
        if (($model = $this->getModelFromMethod($method)) instanceof MethodModel) {
265 138
            $operationAnnotationBlock = new OperationAnnotationBlock($model, $this->getGenerator());
266 138
            $operationAnnotationBlock->addAnnotationBlockForOperationMethod($annotationBlock);
267 69
        }
268 138
        return $this;
269
    }
270
    /**
271
     * @param PhpAnnotationBlock $annotationBlock
272
     * @return Service
273
     */
274 138
    protected function addAnnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock)
275
    {
276
        $annotationBlock
277 138
            ->addChild('Returns the result')->addChild(new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::getResult()', $this->getModel()->getExtends(true))))
278 138
            ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getServiceReturnTypes()));
279 138
        return $this;
280
    }
281
    /**
282
     * @return string
283
     */
284 138
    protected function getServiceReturnTypes()
285
    {
286 138
        $returnTypes = [];
287 138
        foreach ($this->getModel()->getMethods() as $method) {
288 138
            $returnTypes[] = self::getOperationMethodReturnType($method, $this->getGenerator());
289 69
        }
290 138
        natcasesort($returnTypes);
291 138
        return implode('|', array_unique($returnTypes));
292
    }
293
    /**
294
     * @param MethodModel $method
295
     * @return string
296
     */
297 138
    public static function getOperationMethodReturnType(MethodModel $method, Generator $generator)
298
    {
299 138
        $returnType = $method->getReturnType();
300 138
        if ((($struct = $generator->getStructByName($returnType)) instanceof StructModel) && !$struct->isRestriction()) {
301 120
            if ($struct->isStruct()) {
302 120
                $returnType = $struct->getPackagedName(true);
303 72
            } elseif ($struct->isArray()) {
304 24
                if (($structInheritance = $struct->getInheritanceStruct()) instanceof StructModel) {
305 24
                    $returnType = sprintf('%s[]', $structInheritance->getPackagedName(true));
306 12
                } else {
307 12
                    $returnType = $struct->getInheritance();
308
                }
309 12
            }
310 60
        }
311 138
        return $returnType;
312
    }
313
    /**
314
     * @param PhpMethod $method
315
     * @return MethodModel|null
316
     */
317 138
    protected function getModelFromMethod(PhpMethod $method)
318
    {
319 138
        $model = $this->getGenerator()->getServiceMethod($method->getName());
320 138
        if (!$model instanceof MethodModel) {
321 24
            $model = array_key_exists($method->getName(), $this->methodNames) ? $this->methodNames[$method->getName()] : null;
322 12
        }
323 138
        return $model;
324
    }
325
    /**
326
     * @param PhpMethod $phpMethod
327
     * @param MethodModel $methodModel
328
     * @return Service
329
     */
330 138
    protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel)
331
    {
332 138
        $this->methodNames[$phpMethod->getName()] = $methodModel;
333 138
        return $this;
334
    }
335
    /**
336
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel()
337
     * @return ServiceModel
338
     */
339 144
    public function getModel()
340
    {
341 144
        return parent::getModel();
342
    }
343
    /**
344
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
345
     * @throws \InvalidArgumentException
346
     * @param AbstractModel $model
347
     * @return Service
348
     */
349 150
    public function setModel(AbstractModel $model)
350
    {
351 150
        if (!$model instanceof ServiceModel) {
352 6
            throw new \InvalidArgumentException('Model must be an instance of a Service', __LINE__);
353
        }
354 144
        return parent::setModel($model);
355
    }
356
}
357