|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stp\SndApi\Common; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
use GuzzleHttp\Message\RequestInterface; |
|
8
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Psr\Log\NullLogger; |
|
12
|
|
|
use Stp\SndApi\Common\Exception\InvalidPublicationIdException; |
|
13
|
|
|
use Stp\SndApi\Common\Validator\PublicationIdValidator; |
|
14
|
|
|
|
|
15
|
|
|
abstract class Client implements LoggerAwareInterface |
|
16
|
|
|
{ |
|
17
|
|
|
const BASE_URL = 'http://api.snd.no'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var GuzzleHttpClient |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $client; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $apiKey; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $apiSecret; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $publicationId; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $apiUrl = self::BASE_URL; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var LoggerInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $logger; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $apiKey |
|
51
|
|
|
* @param string $apiSecret |
|
52
|
|
|
* @param string $publicationId |
|
53
|
|
|
* @param string $apiUrl |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($apiKey, $apiSecret, $publicationId, $apiUrl = self::BASE_URL) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->client = new GuzzleHttpClient(['base_url' => $apiUrl]); |
|
58
|
|
|
$this->apiUrl = $apiUrl; |
|
59
|
|
|
$this->setApiKey($apiKey); |
|
60
|
|
|
$this->setApiSecret($apiSecret); |
|
61
|
|
|
$this->setPublicationId($publicationId); |
|
62
|
|
|
$this->setLogger(new NullLogger()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param RequestInterface $request |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function signRequest(RequestInterface $request) |
|
69
|
|
|
{ |
|
70
|
|
|
$now = new \DateTime('now', new \DateTimeZone('UTC')); |
|
71
|
|
|
$signature = sprintf('0x%s', hash_hmac('sha256', $now->format('d M Y H'), $this->getApiSecret())); |
|
72
|
|
|
|
|
73
|
|
|
$request->addHeader('X-Snd-ApiSignature', $signature); |
|
74
|
|
|
|
|
75
|
|
|
if (!empty($this->getApiKey())) { |
|
76
|
|
|
$request->addHeader('X-Snd-ApiKey', $this->getApiKey()); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param RequestInterface $request |
|
82
|
|
|
* @param bool $acceptJsonResponse |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function buildRequest(RequestInterface $request, $acceptJsonResponse = true) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($acceptJsonResponse) { |
|
87
|
|
|
$request->addHeader('Accept', 'application/json'); |
|
88
|
|
|
} |
|
89
|
|
|
$request->addHeader('Accept-Charset', 'UTF-8'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $url |
|
94
|
|
|
* @param bool $acceptJsonResponse |
|
95
|
|
|
* |
|
96
|
|
|
* @return ResponseInterface |
|
97
|
|
|
* @throws RequestException |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function apiGet($url, $acceptJsonResponse = true) |
|
100
|
|
|
{ |
|
101
|
|
|
if (0 === preg_match('/^\//', $url)) { |
|
102
|
|
|
$url = '/' . $url; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$url = str_replace('{publicationId}', $this->getPublicationId(), $url); |
|
106
|
|
|
|
|
107
|
|
|
$request = $this->client->createRequest('GET', $this->getApiUrl() . $url); |
|
108
|
|
|
$this->buildRequest($request, $acceptJsonResponse); |
|
109
|
|
|
$this->signRequest($request); |
|
110
|
|
|
|
|
111
|
|
|
return $this->client->send($request); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getApiKey() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->apiKey; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $apiKey |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setApiKey($apiKey) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->apiKey = $apiKey; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getApiSecret() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->apiSecret; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $apiSecret |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setApiSecret($apiSecret) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->apiSecret = $apiSecret; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getApiUrl() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->apiUrl; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getPublicationId() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->publicationId; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string $publicationId |
|
164
|
|
|
* |
|
165
|
|
|
* @throws InvalidPublicationIdException |
|
166
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setPublicationId($publicationId) |
|
169
|
|
|
{ |
|
170
|
|
|
$publicationIdValidator = new PublicationIdValidator($publicationId); |
|
171
|
|
|
|
|
172
|
|
|
if (!$publicationIdValidator->isValid()) { |
|
173
|
|
|
throw new InvalidPublicationIdException; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$this->publicationId = $publicationId; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Sets a logger instance on the object |
|
181
|
|
|
* |
|
182
|
|
|
* @param LoggerInterface $logger |
|
183
|
|
|
* @return null |
|
184
|
|
|
*/ |
|
185
|
|
|
public function setLogger(LoggerInterface $logger) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->logger = $logger; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param GuzzleHttpClient $client |
|
192
|
|
|
*/ |
|
193
|
|
|
public function setClient(GuzzleHttpClient $client) |
|
194
|
|
|
{ |
|
195
|
|
|
$this->client = $client; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|