Issues (5)

src/ClientHandler.php (1 issue)

1
<?php
2
3
namespace DMT\WebservicesNl\Client;
4
5
use DMT\WebservicesNl\Client\Request\LoginRequest;
6
use DMT\WebservicesNl\Client\Request\LogoutRequest;
7
use DMT\WebservicesNl\Client\Request\RequestInterface;
8
use DMT\WebservicesNl\Client\Response\LoginResponse;
9
use DMT\WebservicesNl\Client\Response\ResponseInterface;
10
use GuzzleHttp\Client;
11
use JMS\Serializer\SerializationContext;
12
use JMS\Serializer\Serializer;
13
14
/**
15
 * Class AbstractHandler
16
 *
17
 * @package DMT\WebservicesNl\Client
18
 */
19
class ClientHandler
20
{
21
    /**
22
     * @var Client
23
     */
24
    protected $httpClient;
25
26
    /**
27
     * @var Serializer
28
     */
29
    protected $serializer;
30
31
    /**
32
     * @var string
33
     */
34
    protected $serializerFormat;
35
36
    /**
37
     * @var string
38
     */
39
    protected $deserializerFormat;
40
41
    /**
42
     * DutchBusinessHandler constructor.
43
     *
44
     * @param Client $httpClient
45
     * @param Serializer $serializer
46
     * @param string $serializerFormat
47
     * @param string|null $deserializerFormat
48
     */
49 3
    public function __construct(
50
        Client $httpClient,
51
        Serializer $serializer,
52
        string $serializerFormat,
53
        string $deserializerFormat = null
54
    ) {
55 3
        $this->httpClient = $httpClient;
56 3
        $this->serializer = $serializer;
57 3
        $this->serializerFormat = $serializerFormat;
58 3
        $this->deserializerFormat = $deserializerFormat ?? $serializerFormat;
59 3
    }
60
61
    /**
62
     * @param LoginRequest|RequestInterface $request
63
     *
64
     * @return LoginResponse|ResponseInterface
65
     */
66 1
    public function login(LoginRequest $request): LoginResponse
67
    {
68 1
        return $this->process($request, LoginResponse::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($r...e\LoginResponse::class) returns the type null which is incompatible with the type-hinted return DMT\WebservicesNl\Client\Response\LoginResponse.
Loading history...
69
    }
70
71
    /**
72
     * @param LogoutRequest|RequestInterface $request
73
     *
74
     * @return null
75
     */
76 1
    public function logout(LogoutRequest $request)
77
    {
78 1
        return $this->process($request);
79
    }
80
81
    /**
82
     * @param RequestInterface $request
83
     * @param string $responseClassName
84
     *
85
     * @return ResponseInterface
86
     */
87 2
    protected function process(RequestInterface $request, string $responseClassName = null): ?ResponseInterface
88
    {
89 2
        $context = SerializationContext::create();
90
91 2
        $request = $this->serializer->serialize($request, $this->serializerFormat, $context);
92
93 2
        $response = $this->httpClient->post('', ['body' => $request]);
94
95 2
        if (!$responseClassName) {
96 1
            return null;
97
        }
98
99 1
        return $this->serializer->deserialize($response->getBody(), $responseClassName, $this->deserializerFormat);
100
    }
101
}
102