|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
4
|
|
|
* To change this template file, choose Tools | Templates |
|
5
|
|
|
* and open the template in the editor. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Description of ApiService |
|
10
|
|
|
* |
|
11
|
|
|
* @author matias |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Columnis\Service; |
|
15
|
|
|
|
|
16
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
17
|
|
|
use GuzzleHttp\Exception\RequestException as GuzzleRequestException; |
|
18
|
|
|
use Columnis\Model\ApiResponse; |
|
19
|
|
|
use Columnis\Exception\Api\ApiRequestException; |
|
20
|
|
|
use Columnis\Exception\Api\UnauthorizedException; |
|
21
|
|
|
|
|
22
|
|
|
class ApiService |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Guzzle Client |
|
26
|
|
|
* @var \GuzzleHttp\Client $httpClient |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $httpClient; |
|
29
|
|
|
|
|
30
|
|
|
/* |
|
31
|
|
|
* Columnis Api Client Number |
|
32
|
|
|
* @var string $clientNumber |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $clientNumber; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the Guzzle Client |
|
38
|
|
|
* @return \GuzzleHttp\Client |
|
39
|
7 |
|
*/ |
|
40
|
7 |
|
public function getHttpClient() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->httpClient; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets the Guzzle Client |
|
47
|
8 |
|
* @param \GuzzleHttp\Client $httpClient |
|
48
|
8 |
|
*/ |
|
49
|
8 |
|
public function setHttpClient(GuzzleClient $httpClient) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->httpClient = $httpClient; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
8 |
|
* Returns the Client Number of Columnis Api |
|
56
|
8 |
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getClientNumber() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->clientNumber; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
8 |
|
/** |
|
64
|
8 |
|
* Sets the Client Number of Columnis Api |
|
65
|
8 |
|
* @param string $clientNumber |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function setClientNumber($clientNumber) |
|
68
|
7 |
|
{ |
|
69
|
7 |
|
$this->clientNumber = $clientNumber; |
|
70
|
7 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function __construct(GuzzleClient $httpClient, $clientNumber) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->setHttpClient($httpClient); |
|
75
|
|
|
$this->setClientNumber($clientNumber); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Performs a request to Columnis api |
|
80
|
|
|
* |
|
81
|
6 |
|
* @param string $uri |
|
82
|
|
|
* @param Array $queryParams |
|
|
|
|
|
|
83
|
|
|
* @param Array $params |
|
|
|
|
|
|
84
|
6 |
|
* @return ApiResponse |
|
85
|
6 |
|
* @trows ApiRequestException |
|
86
|
6 |
|
*/ |
|
87
|
6 |
|
public function request($uri, $method = 'GET', Array $options = null) |
|
88
|
|
|
{ |
|
89
|
6 |
|
$mergedOptions = $this->mergeWithDefault($options); |
|
90
|
|
|
|
|
91
|
6 |
|
try { |
|
92
|
6 |
|
$httpClient = $this->getHttpClient(); |
|
93
|
6 |
|
$request = $httpClient->createRequest($method, $uri, $mergedOptions); |
|
94
|
4 |
|
$response = $httpClient->send($request); |
|
95
|
6 |
|
$apiResponse = new ApiResponse($response); |
|
|
|
|
|
|
96
|
|
|
} catch (GuzzleRequestException $e) { |
|
97
|
2 |
|
throw $this->createException($e); |
|
98
|
|
|
} |
|
99
|
4 |
|
return $apiResponse; |
|
100
|
|
|
} |
|
101
|
|
|
protected function mergeWithDefault(Array $options = null) { |
|
102
|
|
|
$defaults = array( |
|
103
|
|
|
'headers' => array( |
|
104
|
|
|
'Accept' => 'application/json', |
|
105
|
|
|
'Content-Type' => 'application/json' |
|
106
|
|
|
) |
|
107
|
7 |
|
); |
|
108
|
7 |
|
return array_replace_recursive($defaults, (is_array($options) ? $options : array())); |
|
109
|
|
|
} |
|
110
|
|
|
public function buildOptions(Array $params = null, Array $queryString = null, Array $headers = null) { |
|
111
|
|
|
return array( |
|
112
|
|
|
'headers' => $headers, |
|
113
|
|
|
'query' => $queryString, |
|
114
|
|
|
'body' => $params |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
protected function createException(GuzzleRequestException $e) { |
|
118
|
|
|
$statusCode = $e->getResponse()->getStatusCode(); |
|
119
|
|
|
switch($statusCode) { |
|
120
|
|
|
case 401: |
|
121
|
|
|
$authInfo = $this->parseAuthHeader($e->getResponse()->getHeader('www-authenticate')); |
|
122
|
|
|
$code = array_key_exists('error', $authInfo) ? $authInfo['error'] : 401; |
|
123
|
|
|
$message = array_key_exists('error_description', $authInfo) ? $authInfo['error_description'] : $e->getMessage(); |
|
124
|
|
|
return new UnauthorizedException($message, $code, $e); |
|
125
|
|
|
default: |
|
126
|
|
|
return new ApiRequestException('Api Request failed: '.$e->getMessage(), 0, $e); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
protected function parseAuthHeader($header) { |
|
130
|
|
|
$matches = array(); |
|
131
|
|
|
$pattern = '/(?:Bearer |, )(\w+)="((?:[^\\"]+|\\.)*)"/'; |
|
132
|
|
|
preg_match_all($pattern, $header, $matches); |
|
133
|
|
|
return array_combine($matches[1], $matches[2]); |
|
134
|
|
|
} |
|
135
|
|
|
/** |
|
136
|
|
|
* Gets the Uri for the desire enpoint |
|
137
|
|
|
* @param string $endpoint |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getUri($endpoint) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->getClientNumber().'/columnis'.$endpoint; |
|
143
|
|
|
} |
|
144
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.