@@ -8,10 +8,10 @@ |
||
8 | 8 | class Widgets extends Endpoint |
9 | 9 | { |
10 | 10 | |
11 | - use BelongsToOrganization, CrudEndpoint; |
|
11 | + use BelongsToOrganization, CrudEndpoint; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $endpoint = 'widgets'; |
|
13 | + /** |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $endpoint = 'widgets'; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -7,10 +7,10 @@ |
||
7 | 7 | |
8 | 8 | class Collections extends Endpoint |
9 | 9 | { |
10 | - use BelongsToOrganization, CrudEndpoint; |
|
10 | + use BelongsToOrganization, CrudEndpoint; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $endpoint = 'collections'; |
|
12 | + /** |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $endpoint = 'collections'; |
|
16 | 16 | } |
17 | 17 | \ No newline at end of file |
@@ -6,93 +6,93 @@ |
||
6 | 6 | |
7 | 7 | class Endpoint |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - protected $allowedMethods = [ |
|
13 | - 'getById', |
|
14 | - 'getAll', |
|
15 | - 'create', |
|
16 | - 'update', |
|
17 | - 'delete', |
|
18 | - ]; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + protected $allowedMethods = [ |
|
13 | + 'getById', |
|
14 | + 'getAll', |
|
15 | + 'create', |
|
16 | + 'update', |
|
17 | + 'delete', |
|
18 | + ]; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $endpoint; |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $endpoint; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $headers = []; |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $headers = []; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var HttpInterface |
|
32 | - */ |
|
33 | - protected $http; |
|
30 | + /** |
|
31 | + * @var HttpInterface |
|
32 | + */ |
|
33 | + protected $http; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param HttpInterface $http |
|
37 | - */ |
|
38 | - public function __construct(HttpInterface $http) |
|
39 | - { |
|
40 | - $this->http = $http; |
|
41 | - } |
|
35 | + /** |
|
36 | + * @param HttpInterface $http |
|
37 | + */ |
|
38 | + public function __construct(HttpInterface $http) |
|
39 | + { |
|
40 | + $this->http = $http; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param string $verb |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function makeRequestUrl($verb = '') |
|
48 | - { |
|
49 | - return "https://favro.com/api/v1/{$this->endpoint}/$verb"; |
|
50 | - } |
|
43 | + /** |
|
44 | + * @param string $verb |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function makeRequestUrl($verb = '') |
|
48 | + { |
|
49 | + return "https://favro.com/api/v1/{$this->endpoint}/$verb"; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param string $method |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - public function isMethodAllowed($method) |
|
57 | - { |
|
58 | - return in_array($method, $this->allowedMethods); |
|
59 | - } |
|
52 | + /** |
|
53 | + * @param string $method |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + public function isMethodAllowed($method) |
|
57 | + { |
|
58 | + return in_array($method, $this->allowedMethods); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @return HttpInterface |
|
63 | - */ |
|
64 | - public function getHttp() |
|
65 | - { |
|
66 | - return $this->http; |
|
67 | - } |
|
61 | + /** |
|
62 | + * @return HttpInterface |
|
63 | + */ |
|
64 | + public function getHttp() |
|
65 | + { |
|
66 | + return $this->http; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @param array $params |
|
71 | - * @return array |
|
72 | - */ |
|
73 | - public function getAll(array $params = []) |
|
74 | - { |
|
75 | - return $this->getHttp()->get( |
|
76 | - $this->makeRequestUrl(), $params, $this->getHeaders() |
|
77 | - ); |
|
78 | - } |
|
69 | + /** |
|
70 | + * @param array $params |
|
71 | + * @return array |
|
72 | + */ |
|
73 | + public function getAll(array $params = []) |
|
74 | + { |
|
75 | + return $this->getHttp()->get( |
|
76 | + $this->makeRequestUrl(), $params, $this->getHeaders() |
|
77 | + ); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @param string $id |
|
82 | - * @return array |
|
83 | - */ |
|
84 | - public function getById($id) |
|
85 | - { |
|
86 | - return $this->getHttp()->get( |
|
87 | - $this->makeRequestUrl($id), [], $this->getHeaders() |
|
88 | - ); |
|
89 | - } |
|
80 | + /** |
|
81 | + * @param string $id |
|
82 | + * @return array |
|
83 | + */ |
|
84 | + public function getById($id) |
|
85 | + { |
|
86 | + return $this->getHttp()->get( |
|
87 | + $this->makeRequestUrl($id), [], $this->getHeaders() |
|
88 | + ); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - protected function getHeaders() |
|
95 | - { |
|
96 | - return $this->headers; |
|
97 | - } |
|
91 | + /** |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + protected function getHeaders() |
|
95 | + { |
|
96 | + return $this->headers; |
|
97 | + } |
|
98 | 98 | } |
99 | 99 | \ No newline at end of file |
@@ -5,48 +5,48 @@ |
||
5 | 5 | trait CrudEndpoint |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * @param array $attributes |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function create(array $attributes) |
|
13 | - { |
|
14 | - return $this |
|
15 | - ->getHttp() |
|
16 | - ->post( |
|
17 | - $this->makeRequestUrl(), |
|
18 | - $attributes, |
|
19 | - $this->getHeaders() |
|
20 | - ); |
|
21 | - } |
|
8 | + /** |
|
9 | + * @param array $attributes |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function create(array $attributes) |
|
13 | + { |
|
14 | + return $this |
|
15 | + ->getHttp() |
|
16 | + ->post( |
|
17 | + $this->makeRequestUrl(), |
|
18 | + $attributes, |
|
19 | + $this->getHeaders() |
|
20 | + ); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param string $itemId |
|
25 | - * @param array $attributes |
|
26 | - * @return mixed |
|
27 | - */ |
|
28 | - public function update($itemId, array $attributes) |
|
29 | - { |
|
30 | - return $this |
|
31 | - ->getHttp() |
|
32 | - ->put( |
|
33 | - $this->makeRequestUrl($itemId), |
|
34 | - $attributes, |
|
35 | - $this->getHeaders() |
|
36 | - ); |
|
37 | - } |
|
23 | + /** |
|
24 | + * @param string $itemId |
|
25 | + * @param array $attributes |
|
26 | + * @return mixed |
|
27 | + */ |
|
28 | + public function update($itemId, array $attributes) |
|
29 | + { |
|
30 | + return $this |
|
31 | + ->getHttp() |
|
32 | + ->put( |
|
33 | + $this->makeRequestUrl($itemId), |
|
34 | + $attributes, |
|
35 | + $this->getHeaders() |
|
36 | + ); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $itemId |
|
41 | - * @return mixed |
|
42 | - */ |
|
43 | - public function delete($itemId) |
|
44 | - { |
|
45 | - return $this |
|
46 | - ->getHttp() |
|
47 | - ->delete( |
|
48 | - $this->makeRequestUrl($itemId), |
|
49 | - $this->getHeaders() |
|
50 | - ); |
|
51 | - } |
|
39 | + /** |
|
40 | + * @param string $itemId |
|
41 | + * @return mixed |
|
42 | + */ |
|
43 | + public function delete($itemId) |
|
44 | + { |
|
45 | + return $this |
|
46 | + ->getHttp() |
|
47 | + ->delete( |
|
48 | + $this->makeRequestUrl($itemId), |
|
49 | + $this->getHeaders() |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | } |
@@ -8,111 +8,111 @@ |
||
8 | 8 | |
9 | 9 | class GuzzleHttpAdapter implements HttpInterface |
10 | 10 | { |
11 | - /** |
|
12 | - * @var ClientInterface |
|
13 | - */ |
|
14 | - protected $client; |
|
15 | - |
|
16 | - /** |
|
17 | - * @param ClientInterface $client |
|
18 | - */ |
|
19 | - public function __construct(ClientInterface $client) |
|
20 | - { |
|
21 | - $this->client = $client; |
|
22 | - } |
|
23 | - |
|
24 | - /** |
|
25 | - * @param string $uri |
|
26 | - * @param array $params |
|
27 | - * @param array $headers |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - public function get($uri, $params = [], $headers = []) |
|
31 | - { |
|
32 | - if (!empty($params)) { |
|
33 | - $uri .= '?' . http_build_query($params); |
|
34 | - } |
|
35 | - |
|
36 | - $response = $this |
|
37 | - ->client |
|
38 | - ->get($uri, ['headers' => $headers]); |
|
39 | - |
|
40 | - return $this->parseResponse($response); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @param string $uri |
|
45 | - * @param array $body |
|
46 | - * @param array $headers |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function post($uri, $body = [], $headers = []) |
|
50 | - { |
|
51 | - $response = $this |
|
52 | - ->client |
|
53 | - ->post( |
|
54 | - $uri, [ |
|
55 | - 'headers' => $headers, |
|
56 | - 'form_params' => $body, |
|
57 | - ] |
|
58 | - ); |
|
59 | - |
|
60 | - return $this->parseResponse($response); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string $uri |
|
65 | - * @param array $body |
|
66 | - * @param array $headers |
|
67 | - * @return mixed |
|
68 | - */ |
|
69 | - public function put($uri, $body = [], $headers = []) |
|
70 | - { |
|
71 | - $response = $this |
|
72 | - ->client |
|
73 | - ->put( |
|
74 | - $uri, [ |
|
75 | - 'headers' => $headers, |
|
76 | - 'form_params' => $body, |
|
77 | - ] |
|
78 | - ); |
|
79 | - |
|
80 | - return $this->parseResponse($response); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param string $uri |
|
85 | - * @param array $headers |
|
86 | - * @return mixed |
|
87 | - */ |
|
88 | - public function delete($uri, $headers = []) |
|
89 | - { |
|
90 | - $response = $this |
|
91 | - ->client |
|
92 | - ->delete($uri, ['headers' => $headers]); |
|
93 | - |
|
94 | - return $this->parseResponse($response); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $url |
|
99 | - * @return $this |
|
100 | - */ |
|
101 | - public function setBaseUrl($url) |
|
102 | - { |
|
103 | - $this->client->setBaseUrl($url); |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param ResponseInterface $response |
|
109 | - * @return array|null |
|
110 | - */ |
|
111 | - protected function parseResponse(ResponseInterface $response) |
|
112 | - { |
|
113 | - $responseContents = $response->getBody()->getContents(); |
|
114 | - |
|
115 | - return json_decode($responseContents, true); |
|
116 | - } |
|
11 | + /** |
|
12 | + * @var ClientInterface |
|
13 | + */ |
|
14 | + protected $client; |
|
15 | + |
|
16 | + /** |
|
17 | + * @param ClientInterface $client |
|
18 | + */ |
|
19 | + public function __construct(ClientInterface $client) |
|
20 | + { |
|
21 | + $this->client = $client; |
|
22 | + } |
|
23 | + |
|
24 | + /** |
|
25 | + * @param string $uri |
|
26 | + * @param array $params |
|
27 | + * @param array $headers |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + public function get($uri, $params = [], $headers = []) |
|
31 | + { |
|
32 | + if (!empty($params)) { |
|
33 | + $uri .= '?' . http_build_query($params); |
|
34 | + } |
|
35 | + |
|
36 | + $response = $this |
|
37 | + ->client |
|
38 | + ->get($uri, ['headers' => $headers]); |
|
39 | + |
|
40 | + return $this->parseResponse($response); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @param string $uri |
|
45 | + * @param array $body |
|
46 | + * @param array $headers |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function post($uri, $body = [], $headers = []) |
|
50 | + { |
|
51 | + $response = $this |
|
52 | + ->client |
|
53 | + ->post( |
|
54 | + $uri, [ |
|
55 | + 'headers' => $headers, |
|
56 | + 'form_params' => $body, |
|
57 | + ] |
|
58 | + ); |
|
59 | + |
|
60 | + return $this->parseResponse($response); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string $uri |
|
65 | + * @param array $body |
|
66 | + * @param array $headers |
|
67 | + * @return mixed |
|
68 | + */ |
|
69 | + public function put($uri, $body = [], $headers = []) |
|
70 | + { |
|
71 | + $response = $this |
|
72 | + ->client |
|
73 | + ->put( |
|
74 | + $uri, [ |
|
75 | + 'headers' => $headers, |
|
76 | + 'form_params' => $body, |
|
77 | + ] |
|
78 | + ); |
|
79 | + |
|
80 | + return $this->parseResponse($response); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param string $uri |
|
85 | + * @param array $headers |
|
86 | + * @return mixed |
|
87 | + */ |
|
88 | + public function delete($uri, $headers = []) |
|
89 | + { |
|
90 | + $response = $this |
|
91 | + ->client |
|
92 | + ->delete($uri, ['headers' => $headers]); |
|
93 | + |
|
94 | + return $this->parseResponse($response); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $url |
|
99 | + * @return $this |
|
100 | + */ |
|
101 | + public function setBaseUrl($url) |
|
102 | + { |
|
103 | + $this->client->setBaseUrl($url); |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param ResponseInterface $response |
|
109 | + * @return array|null |
|
110 | + */ |
|
111 | + protected function parseResponse(ResponseInterface $response) |
|
112 | + { |
|
113 | + $responseContents = $response->getBody()->getContents(); |
|
114 | + |
|
115 | + return json_decode($responseContents, true); |
|
116 | + } |
|
117 | 117 | |
118 | 118 | } |
119 | 119 | \ No newline at end of file |
@@ -25,55 +25,55 @@ |
||
25 | 25 | */ |
26 | 26 | class Api |
27 | 27 | { |
28 | - /** |
|
29 | - * @var EndpointsContainer |
|
30 | - */ |
|
31 | - protected $endpointsContainer; |
|
28 | + /** |
|
29 | + * @var EndpointsContainer |
|
30 | + */ |
|
31 | + protected $endpointsContainer; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $organizationId; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $organizationId; |
|
37 | 37 | |
38 | - public function __construct(EndpointsContainer $endpointsContainer) |
|
39 | - { |
|
40 | - $this->endpointsContainer = $endpointsContainer; |
|
41 | - } |
|
38 | + public function __construct(EndpointsContainer $endpointsContainer) |
|
39 | + { |
|
40 | + $this->endpointsContainer = $endpointsContainer; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Magic method to access different endpoints. |
|
45 | - * |
|
46 | - * @param string $endpoint |
|
47 | - * |
|
48 | - * @return Endpoint |
|
49 | - */ |
|
50 | - public function __get($endpoint) |
|
51 | - { |
|
52 | - $endpoint = $this->endpointsContainer->resolve($endpoint); |
|
43 | + /** |
|
44 | + * Magic method to access different endpoints. |
|
45 | + * |
|
46 | + * @param string $endpoint |
|
47 | + * |
|
48 | + * @return Endpoint |
|
49 | + */ |
|
50 | + public function __get($endpoint) |
|
51 | + { |
|
52 | + $endpoint = $this->endpointsContainer->resolve($endpoint); |
|
53 | 53 | |
54 | - if (method_exists($endpoint, 'setOrganizationId')) { |
|
55 | - $endpoint->setOrganizationId($this->organizationId); |
|
56 | - } |
|
54 | + if (method_exists($endpoint, 'setOrganizationId')) { |
|
55 | + $endpoint->setOrganizationId($this->organizationId); |
|
56 | + } |
|
57 | 57 | |
58 | - return $endpoint; |
|
59 | - } |
|
58 | + return $endpoint; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param int $organizationId |
|
63 | - * @return $this |
|
64 | - */ |
|
65 | - public function setOrganization($organizationId) |
|
66 | - { |
|
67 | - $this->organizationId = $organizationId; |
|
61 | + /** |
|
62 | + * @param int $organizationId |
|
63 | + * @return $this |
|
64 | + */ |
|
65 | + public function setOrganization($organizationId) |
|
66 | + { |
|
67 | + $this->organizationId = $organizationId; |
|
68 | 68 | |
69 | - return $this; |
|
70 | - } |
|
69 | + return $this; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function getOrganizationId() |
|
76 | - { |
|
77 | - return $this->organizationId; |
|
78 | - } |
|
72 | + /** |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function getOrganizationId() |
|
76 | + { |
|
77 | + return $this->organizationId; |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | \ No newline at end of file |
@@ -8,63 +8,63 @@ |
||
8 | 8 | |
9 | 9 | class EndpointsContainer |
10 | 10 | { |
11 | - const ENDPOINTS_NAMESPACE = 'seregazhuk\\Favro\\Api\\Endpoints\\'; |
|
11 | + const ENDPOINTS_NAMESPACE = 'seregazhuk\\Favro\\Api\\Endpoints\\'; |
|
12 | 12 | |
13 | - /* |
|
13 | + /* |
|
14 | 14 | * @var HttpInterface |
15 | 15 | */ |
16 | - protected $http; |
|
16 | + protected $http; |
|
17 | 17 | |
18 | - /* |
|
18 | + /* |
|
19 | 19 | * @var array |
20 | 20 | */ |
21 | - protected $endpoints = []; |
|
21 | + protected $endpoints = []; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param HttpInterface $http |
|
25 | - */ |
|
26 | - public function __construct(HttpInterface $http) |
|
27 | - { |
|
28 | - $this->http = $http; |
|
29 | - } |
|
23 | + /** |
|
24 | + * @param HttpInterface $http |
|
25 | + */ |
|
26 | + public function __construct(HttpInterface $http) |
|
27 | + { |
|
28 | + $this->http = $http; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $endpoint |
|
33 | - * @return Endpoint |
|
34 | - */ |
|
35 | - public function resolve($endpoint) |
|
36 | - { |
|
37 | - $endpoint = strtolower($endpoint); |
|
31 | + /** |
|
32 | + * @param string $endpoint |
|
33 | + * @return Endpoint |
|
34 | + */ |
|
35 | + public function resolve($endpoint) |
|
36 | + { |
|
37 | + $endpoint = strtolower($endpoint); |
|
38 | 38 | |
39 | - // Check if an instance has already been initiated |
|
40 | - if (!isset($this->endpoints[$endpoint])) { |
|
41 | - $this->addProvider($endpoint); |
|
42 | - } |
|
39 | + // Check if an instance has already been initiated |
|
40 | + if (!isset($this->endpoints[$endpoint])) { |
|
41 | + $this->addProvider($endpoint); |
|
42 | + } |
|
43 | 43 | |
44 | - return $this->endpoints[$endpoint]; |
|
45 | - } |
|
44 | + return $this->endpoints[$endpoint]; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param $endpoint |
|
49 | - * @throws BadEndpointException |
|
50 | - */ |
|
51 | - protected function addProvider($endpoint) |
|
52 | - { |
|
53 | - $className = self::ENDPOINTS_NAMESPACE . ucfirst($endpoint); |
|
47 | + /** |
|
48 | + * @param $endpoint |
|
49 | + * @throws BadEndpointException |
|
50 | + */ |
|
51 | + protected function addProvider($endpoint) |
|
52 | + { |
|
53 | + $className = self::ENDPOINTS_NAMESPACE . ucfirst($endpoint); |
|
54 | 54 | |
55 | - if (!class_exists($className)) { |
|
56 | - throw new BadEndpointException("Endpoint $className not found."); |
|
57 | - } |
|
55 | + if (!class_exists($className)) { |
|
56 | + throw new BadEndpointException("Endpoint $className not found."); |
|
57 | + } |
|
58 | 58 | |
59 | - $this->endpoints[$endpoint] = $this->buildEndpoint($className); |
|
60 | - } |
|
59 | + $this->endpoints[$endpoint] = $this->buildEndpoint($className); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param string $className |
|
64 | - * @return Endpoint|object |
|
65 | - */ |
|
66 | - protected function buildEndpoint($className) |
|
67 | - { |
|
68 | - return (new ReflectionClass($className))->newInstanceArgs([$this->http]); |
|
69 | - } |
|
62 | + /** |
|
63 | + * @param string $className |
|
64 | + * @return Endpoint|object |
|
65 | + */ |
|
66 | + protected function buildEndpoint($className) |
|
67 | + { |
|
68 | + return (new ReflectionClass($className))->newInstanceArgs([$this->http]); |
|
69 | + } |
|
70 | 70 | } |
71 | 71 | \ No newline at end of file |