Client::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 8
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 9.4285
1
<?php
2
3
namespace Clarify;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use Clarify\Exceptions\InvalidIntegerArgumentException;
7
8
9
/**
10
 * This is the base class that all of the individual media-related classes extend. At the moment, it simply initializes
11
 *   the connection by setting the user agent and the base URI for the API calls.
12
 *
13
 * Class Client
14
 * @package Clarify
15
 *
16
 * @property mixed  stores      This is the stores subresource of the client.
17
 * @property mixed  products    This is the products subresource of the client.
18
 */
19
class Client
20
{
21
    const USER_AGENT = 'clarify-php';
22
    const VERSION = '2.1.1';
23
24
    protected $baseURI  = 'https://api.clarify.io/v1/';
25
    protected $apiKey   = '';
26
    protected $client   = null;
27
    protected $request  = null;
28
    public $response    = null;
29
    public $statusCode  = null;
30
    public $raw         = null; // This is the raw response body. It's unlikely you'll need it but just in case.
31
    public $detail      = null; // This is the json decoded response. This is probably what you want.
32
33
34
    /**
35
     * @param $key
36
     * @param null $httpClient
37
     * @param string $user_agent    only used if you need a custom/unique user agent
38
     */
39
    public function __construct($key, $httpClient = null, $user_agent = '')
40
    {
41
        $this->apiKey = $key;
42
        $_agent = ('' == $user_agent) ? $this::USER_AGENT : $user_agent;
43
        $this->client = (is_null($httpClient)) ? new GuzzleClient(
44
            ['base_uri' => $this->baseURI, 'headers' => ['User-Agent' => $_agent . '/' . $this::VERSION . '/' . PHP_VERSION ]]
45
        ) : $httpClient;
46
    }
47
48
    /**
49
     * @param $uri
50
     * @param array $options
51
     * @return bool
52
     */
53
    public function post($uri, array $options = array())
54
    {
55
        $this->process('POST', $uri, ['form_params' => $options]);
56
        $this->detail = json_decode($this->raw, true);
57
58
        return $this->isSuccessful();
59
    }
60
61
    /**
62
     * @param $uri
63
     * @param array $options
64
     * @return bool
65
     * @throws Exceptions\InvalidIntegerArgumentException
66
     */
67
    public function put($uri, array $options)
68
    {
69
        $version = isset($options['version']) ? $options['version'] : '1';
70
        if (!is_numeric($version)) {
71
            throw new InvalidIntegerArgumentException();
72
        }
73
74
        unset($options['id']);
75
        $this->process('PUT', $uri, ['form_params' => $options]);
76
        $this->detail = json_decode($this->raw, true);
77
78
        return $this->isSuccessful();
79
    }
80
81
    /**
82
     * @param $uri
83
     * @param array $parameters
84
     * @return array|bool|float|int|string
85
     */
86
    public function get($uri, array $parameters = array())
87
    {
88
        $options = array();
89
        $options['query'] = $parameters;
90
91
        $this->process('GET', $uri, $options);
92
        $this->detail = json_decode($this->raw, true);
93
94
        return $this->detail;
95
    }
96
97
    /**
98
     * This deletes a resource using a full URI.
99
     *
100
     * @param $uri
101
     * @return bool
102
     */
103
    public function delete($uri)
104
    {
105
        return $this->process('DELETE', $uri);
106
    }
107
108
    /**
109
     * @param $method
110
     * @param $uri
111
     * @param array $options
112
     * @return mixed
113
     */
114
    protected function process($method, $uri, $options = array())
115
    {
116
        $options['http_errors'] = 'false';
117
        $options['headers']     = ['Authorization' => 'Bearer ' . $this->apiKey ];
118
119
        $this->response = $this->client->request($method, $uri, $options);
120
        $this->statusCode = $this->response->getStatusCode();
121
        $this->raw = $this->response->getBody();
122
123
        return $this->isSuccessful();
124
    }
125
126
    protected function isSuccessful()
127
    {
128
        $successful = false;
129
130
        if (2 == substr($this->statusCode, 0, 1)) {
131
            $successful = true;
132
        }
133
134
        return $successful;
135
    }
136
}
137