Passed
Push — develop ( 393819...8447c9 )
by Mikaël
74:52 queued 24:50
created

Service::fillClassConstants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use InvalidArgumentException;
8
use SoapFault;
9
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
10
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
11
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
12
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
13
use WsdlToPhp\PackageGenerator\File\Validation\Rules;
14
use WsdlToPhp\PackageGenerator\Generator\Generator;
15
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
16
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
17
use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel;
18
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
19
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
20
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader;
21
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
22
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
23
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
24
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter as PhpFunctionParameterBase;
25
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
26
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
27
28
final class Service extends AbstractModelFile
29
{
30
    public const METHOD_SET_HEADER_PREFIX = 'setSoapHeader';
31
    public const PARAM_SET_HEADER_NAMESPACE = 'namespace';
32
    public const PARAM_SET_HEADER_MUSTUNDERSTAND = 'mustUnderstand';
33
    public const PARAM_SET_HEADER_ACTOR = 'actor';
34
    public const METHOD_GET_RESULT = 'getResult';
35
36
    /**
37
     * Method model can't be found in case the original method's name is unclean:
38
     * - ex: my.operation.name becomes my_operation_name
39
     * thus the Model from Model\Service::getMethod() can't be found
40
     * So we store the generated name associated to the original method object.
41
     */
42
    protected array $methodNames = [];
43
44
    public static function getOperationMethodReturnType(MethodModel $method, Generator $generator): string
45
    {
46
        $returnType = $method->getReturnType();
47
48
        if (is_null($returnType)) {
49
            return 'null';
50
        }
51
52
        if ((($struct = $generator->getStructByName($returnType)) instanceof StructModel) && !$struct->isRestriction()) {
53
            if ($struct->isStruct()) {
54
                $returnType = $struct->getPackagedName(true);
55
            } elseif ($struct->isArray()) {
56
                if (($structInheritance = $struct->getInheritanceStruct()) instanceof StructModel) {
57 138
                    $returnType = sprintf('%s[]', $structInheritance->getPackagedName(true));
58
                } else {
59 138
                    $returnType = $struct->getInheritance();
60
                }
61
            }
62
        }
63
64
        return $returnType;
65
    }
66
67
    public function setModel(AbstractModel $model): self
68
    {
69 138
        if (!$model instanceof ServiceModel) {
70
            throw new InvalidArgumentException('Model must be an instance of a Service', __LINE__);
71 138
        }
72
73
        return parent::setModel($model);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setModel($model) returns the type WsdlToPhp\PackageGenerator\File\AbstractModelFile which includes types incompatible with the type-hinted return WsdlToPhp\PackageGenerator\File\Service.
Loading history...
74
    }
75
76
    protected function fillClassConstants(ConstantContainer $constants): void
77
    {
78
    }
79
80
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
81 138
    {
82
    }
83 138
84
    protected function fillClassProperties(PropertyContainer $properties): void
85
    {
86
    }
87
88 138
    protected function getPropertyAnnotationBlock(PhpProperty $property): ?PhpAnnotationBlock
89
    {
90 69
    }
91 138
92 138
    protected function defineUseStatements(): AbstractModelFile
93 138
    {
94 138
        $this->getFile()->addUse(SoapFault::class);
95
96
        return parent::defineUseStatements();
97
    }
98 138
99
    protected function getClassDeclarationLineText(): string
100 138
    {
101 138
        return GeneratorOptions::VALUE_NONE === $this->getGenerator()->getOptionGatherMethods() ? 'This class stands for all operations' : parent::getClassDeclarationLineText();
102 69
    }
103 138
104
    protected function fillClassMethods(): void
105
    {
106
        $this
107
            ->addSoapHeaderMethods()
108
            ->addOperationsMethods()
109 138
            ->addGetResultMethod()
110
        ;
111 138
    }
112 138
113 138
    protected function addSoapHeaderMethods(): self
114 138
    {
115 138
        foreach ($this->getModel()->getMethods() as $method) {
0 ignored issues
show
Bug introduced by
The method getMethods() does not exist on WsdlToPhp\PackageGenerator\Model\AbstractModel. Did you maybe mean getMeta()? ( Ignorable by Annotation )

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

115
        foreach ($this->getModel()->/** @scrutinizer ignore-call */ getMethods() as $method) {

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...
116 36
            $this->addSoapHeaderFromMethod($method);
117 36
        }
118 36
119 36
        return $this;
120 36
    }
121 18
122 69
    protected function addSoapHeaderFromMethod(MethodModel $method): self
123 69
    {
124 138
        $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []);
125
        $soapHeaderNamespaces = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, []);
126
        $soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []);
