Completed
Push — master ( a48167...1d59e1 )
by Mikaël
100:27 queued 66:25
created

Operation::getOperationCallParameters()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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