1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akeneo\Crowdin\Api; |
4
|
|
|
|
5
|
|
|
use \InvalidArgumentException; |
6
|
|
|
use Akeneo\Crowdin\Client; |
7
|
|
|
use Akeneo\Crowdin\FileReader; |
8
|
|
|
use Akeneo\Crowdin\Translation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Upload latest version of your localization file to Crowdin. |
12
|
|
|
* |
13
|
|
|
* @author Nicolas Dupont <[email protected]> |
14
|
|
|
* @see http://crowdin.net/page/api/update-file |
15
|
|
|
*/ |
16
|
|
|
class UpdateFile extends AbstractApi |
17
|
|
|
{ |
18
|
|
|
/** @var FileReader */ |
19
|
|
|
protected $fileReader; |
20
|
|
|
|
21
|
|
|
/** @var Translation[] */ |
22
|
|
|
protected $translations; |
23
|
|
|
|
24
|
|
|
/** @var string|null */ |
25
|
|
|
protected $branch; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Client $client |
29
|
|
|
* @param FileReader $fileReader |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Client $client, FileReader $fileReader) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($client); |
34
|
|
|
$this->fileReader = $fileReader; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function execute() |
41
|
|
|
{ |
42
|
|
|
if (count($this->translations) === 0) { |
43
|
|
|
throw new InvalidArgumentException('There is no files to update'); |
44
|
|
|
} |
45
|
|
|
$path = sprintf( |
46
|
|
|
"project/%s/update-file?key=%s", |
47
|
|
|
$this->client->getProjectIdentifier(), |
48
|
|
|
$this->client->getProjectApiKey() |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$data = $this->parameters; |
52
|
|
View Code Duplication |
if (null !== $this->branch) { |
|
|
|
|
53
|
|
|
$data[] = [ |
54
|
|
|
'name' => 'branch', |
55
|
|
|
'contents' => $this->branch |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
foreach ($this->translations as $translation) { |
|
|
|
|
60
|
|
|
$data[] = [ |
61
|
|
|
'name' => 'files['.$translation->getCrowdinPath().']', |
62
|
|
|
'contents' => $this->fileReader->readTranslation($translation) |
63
|
|
|
]; |
64
|
|
|
if ($translation->getTitle()) { |
65
|
|
|
$data[] = [ |
66
|
|
|
'name' => 'titles['.$translation->getCrowdinPath().']', |
67
|
|
|
'contents' => $translation->getTitle() |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
if ($translation->getExportPattern()) { |
71
|
|
|
$data[] = [ |
72
|
|
|
'name' => 'export_patterns['.$translation->getCrowdinPath().']', |
73
|
|
|
'contents' => $translation->getExportPattern() |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$data = ['multipart' => $data]; |
79
|
|
|
$response = $this->client->getHttpClient()->post($path, $data); |
80
|
|
|
|
81
|
|
|
return $response->getBody(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $localPath |
86
|
|
|
* @param string $crowdinPath |
87
|
|
|
* @param string $exportPattern |
88
|
|
|
* @param string $title |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$translation = new Translation($localPath, $crowdinPath); |
95
|
|
|
$translation->setExportPattern($exportPattern); |
96
|
|
|
$translation->setTitle($title); |
97
|
|
|
|
98
|
|
|
$this->translations[] = $translation; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Translation[] |
105
|
|
|
*/ |
106
|
|
|
public function getTranslations() |
107
|
|
|
{ |
108
|
|
|
return $this->translations; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return null|string |
113
|
|
|
*/ |
114
|
|
|
public function getBranch() |
115
|
|
|
{ |
116
|
|
|
return $this->branch; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $branch |
121
|
|
|
*/ |
122
|
|
|
public function setBranch($branch) |
123
|
|
|
{ |
124
|
|
|
$this->branch = $branch; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.