Completed
Push — master ( eb5568...bc1b58 )
by Nicolas
9s
created

UploadTranslation::setImportsAutoApproved()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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