1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\TranslationBundle\Provider; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareTrait; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
use Guzzle\Http\Exception\BadResponseException; |
9
|
|
|
use Guzzle\Http\Client; |
10
|
|
|
use Guzzle\Http\Message\Request; |
11
|
|
|
|
12
|
|
|
abstract class AbstractAPIAdapter implements APIAdapterInterface |
13
|
|
|
{ |
14
|
|
|
use LoggerAwareTrait; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $apiKey; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $projectId; |
21
|
|
|
|
22
|
|
|
/** @var Client */ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
public function __construct(Client $client) |
26
|
|
|
{ |
27
|
|
|
$this->client = $client; |
28
|
|
|
$this->setLogger(new NullLogger()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $apiKey |
33
|
|
|
*/ |
34
|
|
|
public function setApiKey($apiKey) |
35
|
|
|
{ |
36
|
|
|
$this->apiKey = $apiKey; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getApiKey() |
43
|
|
|
{ |
44
|
|
|
return $this->apiKey; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $projectId |
49
|
|
|
*/ |
50
|
|
|
public function setProjectId($projectId) |
51
|
|
|
{ |
52
|
|
|
$this->projectId = $projectId; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Upload source files to translation service |
57
|
|
|
* |
58
|
|
|
* @param string $files file list with translations |
59
|
|
|
* @param string $mode 'update' or 'add' |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
abstract public function upload($files, $mode = 'add'); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Allow adapter to modify request before sending, |
67
|
|
|
* adding API key by default |
68
|
|
|
* |
69
|
|
|
* @param Request $request |
70
|
|
|
*/ |
71
|
|
|
protected function preprocessRequest(Request $request) |
72
|
|
|
{ |
73
|
|
|
$request->getQuery()->add('key', $this->getApiKey()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function request($uri, $data = [], $method = 'GET', $options = [], $headers = []) |
80
|
|
|
{ |
81
|
|
|
$request = $this->client->createRequest( |
82
|
|
|
$method, |
83
|
|
|
$uri, |
84
|
|
|
$headers, |
85
|
|
|
$data, |
86
|
|
|
$options |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
if (!in_array($method, ['POST', 'PUT'], true)) { |
90
|
|
|
$request->getQuery()->merge($data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->preprocessRequest($request); |
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$response = $request->send(); |
96
|
|
|
} catch (BadResponseException $e) { |
97
|
|
|
$response = $e->getResponse(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Extract list of folders recursively from file paths |
105
|
|
|
* |
106
|
|
|
* @param array $files |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function getFileFolders(array $files) |
111
|
|
|
{ |
112
|
|
|
$dirs = []; |
113
|
|
|
|
114
|
|
|
foreach ($files as $remotePath) { |
115
|
|
|
$subFolders = explode(DIRECTORY_SEPARATOR, dirname($remotePath)); |
116
|
|
|
|
117
|
|
|
$currentDir = []; |
118
|
|
|
foreach ($subFolders as $folderName) { |
119
|
|
|
$currentDir[] = $folderName; |
120
|
|
|
|
121
|
|
|
// crowdin understand only "/" as directory separator |
122
|
|
|
$path = implode('/', $currentDir); |
123
|
|
|
$dirs[] = $path; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $dirs; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.