AbstractNetAcuityDatabase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fetch() 0 15 2
A handleGuzzleException() 0 14 2
A parseBody() 0 9 1
A buildQuery() 0 5 1
1
<?php
2
3
namespace TraderInteractive\NetAcuity\Databases;
4
5
use TraderInteractive\Util\Arrays;
6
use Exception;
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Psr7\Request;
10
11
/**
12
 * The Abstract NetAcuityDatabase used as a base for each database model.
13
 */
14
abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface
15
{
16
    /**
17
     * @var ClientInterface The GuzzleHttp Client.
18
     */
19
    protected $client;
20
21
    /**
22
     * @var array The translations array for the data set.
23
     */
24
    protected $translations;
25
26
    /**
27
     * @var string The API User Token.
28
     */
29
    protected $apiUserToken;
30
31
    /**
32
     * @var int The Net Acuity Database ID.
33
     */
34
    protected $databaseIdentifier;
35
36
    /**
37
     * AbstractNetAcuityDatabase constructor.
38
     *
39
     * @param ClientInterface $client       The injected GuzzleHttp Client.
40
     * @param string          $apiUserToken The Net Acuity API User Token.
41
     */
42
    public function __construct(
43
        ClientInterface $client,
44
        string $apiUserToken
45
    ) {
46
        $this->client = $client;
47
        $this->apiUserToken = $apiUserToken;
48
    }
49
50
    /**
51
     * @param string $ip The IP to fetch the result data set for.
52
     *
53
     * @return array The formatted data set.
54
     *
55
     * @throws Exception On failure to send a Guzzle request.
56
     */
57
    public function fetch(string $ip)
58
    {
59
        $queryString = $this->buildQuery($this->apiUserToken, $ip);
60
        $request = new Request('GET', $queryString);
61
62
        $body = [];
63
        try {
64
            $response = $this->client->send($request);
65
            $body = json_decode($response->getBody()->getContents(), true);
66
        } catch (ClientException $e) {
67
            $this->handleGuzzleException($e);
68
        }
69
70
        return $this->parseBody($body);
71
    }
72
73
    /**
74
     * @param ClientException $e The thrown exception for handling.
75
     *
76
     * @throws Exception A formatted exception masking the API User Token in the event that it becomes invalid.
77
     *
78
     * @return void
79
     */
80
    protected function handleGuzzleException(ClientException $e)
81
    {
82
        $response = $e->getResponse();
83
        $code = $response->getStatusCode();
84
85
        if ($code === 403) {
86
            throw new Exception('NetAcuity API rejected the provided api user token.', $code);
87
        }
88
89
        $error = json_decode($response->getBody()->getContents(), true);
90
        $reason = Arrays::getNested($error, 'error.message');
91
92
        throw new Exception("NetAcuity API rejected the request, Reason: {$reason}", $code);
93
    }
94
95
    /**
96
     * Parses the response into an array using the field definition.
97
     *
98
     * @param array $response The response from NetAcuity.
99
     *
100
     * @return array The response where the keys are from $fields and the values are from the $response.
101
     */
102
    protected function parseBody(array $response)
103
    {
104
        $responseData = Arrays::get($response, 'response');
105
106
        $result = [];
107
        Arrays::copyIfKeysExist($responseData, $result, $this->translations);
108
109
        return $result;
110
    }
111
112
    /**
113
     * @param string $userToken Net Acuity API User Token.
114
     * @param string $ip        The IP to be referenced in the lookup.
115
     *
116
     * @return string The formatted url query string.
117
     */
118
    protected function buildQuery(string $userToken, string $ip) : string
119
    {
120
        $baseUrl = 'https://usa.cloud.netacuity.com/webservice/query';
121
        return "{$baseUrl}?u={$userToken}&dbs={$this->databaseIdentifier}&ip={$ip}&json=true";
122
    }
123
}
124