1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient; |
3
|
|
|
|
4
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReader; |
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReaderInterface; |
6
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler; |
7
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope\Messages\Fault as Fault11; |
8
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope12\Messages\Fault as Fault12; |
9
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\ClientException; |
10
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\ServerException; |
11
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\SoapException; |
12
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\UnexpectedFormatException; |
13
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreator; |
14
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreatorInterface; |
15
|
|
|
use GoetasWebservices\SoapServices\SoapEnvelope; |
16
|
|
|
use Http\Client\Exception\HttpException; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Message\MessageFactory; |
19
|
|
|
use JMS\Serializer\Serializer; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
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) |
54
|
|
|
{ |
55
|
32 |
|
$this->serviceDefinition = $serviceDefinition; |
56
|
32 |
|
$this->serializer = $serializer; |
57
|
|
|
|
58
|
32 |
|
$this->client = $client; |
59
|
32 |
|
$this->messageFactory = $messageFactory; |
60
|
32 |
|
$this->argumentsReader = new ArgumentsReader($this->serializer, $headerHandler); |
61
|
32 |
|
$this->resultCreator = new ResultCreator($this->serializer, !empty($serviceDefinition['unwrap'])); |
62
|
32 |
|
} |
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) |
115
|
|
|
{ |
116
|
20 |
|
if (strpos($response->getHeaderLine('Content-Type'), 'application/soap+xml') === 0) { |
117
|
11 |
|
return Fault12::class; |
118
|
|
|
} else { |
119
|
9 |
|
return Fault11::class; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
30 |
|
protected function buildHeaders(array $operation) |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
30 |
|
'Content-Type' => isset($operation['version']) && $operation['version'] === '1.2' |
127
|
30 |
|
? 'application/soap+xml; charset=utf-8' |
128
|
30 |
|
: 'text/xml; charset=utf-8', |
129
|
30 |
|
'SoapAction' => '"' . $operation['action'] . '"', |
130
|
30 |
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
30 |
|
protected function findOperation($functionName, array $serviceDefinition) |
134
|
|
|
{ |
135
|
30 |
|
if (isset($serviceDefinition['operations'][$functionName])) { |
136
|
29 |
|
return $serviceDefinition['operations'][$functionName]; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
foreach ($serviceDefinition['operations'] as $opName => $operation) { |
140
|
1 |
|
if (strtolower($functionName) == strtolower($opName)) { |
141
|
1 |
|
return $operation; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
throw new ClientException("Can not find an operation to run $functionName service call"); |
145
|
|
|
} |
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.