1
|
|
|
<?php namespace Atog\Api; |
2
|
|
|
|
3
|
|
|
use Atog\Api\Exceptions\InvalidEndpointException; |
4
|
|
|
use Jyggen\Curl\Request; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Abstract Client |
8
|
|
|
* @package Atog\Api |
9
|
|
|
*/ |
10
|
|
|
abstract class Client |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Endpoints |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $endpoints = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $domain; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A array containing the cached endpoints |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $cachedEndpoints = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new client instance. |
36
|
|
|
* @param array $endpoints |
37
|
|
|
* @param array $config |
38
|
|
|
*/ |
39
|
11 |
|
public function __construct(array $endpoints, array $config = []) |
40
|
|
|
{ |
41
|
11 |
|
$this->setEndpoints($endpoints); |
42
|
11 |
|
$this->config = $config; |
43
|
11 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $endpoints |
47
|
|
|
* @throws \Atog\Api\Exceptions\InvalidEndpointException |
48
|
|
|
*/ |
49
|
11 |
|
protected function setEndpoints(array $endpoints) |
50
|
|
|
{ |
51
|
11 |
|
foreach ($endpoints as $key => $endpoint) { |
52
|
|
|
// check if class exists |
53
|
11 |
|
if (!class_exists($endpoint)) { |
54
|
1 |
|
throw new InvalidEndpointException("Class {$endpoint} does not exists"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// get key |
58
|
11 |
|
if (!is_string($key)) { |
59
|
11 |
|
$parts = explode('\\', $endpoint); |
60
|
11 |
|
$key = end($parts); |
61
|
11 |
|
} |
62
|
|
|
|
63
|
|
|
// save |
64
|
11 |
|
$this->endpoints[studly_case($key)] = $endpoint; |
65
|
11 |
|
} |
66
|
11 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
1 |
|
public function getConfig() |
72
|
|
|
{ |
73
|
1 |
|
return $this->config; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Make a GET Request to an endpoint |
78
|
|
|
* @param $endpoint |
79
|
|
|
* @return \Jyggen\Curl\Response |
80
|
|
|
* @throws \Jyggen\Curl\Exception\CurlErrorException |
81
|
|
|
* @throws \Jyggen\Curl\Exception\ProtectedOptionException |
82
|
|
|
*/ |
83
|
1 |
|
public function get($endpoint) |
84
|
|
|
{ |
85
|
1 |
|
$request = new Request($this->domain . '/' . $endpoint); |
86
|
|
|
|
87
|
|
|
// Add client secret header |
88
|
1 |
|
if (isset($this->config['secret'])) { |
89
|
1 |
|
$request->headers->add(['X-Client-Secret' => $this->config['secret']]); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
// add curl options |
93
|
1 |
|
if (isset($this->config['curl']) && is_array($this->config['curl'])) { |
94
|
1 |
|
foreach ($this->config['curl'] as $option => $value) { |
95
|
1 |
|
$request->setOption($option, $value); |
96
|
1 |
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
$request->execute(); |
100
|
|
|
|
101
|
1 |
|
return $request->getResponse(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get an API endpoint |
106
|
|
|
* @param string $endpoint |
107
|
|
|
* @return \Atog\Api\Endpoint |
108
|
|
|
* @throws \Atog\Api\Exceptions\InvalidEndpointException |
109
|
|
|
*/ |
110
|
4 |
|
public function __get($endpoint) |
111
|
|
|
{ |
112
|
4 |
|
return $this->getEndpoint($endpoint); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get an API endpoint. |
117
|
|
|
* @param string $endpoint |
118
|
|
|
* @return \Atog\Api\Endpoint |
119
|
|
|
* @throws \Atog\Api\Exceptions\InvalidEndpointException |
120
|
|
|
*/ |
121
|
8 |
|
public function getEndpoint($endpoint) |
122
|
|
|
{ |
123
|
|
|
// Get Endpoint Class name |
124
|
8 |
|
$endpoint = studly_case($endpoint); |
125
|
|
|
|
126
|
8 |
|
if (!array_key_exists($endpoint, $this->endpoints)) { |
127
|
2 |
|
throw new InvalidEndpointException("Endpoint {$endpoint} does not exists"); |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
$class = $this->endpoints[$endpoint]; |
131
|
|
|
|
132
|
|
|
// Check if an instance has already been initiated |
133
|
6 |
|
if (isset($this->cachedEndpoints[$endpoint]) === false) { |
134
|
|
|
// check if class is an EndPoint |
135
|
6 |
|
$endpointClass = new \ReflectionClass($class); |
136
|
6 |
|
if (!$endpointClass->isSubclassOf('Atog\Api\Endpoint')) { |
137
|
2 |
|
throw new InvalidEndpointException("Class {$class} does not extend Atog\\Api\\Endpoint"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// check for model |
141
|
4 |
|
$model = new Model(); |
142
|
4 |
|
if (array_key_exists('models', $this->config) && array_key_exists($endpoint, $this->config['models'])) { |
143
|
4 |
|
$modelClass = $this->config['models'][$endpoint]; |
144
|
4 |
|
if (class_exists($modelClass)) { |
145
|
4 |
|
$model = new $modelClass(); |
146
|
4 |
|
} |
147
|
4 |
|
} |
148
|
4 |
|
$this->cachedEndpoints[$endpoint] = new $class($this, $model); |
149
|
4 |
|
} |
150
|
|
|
|
151
|
4 |
|
return $this->cachedEndpoints[$endpoint]; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|