Completed
Push — fix-checkstyle ( 525678...700890 )
by
unknown
03:06
created

UploadTranslation   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
c 2
b 1
f 0
lcom 1
cbo 5
dl 0
loc 178
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 33 4
A addTranslation() 0 9 2
A getTranslations() 0 4 1
A setImportsAutoApproved() 0 10 2
A areImportsAutoApproved() 0 4 1
A setDuplicatesImported() 0 10 2
A areDuplicatesImported() 0 4 1
A setEqualSuggestionsImported() 0 10 2
A areEqualSuggestionsImported() 0 4 1
A setLocale() 0 6 1
A getLocale() 0 4 1
1
<?php
2
3
namespace Akeneo\Crowdin\Api;
4
5
use \InvalidArgumentException;
6
7
/**
8
 *  Upload existing translations to your Crowdin project.
9
 *
10
 * @author Julien Janvier <[email protected]>
11
 * @see https://crowdin.net/page/api/upload-translation
12
 */
13
class UploadTranslation extends AbstractApi
14
{
15
    /** @var array */
16
    protected $translations;
17
18
    /** @var string */
19
    protected $locale;
20
21
    /** @var bool */
22
    protected $areDuplicatesImported = false;
23
24
    /** @var bool */
25
    protected $areEqualSuggestionsImported = false;
26
27
    /** @var bool */
28
    protected $areImportsAutoApproved = false;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function execute()
34
    {
35
        if (0 === count($this->translations)) {
36
            throw new InvalidArgumentException('There are no translations to upload.');
37
        }
38
39
        if (null === $this->locale) {
40
            throw new InvalidArgumentException('Locale is not set.');
41
        }
42
43
        $path = sprintf(
44
            "project/%s/upload-translation?key=%s",
45
            $this->client->getProjectIdentifier(),
46
            $this->client->getProjectApiKey()
47
        );
48
49
        $data = array_merge($this->parameters, [
50
            'import_duplicates'     => (int)$this->areDuplicatesImported,
51
            'import_eq_suggestions' => (int)$this->areEqualSuggestionsImported,
52
            'auto_approve_imported' => (int)$this->areImportsAutoApproved,
53
            'language'              => $this->locale,
54
        ]);
55
56
        foreach ($this->translations as $crowdinPath => $localFile) {
57
            $data['files['.$crowdinPath.']'] = '@'.$localFile;
58
        }
59
60
61
        $request = $this->client->getHttpClient()->post($path, [], $data);
62
        $response = $request->send();
63
64
        return $response->getBody(true);
65
    }
66
67
    /**
68
     * @param string $crowdinPath the Crowdin file path
69
     * @param string $localPath   the local file path
70
     *
71
     * @throws InvalidArgumentException
72
     *
73
     * @return UploadTranslation
74
     */
75
    public function addTranslation($crowdinPath, $localPath)
76
    {
77
        if (!file_exists($localPath)) {
78
            throw new InvalidArgumentException(sprintf('File %s does not exist.', $localPath));
79
        }
80
        $this->translations[$crowdinPath] = $localPath;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getTranslations()
89
    {
90
        return $this->translations;
91
    }
92
93
    /**
94
     * @param bool $importsAutoApproved
95
     *
96
     * @throws InvalidArgumentException
97
     *
98
     * @return UploadTranslation
99
     */
100
    public function setImportsAutoApproved($importsAutoApproved)
101
    {
102
        if (!is_bool($importsAutoApproved)) {
103
            throw new InvalidArgumentException('A boolean is required.');
104
        }
105
106
        $this->areImportsAutoApproved = $importsAutoApproved;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function areImportsAutoApproved()
115
    {
116
        return $this->areImportsAutoApproved;
117
    }
118
119
    /**
120
     * @param bool $duplicatesImported
121
     *
122
     * @throws InvalidArgumentException
123
     *
124
     * @return UploadTranslation
125
     */
126
    public function setDuplicatesImported($duplicatesImported)
127
    {
128
        if (!is_bool($duplicatesImported)) {
129
            throw new InvalidArgumentException('A boolean is required.');
130
        }
131
132
        $this->areDuplicatesImported = $duplicatesImported;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    public function areDuplicatesImported()
141
    {
142
        return $this->areDuplicatesImported;
143
    }
144
145
    /**
146
     * @param bool $equalSuggestionsImported
147
     *
148
     * @throws InvalidArgumentException
149
     *
150
     * @return UploadTranslation
151
     */
152
    public function setEqualSuggestionsImported($equalSuggestionsImported)
153
    {
154
        if (!is_bool($equalSuggestionsImported)) {
155
            throw new InvalidArgumentException('A boolean is required.');
156
        }
157
158
        $this->areEqualSuggestionsImported = $equalSuggestionsImported;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166
    public function areEqualSuggestionsImported()
167
    {
168
        return $this->areEqualSuggestionsImported;
169
    }
170
171
    /**
172
     * @param string $locale
173
     *
174
     * @return UploadTranslation
175
     */
176
    public function setLocale($locale)
177
    {
178
        $this->locale = $locale;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getLocale()
187
    {
188
        return $this->locale;
189
    }
190
}
191