|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace BabDev\Transifex; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Client\ClientInterface; |
|
6
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
10
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
11
|
|
|
use Psr\Http\Message\UriInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base API connector class. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class ApiConnector |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The HTTP client. |
|
20
|
|
|
* |
|
21
|
|
|
* @var ClientInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The request factory. |
|
27
|
|
|
* |
|
28
|
|
|
* @var RequestFactoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $requestFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The stream factory. |
|
34
|
|
|
* |
|
35
|
|
|
* @var StreamFactoryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $streamFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The URI factory. |
|
41
|
|
|
* |
|
42
|
|
|
* @var UriFactoryInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $uriFactory; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Options for the Transifex object. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $options; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ClientInterface $client The HTTP client |
|
55
|
|
|
* @param RequestFactoryInterface $requestFactory The request factory |
|
56
|
|
|
* @param StreamFactoryInterface $streamFactory The stream factory |
|
57
|
|
|
* @param UriFactoryInterface $uriFactory The URI factory |
|
58
|
|
|
* @param array $options Transifex options array |
|
59
|
|
|
*/ |
|
60
|
92 |
|
public function __construct( |
|
61
|
|
|
ClientInterface $client, |
|
62
|
|
|
RequestFactoryInterface $requestFactory, |
|
63
|
|
|
StreamFactoryInterface $streamFactory, |
|
64
|
|
|
UriFactoryInterface $uriFactory, |
|
65
|
|
|
array $options = [] |
|
66
|
|
|
) { |
|
67
|
92 |
|
$this->client = $client; |
|
68
|
92 |
|
$this->requestFactory = $requestFactory; |
|
69
|
92 |
|
$this->streamFactory = $streamFactory; |
|
70
|
92 |
|
$this->uriFactory = $uriFactory; |
|
71
|
92 |
|
$this->options = $options; |
|
72
|
92 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Creates the Authorization header for the request |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \InvalidArgumentException if credentials are not set |
|
80
|
|
|
*/ |
|
81
|
79 |
|
protected function createAuthorizationHeader(): string |
|
82
|
|
|
{ |
|
83
|
79 |
|
$username = $this->getOption('api.username'); |
|
84
|
79 |
|
$password = $this->getOption('api.password'); |
|
85
|
|
|
|
|
86
|
|
|
// The API requires HTTP Basic Authentication, we can't proceed without credentials |
|
87
|
79 |
|
if ($username === null || $password === null) { |
|
88
|
1 |
|
throw new \InvalidArgumentException('Missing credentials for API authentication.'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
78 |
|
return 'Basic ' . \base64_encode("$username:$password"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Create a Request object for the given URI |
|
96
|
|
|
* |
|
97
|
|
|
* This method will also set the Authorization header for the request |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $method |
|
100
|
|
|
* @param UriInterface $uri |
|
101
|
|
|
* |
|
102
|
|
|
* @return RequestInterface |
|
103
|
|
|
*/ |
|
104
|
79 |
|
protected function createRequest(string $method, UriInterface $uri): RequestInterface |
|
105
|
|
|
{ |
|
106
|
79 |
|
$request = $this->requestFactory->createRequest($method, $uri); |
|
107
|
|
|
|
|
108
|
79 |
|
return $request->withHeader('Authorization', $this->createAuthorizationHeader()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Create a Uri object for the path |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $path |
|
115
|
|
|
* |
|
116
|
|
|
* @return UriInterface |
|
117
|
|
|
*/ |
|
118
|
81 |
|
protected function createUri(string $path): UriInterface |
|
119
|
|
|
{ |
|
120
|
81 |
|
$baseUrl = $this->getOption('base_uri', 'https://www.transifex.com'); |
|
121
|
|
|
|
|
122
|
81 |
|
return $this->uriFactory->createUri($baseUrl . $path); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get an option from the options store. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $key The name of the option to get |
|
129
|
|
|
* @param mixed $default The default value if the option is not set |
|
130
|
|
|
* |
|
131
|
|
|
* @return mixed The option value |
|
132
|
|
|
*/ |
|
133
|
81 |
|
protected function getOption(string $key, $default = null) |
|
134
|
|
|
{ |
|
135
|
81 |
|
return $this->options[$key] ?? $default; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set an option for the Transifex instance. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $key The name of the option to set |
|
142
|
|
|
* @param mixed $value The option value to set |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
6 |
|
protected function setOption(string $key, $value): self |
|
147
|
|
|
{ |
|
148
|
6 |
|
$this->options[$key] = $value; |
|
149
|
|
|
|
|
150
|
6 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Update an API endpoint with resource content. |
|
155
|
|
|
* |
|
156
|
|
|
* @param UriInterface $uri URI object representing the API path to request |
|
157
|
|
|
* @param string $content The content of the resource, this can either be a string of data or a file path |
|
158
|
|
|
* @param string $type The type of content in the $content variable, this should be either string or file |
|
159
|
|
|
* |
|
160
|
|
|
* @return ResponseInterface |
|
161
|
|
|
* |
|
162
|
|
|
* @throws \InvalidArgumentException |
|
163
|
|
|
*/ |
|
164
|
10 |
|
protected function updateResource(UriInterface $uri, string $content, string $type): ResponseInterface |
|
165
|
|
|
{ |
|
166
|
|
|
// Verify the content type is allowed |
|
167
|
10 |
|
if (!\in_array($type, ['string', 'file'])) { |
|
168
|
2 |
|
throw new \InvalidArgumentException('The content type must be specified as file or string.'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
8 |
|
$request = $this->createRequest('PUT', $uri); |
|
172
|
8 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
|
173
|
|
|
|
|
174
|
8 |
|
if ($type == 'file') { |
|
175
|
4 |
|
if (!\file_exists($content)) { |
|
176
|
2 |
|
throw new \InvalidArgumentException( |
|
177
|
2 |
|
\sprintf('The specified file, "%s", does not exist.', $content) |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
2 |
|
$request = $request->withBody($this->streamFactory->createStreamFromFile($content)); |
|
182
|
|
|
} else { |
|
183
|
|
|
$data = [ |
|
184
|
4 |
|
'content' => $content, |
|
185
|
|
|
]; |
|
186
|
|
|
|
|
187
|
4 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
6 |
|
return $this->client->sendRequest($request); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|