Completed
Push — master ( 495e06...161604 )
by Welling
02:05
created

BaseClientRemote::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Directus\SDK;
4
5
use GuzzleHttp\Client as HTTPClient;
6
7
abstract class BaseClientRemote
8
{
9
    /**
10
     * Directus Server base endpoint
11
     *
12
     * @var string
13
     */
14
    protected $baseEndpoint;
15
16
    /**
17
     * API Version
18
     *
19
     * @var string
20
     */
21
    protected $apiVersion;
22
23
    /**
24
     * Directus Hosted endpoint format.
25
     *
26
     * @var string
27
     */
28
    protected $hostedBaseEndpointFormat;
29
30
    /**
31
     * Directus Hosted Instance Key
32
     *
33
     * @var int|string
34
     */
35
    protected $instanceKey;
36
37
    /**
38
     * Authentication Token
39
     *
40
     * @var string
41
     */
42
    protected $accessToken;
43
44
    /**
45
     * HTTP Client
46
     *
47
     * @var \GuzzleHttp\Client
48
     */
49
    protected $httpClient;
50
51
    /**
52
     * HTTP Client request timeout
53
     *
54
     * @var int
55
     */
56
    protected $timeout = 60;
57
58
    const TABLE_ENTRIES_ENDPOINT = 'tables/%s/rows';
59
    const TABLE_ENTRY_ENDPOINT = 'tables/%s/rows/%s';
60
    const TABLE_LIST_ENDPOINT = 'tables';
61
    const TABLE_INFORMATION_ENDPOINT = 'tables/%s';
62
    const TABLE_PREFERENCES_ENDPOINT = 'tables/%s/preferences';
63
64
    const COLUMN_LIST_ENDPOINT = 'tables/%s/columns';
65
    const COLUMN_INFORMATION_ENDPOINT = 'tables/%s/columns/%s';
66
67
    const GROUP_LIST_ENDPOINT = 'groups';
68
    const GROUP_INFORMATION_ENDPOINT = 'groups/%s';
69
    const GROUP_PRIVILEGES_ENDPOINT = 'privileges/%s';
70
71
    const FILE_LIST_ENDPOINT = 'files';
72
    const FILE_INFORMATION_ENDPOINT = 'files/%s';
73
74
    const SETTING_LIST_ENDPOINT = 'settings';
75
    const SETTING_COLLECTION_ENDPOINT = 'settings/%s';
76
77
    public function __construct($accessToken, $options = [])
78
    {
79
        $this->accessToken = $accessToken;
80
81
        if (isset($options['base_url'])) {
82
            $this->baseEndpoint = $options['base_url'];
83
        }
84
85
        $instanceKey = isset($options['instance_key']) ? $options['instance_key'] : false;
86
        if ($instanceKey) {
87
            $this->instanceKey = $instanceKey;
88
            $this->baseEndpoint = sprintf($this->hostedBaseEndpointFormat, $instanceKey);
89
        }
90
91
        $this->apiVersion = isset($options['version']) ? $options['version'] : 1;
92
        $this->baseEndpoint = rtrim(rtrim($this->baseEndpoint, '/').'/'.$this->apiVersion, '/').'/';
93
94
        $this->setHTTPClient($this->getDefaultHTTPClient());
95
    }
96
97
    /**
98
     * Get the base endpoint url
99
     *
100
     * @return string
101
     */
102
    public function getBaseEndpoint()
103
    {
104
        return $this->baseEndpoint;
105
    }
106
107
    /**
108
     * Get API Version
109
     *
110
     * @return int|string
111
     */
112
    public function getAPIVersion()
113
    {
114
        return $this->apiVersion;
115
    }
116
117
    /**
118
     * Get the authentication access token
119
     *
120
     * @return string
121
     */
122
    public function getAccessToken()
123
    {
124
        return $this->accessToken;
125
    }
126
127
    /**
128
     * Set a new authentication access token
129
     *
130
     * @param $newAccessToken
131
     */
132
    public function setAccessToken($newAccessToken)
133
    {
134
        $this->accessToken = $newAccessToken;
135
    }
136
137
    /**
138
     * Get the Directus hosted instance key
139
     *
140
     * @return null|string
141
     */
142
    public function getInstanceKey()
143
    {
144
        return $this->instanceKey;
145
    }
146
147
    /**
148
     * Set the HTTP Client
149
     *
150
     * @param HTTPClient $httpClient
151
     */
152
    public function setHTTPClient(HTTPClient $httpClient)
153
    {
154
        $this->httpClient = $httpClient;
155
    }
156
157
    /**
158
     * Get the HTTP Client
159
     *
160
     * @return HTTPClient|null
161
     */
162
    public function getHTTPClient()
163
    {
164
        return $this->httpClient;
165
    }
166
167
    /**
168
     * Get the default HTTP Client
169
     *
170
     * @return HTTPClient
171
     */
172
    public function getDefaultHTTPClient()
173
    {
174
        return new HTTPClient(array('base_url' => $this->baseEndpoint));
175
    }
176
177
    public function performRequest($method, $pathFormat, $variables = [])
178
    {
179
        $request = $this->buildRequest($method, $pathFormat, $variables);
180
        $response = $this->httpClient->send($request);
181
182
        return json_decode($response->getBody()->getContents());
183
    }
184
185
    /**
186
     * Build a request object
187
     *
188
     * @param $method
189
     * @param $pathFormat
190
     * @param $variables
191
     *
192
     * @return \GuzzleHttp\Message\Request
193
     */
194
    public function buildRequest($method, $pathFormat, $variables = [])
195
    {
196
        $request = $this->httpClient->createRequest($method, $this->buildPath($pathFormat, $variables), [
197
            'auth' => [$this->accessToken, '']
198
        ]);
199
200
        return $request;
201
    }
202
203
    /**
204
     * Build a endpoint path based on a format
205
     *
206
     * @param string $pathFormat
207
     * @param array $variables
208
     *
209
     * @return string
210
     */
211
    public function buildPath($pathFormat, $variables = [])
212
    {
213
        return vsprintf(ltrim($pathFormat, '/'), $variables);
214
    }
215
}
216