Handler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
18
class Handler
19
{
20
    protected RequestFactoryInterface $requestFactory;
21
    private SerializerInterface $serializer;
0 ignored issues
show
Coding Style introduced by
Private member variable "serializer" must be prefixed with an underscore
Loading history...
22
    private RequestHandler $requestHandler;
0 ignored issues
show
Coding Style introduced by
Private member variable "requestHandler" must be prefixed with an underscore
Loading history...
23
    private Config $config;
0 ignored issues
show
Coding Style introduced by
Private member variable "config" must be prefixed with an underscore
Loading history...
24
25
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
27
     * @param RequestHandler $requestHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
28
     * @param RequestFactoryInterface $requestFactory
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
29
     * @param SerializerInterface $serializer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
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
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
47
     * @return Response
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
48
     * @throws Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
49
     * @throws ClientExceptionInterface
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
50
     * @throws SerializerException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
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
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
68
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
69
     * @throws UnavailableException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
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