1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Afonso\Emt; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
class RequestLauncher |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The API client ID issued by EMT. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $clientId; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The API passkey issued by EMT. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $passkey; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The Guzzle HTTP client. |
27
|
|
|
* |
28
|
|
|
* @var \GuzzleHttp\Client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new RequestLauncher instance with the provided client ID and |
34
|
|
|
* passkey. |
35
|
|
|
* |
36
|
|
|
* An optional Guzzle HTTP client can be provided as well. |
37
|
|
|
* |
38
|
|
|
* @param string $clientId |
39
|
|
|
* @param string $passkey |
40
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
41
|
|
|
*/ |
42
|
12 |
|
public function __construct($clientId, $passkey, ClientInterface $client = null) |
43
|
|
|
{ |
44
|
12 |
|
$this->clientId = $clientId; |
45
|
12 |
|
$this->passkey = $passkey; |
46
|
|
|
|
47
|
12 |
|
if ($client === null) { |
48
|
3 |
|
$client = new Client(); |
49
|
2 |
|
} |
50
|
12 |
|
$this->client = $client; |
51
|
12 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Authenticate and execute an arbitrary request. |
55
|
|
|
* |
56
|
|
|
* This method adds the relevant authentication parameters to the request |
57
|
|
|
* and decodes the returned response. |
58
|
|
|
* |
59
|
|
|
* @var string $endpoint |
60
|
|
|
* @var array $params |
61
|
|
|
* @return \stdClass |
62
|
|
|
* @throws \RuntimeException |
63
|
|
|
*/ |
64
|
9 |
|
public function launchRequest($endpoint, $params = []) |
65
|
|
|
{ |
66
|
9 |
|
$authParams = ['idClient' => $this->clientId, 'passKey' => $this->passkey]; |
67
|
9 |
|
$response = $this->client->post( |
68
|
6 |
|
$endpoint, |
69
|
9 |
|
['form_params' => array_merge($params, $authParams)] |
70
|
6 |
|
); |
71
|
|
|
|
72
|
9 |
|
$decoded = json_decode((string) $response->getBody()); |
73
|
9 |
|
if ($decoded === null) { |
74
|
3 |
|
throw new RuntimeException('Response payload could not be parsed as JSON'); |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
if (isset($decoded->ReturnCode)) { |
78
|
3 |
|
$returnCode = $decoded->ReturnCode; |
79
|
3 |
|
throw new RuntimeException("Request returned error code '{$returnCode}' ('{$decoded->Description}')"); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
return $decoded; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the HTTP client used by this instance. |
87
|
|
|
* |
88
|
|
|
* @return \GuzzleHttp\ClientInterface |
89
|
|
|
*/ |
90
|
3 |
|
public function getHttpClient() |
91
|
|
|
{ |
92
|
3 |
|
return $this->client; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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: