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); |
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: