1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\PhraseApp\Api; |
11
|
|
|
|
12
|
|
|
use FAPI\PhraseApp\Exception; |
13
|
|
|
use FAPI\PhraseApp\Model\Locale\Uploaded; |
14
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Upload extends HttpApi |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Upload a locale. |
24
|
|
|
* |
25
|
|
|
* @param string $projectKey |
26
|
|
|
* @param string $ext |
27
|
|
|
* @param string $filename |
28
|
|
|
* @param array $params |
29
|
|
|
* |
30
|
|
|
* @throws Exception\DomainException |
31
|
|
|
* @throws ClientExceptionInterface |
32
|
|
|
* |
33
|
|
|
* @return Uploaded|ResponseInterface |
34
|
|
|
*/ |
35
|
|
|
public function upload(string $projectKey, string $ext, string $filename, array $params) |
36
|
|
|
{ |
37
|
|
|
if (!file_exists($filename)) { |
38
|
|
|
throw new Exception\InvalidArgumentException('file '.$filename.' not found'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!isset($params['locale_id'])) { |
42
|
|
|
throw new Exception\InvalidArgumentException('locale_id is missing in params'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$postData = [ |
46
|
|
|
['name' => 'file', 'content' => file_get_contents($filename), 'filename' => basename($filename)], |
47
|
|
|
['name' => 'file_format', 'content' => $ext], |
48
|
|
|
['name' => 'locale_id', 'content' => $params['locale_id']], |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
View Code Duplication |
if (isset($params['update_translations'])) { |
|
|
|
|
52
|
|
|
$postData[] = ['name' => 'update_translations', 'content' => $params['update_translations']]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if (isset($params['tags'])) { |
|
|
|
|
56
|
|
|
$postData[] = ['name' => 'tags', 'content' => $params['tags']]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$response = $this->httpPostRaw(sprintf('/api/v2/projects/%s/uploads', $projectKey), $postData, [ |
60
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
if (!$this->hydrator) { |
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
68
|
|
|
$this->handleErrors($response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->hydrator->hydrate($response, Uploaded::class); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.