1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
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 | */ |
||
0 ignored issues
–
show
|
|||
18 | class Handler |
||
19 | { |
||
20 | protected RequestFactoryInterface $requestFactory; |
||
21 | private SerializerInterface $serializer; |
||
0 ignored issues
–
show
|
|||
22 | private RequestHandler $requestHandler; |
||
0 ignored issues
–
show
|
|||
23 | private Config $config; |
||
0 ignored issues
–
show
|
|||
24 | |||
25 | /** |
||
0 ignored issues
–
show
|
|||
26 | * @param Config $config |
||
0 ignored issues
–
show
|
|||
27 | * @param RequestHandler $requestHandler |
||
0 ignored issues
–
show
|
|||
28 | * @param RequestFactoryInterface $requestFactory |
||
0 ignored issues
–
show
|
|||
29 | * @param SerializerInterface $serializer |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
47 | * @return Response |
||
0 ignored issues
–
show
|
|||
48 | * @throws Exception |
||
0 ignored issues
–
show
|
|||
49 | * @throws ClientExceptionInterface |
||
0 ignored issues
–
show
|
|||
50 | * @throws SerializerException |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
68 | * @return string |
||
0 ignored issues
–
show
|
|||
69 | * @throws UnavailableException |
||
0 ignored issues
–
show
|
|||
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 |