127
        if (is_array($soapHeaderNames) && is_array($soapHeaderNamespaces) && is_array($soapHeaderTypes)) {
128
            foreach ($soapHeaderNames as $index => $soapHeaderName) {
129
                $methodName = $this->getSoapHeaderMethodName($soapHeaderName);
130
                if (is_null($this->methods->get($methodName))) {
131
                    $soapHeaderNamespace = array_key_exists($index, $soapHeaderNamespaces) ? $soapHeaderNamespaces[$index] : null;
132
                    $soapHeaderType = array_key_exists($index, $soapHeaderTypes) ? $soapHeaderTypes[$index] : null;
133 36
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType));
0 ignored issues
show
Bug introduced by
It seems like $soapHeaderNamespace can also be of type null; however, parameter $soapHeaderNamespace of WsdlToPhp\PackageGenerat...::getSoapHeaderMethod() 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

133
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, /** @scrutinizer ignore-type */ $soapHeaderNamespace, $soapHeaderType));
Loading history...
Bug introduced by
It seems like $soapHeaderType can also be of type null; however, parameter $soapHeaderType of WsdlToPhp\PackageGenerat...::getSoapHeaderMethod() 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

133
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, /** @scrutinizer ignore-type */ $soapHeaderType));
Loading history...
134
                }
135
            }
136 36
        }
137 36
138 36
        return $this;
139 36
    }
140 36
141 18
    protected function getSoapHeaderMethod(string $methodName, string $soapHeaderName, string $soapHeaderNamespace, string $soapHeaderType): PhpMethod
142 36
    {
143 36
        try {
144 36
            $method = new PhpMethod($methodName, [
145 36
                $firstParameter = new PhpFunctionParameter(lcfirst($soapHeaderName), PhpFunctionParameterBase::NO_VALUE, $this->getTypeFromName($soapHeaderType)),
146 36
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderNamespace, self::TYPE_STRING),
147 18
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_MUSTUNDERSTAND, false, self::TYPE_BOOL),
148 36
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_ACTOR, null, '?'.self::TYPE_STRING),
149 18
            ], self::TYPE_SELF);
150
151
            $model = $this->getModelByName($soapHeaderType);
152 36
            if ($model instanceof StructModel) {
153
                $rules = new Rules($this, $method, new StructAttributeModel($model->getGenerator(), $soapHeaderType, $model->getName(), $model), $this->methods);
154
                $rules->applyRules(lcfirst($soapHeaderName));
155
                $firstParameter->setModel($model);
156
            }
157
            $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));
158 36
        } catch (InvalidArgumentException $exception) {
159
            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);
160 36
        }
161
162
        return $method;
163
    }
164
165
    protected function getTypeFromName(string $name): ?string
166 36
    {
167
        return self::getPhpType(
168 36
            $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name)),
169
            $this->getGenerator()->getOptionXsdTypesPath(),
170
            $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name))
171
        );
172
    }
173 138
174
    protected function getSoapHeaderMethodName(string $soapHeaderName): string
175 138
    {
176 138
        return sprintf('%s%s', self::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName));
177 69
    }
178 138
179
    protected function addOperationsMethods(): self
180
    {
181
        foreach ($this->getModel()->getMethods() as $method) {
182
            $this->addMainMethod($method);
183 138
        }
184
185 138
        return $this;
186 138
    }
187 138
188 138
    protected function addGetResultMethod(): self
189
    {
190
        $method = new PhpMethod(self::METHOD_GET_RESULT);
191
        $method->addChild('return parent::getResult();');
192
        $this->methods->add($method);
193
194 138
        return $this;
195
    }
196 138
197 138
    protected function addMainMethod(MethodModel $method): self
