Passed
Pull Request — master (#2)
by
unknown
01:45
created

Client::Accounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Incapsula;
4
5
use Exception;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Psr7\Request;
8
use Incapsula\Api\AccountsApi;
9
use Incapsula\Api\IntegrationApi;
10
use Incapsula\Api\SitesApi;
11
use Incapsula\Credentials\CredentialProvider;
12
use Incapsula\Credentials\Credentials;
13
14
class Client
15
{
16
    /**
17
     * @var Credentials
18
     */
19
    private $credentials;
20
21
    /**
22
     * @var ClientInterface
23
     */
24
    private $httpClient;
25
26
    /**
27
     * @param array $options
28
     */
29
    public function __construct(array $options = [])
30
    {
31
        $profile = isset($options['profile']) ? $options['profile'] : null;
32
        $credentials = isset($options['credentials']) ? $options['credentials'] : null;
33
        if (null === $credentials) {
34
            $credentials = CredentialProvider::env();
35
            if (null === $credentials) {
36
                $credentials = CredentialProvider::ini($profile);
37
            }
38
        }
39
40
        $this->credentials = $credentials;
41
    }
42
43
    /**
44
     * @return HttpClient
45
     */
46
    public function getHttpClient()
47
    {
48
        if (!$this->httpClient) {
49
            return new HttpClient(['timeout' => 20]);
50
        }
51
52
        return $this->httpClient;
53
    }
54
55
    /**
56
     * @param ClientInterface $client
57
     */
58
    public function setHttpClient(ClientInterface $client)
59
    {
60
        $this->httpClient = $client;
61
    }
62
63
    /**
64
     * @return IntegrationApi
65
     */
66
    public function integration()
67
    {
68
        return new IntegrationApi($this);
69
    }
70
71
    /**
72
     * @return SitesApi
73
     */
74
    public function sites()
75
    {
76
        return new SitesApi($this);
77
    }
78
79
    /**
80
     * @return AccountsApi
81
     */
82
    public function Accounts()
83
    {
84
        return new AccountsApi($this);
85
    }
86
87
    /**
88
     * @param string $uri
89
     * @param array  $params
90
     * @param array  $headers
91
     *
92
     * @throws Exception
93
     *
94
     * @return array
95
     */
96
    public function send($uri, $params = [], $headers = [])
97
    {
98
        // apply credentials to all api calls except integration/ips, which doesn't require them.
99
        if (false === strpos($uri, 'integration/ips')) {
100
            $params = array_merge($params, [
101
                'api_id' => $this->credentials->getApiId(),
102
                'api_key' => $this->credentials->getApiKey(),
103
            ]);
104
        }
105
106
        $request = new Request('POST', $uri, $headers);
107
        $response = $this->getHttpClient()->send($request, ['form_params' => $params]);
108
        $data = json_decode($response->getBody(), true);
109
110
        if (null === $data) {
111
            throw new Exception(sprintf('Could not parse JSON (code: %s)', json_last_error()));
112
        }
113
        if (0 !== $data['res']) {
114
            throw new Exception(sprintf('Bad response: %s (code: %s)', $data['res_message'], $data['res']));
115
        }
116
117
        return $data;
118
    }
119
}
120