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
|
|
|
* Transifex API object class. |
15
|
|
|
*/ |
16
|
|
|
abstract class TransifexObject |
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
|
79 |
|
public function __construct( |
61
|
|
|
ClientInterface $client, |
62
|
|
|
RequestFactoryInterface $requestFactory, |
63
|
|
|
StreamFactoryInterface $streamFactory, |
64
|
|
|
UriFactoryInterface $uriFactory, |
65
|
|
|
array $options = [] |
66
|
|
|
) { |
67
|
79 |
|
$this->client = $client; |
68
|
79 |
|
$this->requestFactory = $requestFactory; |
69
|
79 |
|
$this->streamFactory = $streamFactory; |
70
|
79 |
|
$this->uriFactory = $uriFactory; |
71
|
79 |
|
$this->options = $options; |
72
|
79 |
|
} |
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
|
72 |
|
protected function createAuthorizationHeader(): string |
82
|
|
|
{ |
83
|
72 |
|
$username = $this->getOption('api.username'); |
84
|
72 |
|
$password = $this->getOption('api.password'); |
85
|
|
|
|
86
|
|
|
// The API requires HTTP Basic Authentication, we can't proceed without credentials |
87
|
72 |
|
if ($username === null || $password === null) { |
88
|
1 |
|
throw new \InvalidArgumentException('Missing credentials for API authentication.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
71 |
|
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
|
72 |
|
protected function createRequest(string $method, UriInterface $uri): RequestInterface |
105
|
|
|
{ |
106
|
72 |
|
$request = $this->requestFactory->createRequest($method, $uri); |
107
|
|
|
|
108
|
72 |
|
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
|
73 |
|
protected function createUri(string $path): UriInterface |
119
|
|
|
{ |
120
|
73 |
|
$baseUrl = $this->getOption('base_uri', 'https://www.transifex.com'); |
121
|
|
|
|
122
|
73 |
|
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
|
73 |
|
protected function getOption(string $key, $default = null) |
134
|
|
|
{ |
135
|
73 |
|
return $this->options[$key] ?? $default; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Update an API endpoint with resource content. |
140
|
|
|
* |
141
|
|
|
* @param UriInterface $uri URI object representing the API path to request |
142
|
|
|
* @param string $content The content of the resource, this can either be a string of data or a file path |
143
|
|
|
* @param string $type The type of content in the $content variable, this should be either string or file |
144
|
|
|
* |
145
|
|
|
* @return ResponseInterface |
146
|
|
|
* |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
*/ |
149
|
8 |
|
protected function updateResource(UriInterface $uri, string $content, string $type): ResponseInterface |
150
|
|
|
{ |
151
|
|
|
// Verify the content type is allowed |
152
|
8 |
|
if (!\in_array($type, ['string', 'file'])) { |
153
|
1 |
|
throw new \InvalidArgumentException('The content type must be specified as file or string.'); |
154
|
|
|
} |
155
|
|
|
|
156
|
7 |
|
$request = $this->createRequest('PUT', $uri); |
157
|
7 |
|
$request = $request->withHeader('Content-Type', 'application/json'); |
158
|
|
|
|
159
|
7 |
|
if ($type == 'file') { |
160
|
3 |
|
if (!\file_exists($content)) { |
161
|
1 |
|
throw new \InvalidArgumentException( |
162
|
1 |
|
\sprintf('The specified file, "%s", does not exist.', $content) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
$request = $request->withBody($this->streamFactory->createStreamFromFile($content)); |
167
|
|
|
} else { |
168
|
|
|
$data = [ |
169
|
4 |
|
'content' => $content, |
170
|
|
|
]; |
171
|
|
|
|
172
|
4 |
|
$request = $request->withBody($this->streamFactory->createStream(\json_encode($data))); |
173
|
|
|
} |
174
|
|
|
|
175
|
6 |
|
return $this->client->sendRequest($request); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|