Passed
Push — feature/issue-124 ( 167236...6b1eff )
by Mikaël
07:01
created

Operation::getMainMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
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
        $method
69 47
            ->addChild('try {')
70 47
            ->addChild($method->getIndentedString(sprintf('$this->setResult(%s = $this->getSoapClient()->%s%s));', $resultVariableName, $this->getSoapCallName(), $this->getOperationCallParameters($method)), 1))
71 47
            ->addChild('')
72 47
            ->addChild($method->getIndentedString(sprintf('return %s;', $resultVariableName), 1))
73 47
            ->addChild('} catch (SoapFault $soapFault) {')
74 47
            ->addChild($method->getIndentedString('$this->saveLastError(__METHOD__, $soapFault);', 1))
75 47
            ->addChild('')
76 47
            ->addChild($method->getIndentedString('return false;', 1))
77 47
            ->addChild('}')
78
        ;
79
80 47
        return $this;
81
    }
82
83 47
    protected function getSoapCallName(): string
84
    {
85 47
        return sprintf('%s(\'%s\'%s', self::SOAP_CALL_NAME, $this->getMethod()->getName(), $this->getOperationCallParametersStarting());
86
    }
87
88 47
    protected function getOperationCallParameters(PhpMethod $method): string
89
    {
90 47
        $parameters = [];
91 47
        foreach ($method->getParameters() as $parameter) {
92 47
            if (!$parameter instanceof PhpFunctionParameter) {
93
                continue;
94
            }
95
96 47
            $parameters[] = $this->getOperationCallParameterName($parameter, $method);
97
        }
98
99 47
        return sprintf('%s%s, [], [], $this->outputHeaders', implode('', $parameters), $this->isParameterTypeEmpty() ? '' : PhpMethod::BREAK_LINE_CHAR.']');
100
    }
101
102 47
    protected function getOperationCallParametersStarting(): string
103
    {
104 47
        return $this->isParameterTypeAnArray() ? ', [' : ($this->isParameterTypeEmpty() ? ', ]' : ', [');
105
    }
106
107
    protected function getOperationCallParametersEnding(): string
108
    {
109
        return sprintf('%s]', PhpMethod::BREAK_LINE_CHAR);
110
    }
111
112 47
    protected function getOperationCallParameterName(PhpFunctionParameter $parameter, PhpMethod $method): string
113
    {
114 47
        $cloneParameter = clone $parameter;
115 47
        $cloneParameter->setType(null);
116
117 47
        return sprintf('%s%s', PhpMethod::BREAK_LINE_CHAR, $method->getIndentedString(sprintf('%s,', $cloneParameter->getPhpDeclaration()), 1));
118
    }
119
}
120