198 138
    {
199 138
        $methodFile = new Operation($method, $this->getGenerator());
200 138
        $mainMethod = $methodFile->getMainMethod();
201
        $this->methods->add($mainMethod);
202
        $this->setModelFromMethod($mainMethod, $method);
203
204
        return $this;
205 138
    }
206
207 138
    protected function getMethodAnnotationBlock(PhpMethod $method): PhpAnnotationBlock
208 138
    {
209 36
        $annotationBlock = new PhpAnnotationBlock();
210 138
        if (0 === mb_stripos($method->getName(), self::METHOD_SET_HEADER_PREFIX)) {
211 138
            $this->addAnnotationBlockForSoapHeaderMethod($annotationBlock, $method);
212 69
        } elseif (self::METHOD_GET_RESULT === $method->getName()) {
213 138
            $this->addAnnotationBlockForgetResultMethod($annotationBlock);
214
        } else {
215 138
            $this->addAnnotationBlockForOperationMethod($annotationBlock, $method);
216
        }
217
218
        return $annotationBlock;
219
    }
220
221
    protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method): self
222 36
    {
223
        $methodParameters = $method->getParameters();
224 36
        $firstParameter = array_shift($methodParameters);
225 36
        if ($firstParameter instanceof PhpFunctionParameter) {
226 36
            $annotationBlock->addChild(sprintf('Sets the %s SoapHeader param', ucfirst($firstParameter->getName())));
227 36
            $firstParameterType = $firstParameter->getType();
228 36
            if ($firstParameter->getModel() instanceof StructModel) {
229 36
                $firstParameterType = $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($firstParameter->getModel()->getGenerator(), $firstParameter->getName(), $firstParameter->getModel()->getName(), $firstParameter->getModel()));
230 36
                if ($firstParameter->getModel()->isRestriction()) {
231 36
                    $annotationBlock
232
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
233 6
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
234 6
                        ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, InvalidArgumentException::class))
235 6
                    ;
236 3
                }
237 18
            }
238
            $annotationBlock
239 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getExtends(true), self::METHOD_SET_HEADER_PREFIX)))
240 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $firstParameterType, $firstParameter->getName())))
241 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_STRING, self::PARAM_SET_HEADER_NAMESPACE)))
242 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_BOOL, self::PARAM_SET_HEADER_MUSTUNDERSTAND)))
243 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_STRING, self::PARAM_SET_HEADER_ACTOR)))
244 36
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)))
245 18
            ;
246 36
        }
247
248
        return $this;
249
    }
250
251
    protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method): self
252
    {
253 138
        if (($model = $this->getModelFromMethod($method)) instanceof MethodModel) {
254
            $operationAnnotationBlock = new OperationAnnotationBlock($model, $this->getGenerator());
255 138
            $operationAnnotationBlock->addAnnotationBlockForOperationMethod($annotationBlock);
256 138
        }
257 138
258 69
        return $this;
259 138
    }
260
261
    protected function addAnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock): self
262
    {
263
        $annotationBlock
264
            ->addChild('Returns the result')->addChild(new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::getResult()', $this->getModel()->getExtends(true))))
265 138
            ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getServiceReturnTypes()))
266
        ;
267
268 138
        return $this;
269 138
    }
270 138
271
    protected function getServiceReturnTypes(): string
272
    {
273
        $returnTypes = [];
274
        foreach ($this->getModel()->getMethods() as $method) {
275 138
            $returnTypes[] = self::getOperationMethodReturnType($method, $this->getGenerator());
276
        }
277 138
        natcasesort($returnTypes);
278 138
279 138
        return implode('|', array_unique($returnTypes));
280 69
    }
281 138
282 138
    protected function getModelFromMethod(PhpMethod $method): ?MethodModel
283
    {
284
        $model = $this->getGenerator()->getServiceMethod($method->getName());
285
        if (!$model instanceof MethodModel) {
286
            $model = array_key_exists($method->getName(), $this->methodNames) ? $this->methodNames[$method->getName()] : null;
287
        }
288 144
289
        return $model;
290 144
    }
291
292 144
    protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel): self
293 6
    {
294
        $this->methodNames[$phpMethod->getName()] = $methodModel;
295
296 138
        return $this;
297 120
    }
298
}
299