1 | <?php |
||
22 | class Client |
||
23 | { |
||
24 | /** |
||
25 | * @var Serializer |
||
26 | */ |
||
27 | protected $serializer; |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $serviceDefinition; |
||
32 | /** |
||
33 | * @var HttpClient |
||
34 | */ |
||
35 | protected $client; |
||
36 | |||
37 | /** |
||
38 | * @var MessageFactory |
||
39 | */ |
||
40 | protected $messageFactory; |
||
41 | |||
42 | /** |
||
43 | * @var ResultCreatorInterface |
||
44 | */ |
||
45 | private $resultCreator; |
||
46 | |||
47 | /** |
||
48 | * @var ArgumentsReaderInterface |
||
49 | */ |
||
50 | private $argumentsReader; |
||
51 | |||
52 | |||
53 | 32 | public function __construct(array $serviceDefinition, Serializer $serializer, MessageFactory $messageFactory, HttpClient $client, HeaderHandler $headerHandler) |
|
63 | |||
64 | 30 | public function __call($functionName, array $args) |
|
65 | { |
||
66 | 30 | $soapOperation = $this->findOperation($functionName, $this->serviceDefinition); |
|
67 | 30 | $message = $this->argumentsReader->readArguments($args, $soapOperation['input']); |
|
68 | |||
69 | 30 | $xmlMessage = $this->serializer->serialize($message, 'xml'); |
|
|
|||
70 | 30 | $headers = $this->buildHeaders($soapOperation); |
|
71 | 30 | $request = $this->messageFactory->createRequest('POST', $this->serviceDefinition['endpoint'], $headers, $xmlMessage); |
|
72 | |||
73 | try { |
||
74 | 30 | $response = $this->client->sendRequest($request); |
|
75 | 20 | if (strpos($response->getHeaderLine('Content-Type'), 'xml') === false) { |
|
76 | 2 | throw new UnexpectedFormatException( |
|
77 | 2 | $response, |
|
78 | 2 | $request, |
|
79 | 2 | "Unexpected content type '" . $response->getHeaderLine('Content-Type') . "'" |
|
80 | 2 | ); |
|
81 | } |
||
82 | |||
83 | // fast return if no return is expected |
||
84 | 18 | if (!count($soapOperation['output']['parts'])) { |
|
85 | 4 | return null; |
|
86 | } |
||
87 | |||
88 | 14 | $body = (string)$response->getBody(); |
|
89 | |||
90 | 14 | $faultClass = $this->findFaultClass($response); |
|
91 | |||
92 | 14 | if (strpos($body, ':Fault>') !== false) { // some server returns a fault with 200 OK HTTP |
|
93 | 1 | $fault = $this->serializer->deserialize($body, $faultClass, 'xml'); |
|
94 | 1 | throw $fault->createException($response, $request); |
|
95 | } |
||
96 | |||
97 | 13 | $response = $this->serializer->deserialize($body, $soapOperation['output']['message_fqcn'], 'xml'); |
|
98 | 26 | } catch (HttpException $e) { |
|
99 | 10 | if (strpos($e->getResponse()->getHeaderLine('Content-Type'), 'xml') !== false) { |
|
100 | 6 | $faultClass = $this->findFaultClass($e->getResponse()); |
|
101 | 6 | $fault = $this->serializer->deserialize((string)$e->getResponse()->getBody(), $faultClass, 'xml'); |
|
102 | 6 | throw $fault->createException($e->getResponse(), $e->getRequest(), $e); |
|
103 | 1 | } else { |
|
104 | 4 | throw new ServerException( |
|
105 | 4 | $e->getResponse(), |
|
106 | 4 | $e->getRequest(), |
|
107 | $e |
||
108 | 4 | ); |
|
109 | } |
||
110 | } |
||
111 | 13 | return $this->resultCreator->prepareResult($response, $soapOperation['output']); |
|
112 | } |
||
113 | |||
114 | 20 | public function findFaultClass(ResponseInterface $response) |
|
122 | |||
123 | 30 | protected function buildHeaders(array $operation) |
|
132 | |||
133 | 30 | protected function findOperation($functionName, array $serviceDefinition) |
|
146 | } |
||
147 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.