Completed
Push — master ( 655f33...f5c016 )
by Carlos
02:12
created

RequestLauncher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 86
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A launchRequest() 0 20 3
A getHttpClient() 0 4 1
1
<?php
2
3
namespace Afonso\Emt;
4
5
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Afonso\Emt\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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