Passed
Push — feature/issue-124 ( 91e4aa...dde646 )
by Mikaël
09:46
created

Operation::getOperationCallParametersEnding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
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 WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
8
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
9
10
final class Operation extends AbstractOperation
11
{
12 47
    public function getMainMethod(): PhpMethod
13
    {
14 47
        $phpMethod = new PhpMethod($this->getMethod()->getMethodName(), []);
15
        $this
16 47
            ->defineParameters($phpMethod)
17 47
            ->defineBody($phpMethod)
18
        ;
19
20 47
        return $phpMethod;
21
    }
22
23 47
    protected function defineParameters(PhpMethod $method): self
24
    {
25 47
        return $this->defineParametersFromArray($method)->defineParametersFromModel($method)->defineParametersFromString($method);
26
    }
27
28 47
    protected function defineParametersFromArray(PhpMethod $method): self
29
    {
30 47
        if ($this->isParameterTypeAnArray()) {
31 43
            $parameters = [];
32 43
            foreach ($this->getParameterTypeArrayTypes(true) as $parameterName => $parameterType) {
33 43
                $parameters[] = $this->getMethodParameter($this->getParameterName($parameterName), $parameterType);
34
            }
35 43
            $method->setParameters($parameters);
36
        }
37
38 47
        return $this;
39
    }
40
41 47
    protected function defineParametersFromModel(PhpMethod $method): self
42
    {
43 47
        if ($this->isParameterTypeAModel()) {
44 2
            if ($this->getParameterTypeModel()->getAttributes(true, true)->count() > 0) {
45 2
                $method->setParameters([
46 2
                    $this->getMethodParameter($this->getParameterName($this->getParameterTypeModel()->getPackagedName()), $this->getParameterTypeModel()->getPackagedName(true)),
47
                ]);
48
            }
49
        }
50
51 47
        return $this;
52
    }
53
54 47
    protected function defineParametersFromString(PhpMethod $method): self
55
    {
56 47
        if ($this->isParameterTypeAString() && !$this->isParameterTypeAModel()) {
57 2
            $method->setParameters([
58 2
                $this->getMethodParameter($this->getParameterName($this->getMethod()->getParameterType())),
59
            ]);
60
        }
61
62 47
        return $this;
63
    }
64
65 47
    protected function defineBody(PhpMethod $method): self
66
    {
67 47
        $resultVariableName = sprintf('$result%s', ucfirst($this->getMethod()->getCleanName(false)));
68 47
        $method->addChild('try {')
69 47
            ->addChild($method->getIndentedString(sprintf('$this->setResult(%s = $this->getSoapClient()->%s%s));', $resultVariableName, $this->getSoapCallName(), $this->getOperationCallParameters($method)), 1))
70 47
            ->addChild('')
71 47
            ->addChild($method->getIndentedString(sprintf('return %s;', $resultVariableName), 1))
72 47
            ->addChild('} catch (SoapFault $soapFault) {')
73 47
            ->addChild($method->getIndentedString('$this->saveLastError(__METHOD__, $soapFault);', 1))
74 47
            ->addChild('')
75 47
            ->addChild($method->getIndentedString('return false;', 1))
76 47
            ->addChild('}')
77
        ;
78
79 47
        return $this;
80
    }
81
82 47
    protected function getSoapCallName(): string
83
    {
84 47
        return sprintf('%s(\'%s\'%s', self::SOAP_CALL_NAME, $this->getMethod()->getName(), $this->getOperationCallParametersStarting());
85
    }
86
87 47
    protected function getOperationCallParameters(PhpMethod $method): string
88
    {
89 47
        $parameters = [];
90 47
        foreach ($method->getParameters() as $parameter) {
91 47
            if ($parameter instanceof PhpFunctionParameter) {
92 47
                $parameters[] = $this->getOperationCallParameterName($parameter, $method);
93
            }
94
        }
95
96 47
        return sprintf('%s%s, [], [], $this->outputHeaders', implode('', $parameters), $this->isParameterTypeEmpty() ? '' : PhpMethod::BREAK_LINE_CHAR.']');
97
    }
98
99 47
    protected function getOperationCallParametersStarting(): string
100
    {
101 47
        return $this->isParameterTypeAnArray() ? ', [' : ($this->isParameterTypeEmpty() ? ', ]' : ', [');
102
    }
103
104
    protected function getOperationCallParametersEnding(): string
105
    {
106
        return sprintf('%s]', PhpMethod::BREAK_LINE_CHAR);
107
    }
108
109 47
    protected function getOperationCallParameterName(PhpFunctionParameter $parameter, PhpMethod $method): string
110
    {
111 47
        $cloneParameter = clone $parameter;
112 47
        $cloneParameter->setType(null);
113
114 47
        return sprintf('%s%s', PhpMethod::BREAK_LINE_CHAR, $method->getIndentedString(sprintf('%s,', $cloneParameter->getPhpDeclaration()), 1));
115
    }
116
}
117