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