Passed
Push — mikaelcom-patch-1 ( 393819...c1a5da )
by Mikaël
45:21
created

Operation::defineParametersFromString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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