|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace DMT\Insolvency\Soap; |
|
4
|
|
|
|
|
5
|
|
|
use DMT\Http\Client\RequestHandler; |
|
6
|
|
|
use DMT\Insolvency\Config; |
|
7
|
|
|
use DMT\Insolvency\Exception\Exception; |
|
8
|
|
|
use DMT\Insolvency\Exception\UnavailableException; |
|
9
|
|
|
use GuzzleHttp\Psr7\Request as HttpRequest; |
|
10
|
|
|
use JMS\Serializer\SerializerInterface; |
|
11
|
|
|
use JMS\Serializer\Exception\Exception as SerializerException; |
|
12
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
|
13
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Handler |
|
17
|
|
|
*/ |
|
|
|
|
|
|
18
|
|
|
class Handler |
|
19
|
|
|
{ |
|
20
|
|
|
protected RequestFactoryInterface $requestFactory; |
|
21
|
|
|
private SerializerInterface $serializer; |
|
|
|
|
|
|
22
|
|
|
private RequestHandler $requestHandler; |
|
|
|
|
|
|
23
|
|
|
private Config $config; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
|
|
|
|
|
26
|
|
|
* @param Config $config |
|
|
|
|
|
|
27
|
|
|
* @param RequestHandler $requestHandler |
|
|
|
|
|
|
28
|
|
|
* @param RequestFactoryInterface $requestFactory |
|
|
|
|
|
|
29
|
|
|
* @param SerializerInterface $serializer |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
11 |
|
public function __construct( |
|
32
|
|
|
Config $config, |
|
33
|
|
|
RequestHandler $requestHandler, |
|
34
|
|
|
RequestFactoryInterface $requestFactory, |
|
35
|
|
|
SerializerInterface $serializer |
|
36
|
|
|
) { |
|
37
|
11 |
|
$this->config = $config; |
|
38
|
11 |
|
$this->requestHandler = $requestHandler; |
|
39
|
11 |
|
$this->requestFactory = $requestFactory; |
|
40
|
11 |
|
$this->serializer = $serializer; |
|
41
|
11 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Handle the soap request. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Request $request |
|
|
|
|
|
|
47
|
|
|
* @return Response |
|
|
|
|
|
|
48
|
|
|
* @throws Exception |
|
|
|
|
|
|
49
|
|
|
* @throws ClientExceptionInterface |
|
|
|
|
|
|
50
|
|
|
* @throws SerializerException |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
11 |
|
public function handle(Request $request): Response |
|
53
|
|
|
{ |
|
54
|
11 |
|
$responseClass = $this->getResponseClassForRequest(get_class($request)); |
|
55
|
|
|
|
|
56
|
11 |
|
$httpRequest = $this->requestFactory->createRequest('POST', $this->config->endPoint); |
|
57
|
11 |
|
$httpRequest->getBody()->write($this->serializer->serialize($request, 'soap')); |
|
58
|
|
|
|
|
59
|
11 |
|
$httpResponse = $this->requestHandler->handle($httpRequest); |
|
60
|
|
|
|
|
61
|
11 |
|
return $this->serializer->deserialize($httpResponse->getBody()->getContents(), $responseClass, 'soap'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Map the soap request class to the right response class. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $request |
|
|
|
|
|
|
68
|
|
|
* @return string |
|
|
|
|
|
|
69
|
|
|
* @throws UnavailableException |
|
|
|
|
|
|
70
|
|
|
*/ |
|
71
|
11 |
|
protected function getResponseClassForRequest(string $request): string |
|
72
|
|
|
{ |
|
73
|
11 |
|
$response = Response::class . '\\' . preg_replace('~^.*\\\\([^\\\\]+)$~', '$1', $request) . 'Response'; |
|
74
|
|
|
|
|
75
|
11 |
|
return $response; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|