Completed
Push — master ( ef15e7...7e484e )
by Michael
02:15 queued 16s
created

Languages::getTranslators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 7
Bugs 0 Features 2
Metric Value
c 7
b 0
f 2
dl 8
loc 8
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * BabDev Transifex Package
5
 *
6
 * (c) Michael Babker <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BabDev\Transifex;
13
14
/**
15
 * Transifex API Languages class.
16
 *
17
 * @link http://docs.transifex.com/developer/api/languages
18
 */
19
class Languages extends TransifexObject
20
{
21
    /**
22
     * Method to create a language for a project.
23
     *
24
     * @param string $slug                The slug for the project
25
     * @param string $langCode            The language code for the new language
26
     * @param array  $coordinators        An array of coordinators for the language
27
     * @param array  $options             Optional additional params to send with the request
28
     * @param bool   $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames
29
     *
30
     * @return \Joomla\Http\Response
31
     *
32
     * @throws \InvalidArgumentException
33
     */
34 3
    public function createLanguage(
35
        $slug,
36
        $langCode,
37
        array $coordinators,
38
        array $options = [],
39
        $skipInvalidUsername = false
40
    ) {
41
        // Make sure the $coordinators array is not empty
42 3
        if (count($coordinators) < 1) {
43 1
            throw new \InvalidArgumentException('The coordinators array must contain at least one username.');
44
        }
45
46
        // Build the request path.
47 2
        $path = '/project/' . $slug . '/languages/';
48
49
        // Check if invalid usernames should be skipped
50 2
        if ($skipInvalidUsername) {
51 1
            $path .= '?skip_invalid_username';
52 1
        }
53
54
        // Build the required request data.
55
        $data = [
56 2
            'language_code' => $langCode,
57 2
            'coordinators'  => $coordinators,
58 2
        ];
59
60
        // Valid options to check
61 2
        $validOptions = ['translators', 'reviewers', 'list'];
62
63
        // Loop through the valid options and if we have them, add them to the request data
64 2
        foreach ($validOptions as $option) {
65 2
            if (isset($options[$option])) {
66 1
                $data[$option] = $options[$option];
67 1
            }
68 2
        }
69
70
        // Send the request.
71 2
        return $this->processResponse(
72 2
            $this->client->post(
73 2
                $this->fetchUrl($path),
74 2
                json_encode($data),
75 2
                ['Content-Type' => 'application/json']
76 2
            ),
77
            201
78 2
        );
79
    }
80
81
    /**
82
     * Method to delete a language within a project.
83
     *
84
     * @param string $project  The project to retrieve details for
85
     * @param string $langCode The language code to retrieve details for
86
     *
87
     * @return \Joomla\Http\Response
88
     */
89 2
    public function deleteLanguage($project, $langCode)
90
    {
91
        // Build the request path.
92 2
        $path = '/project/' . $project . '/language/' . $langCode . '/';
93
94
        // Send the request.
95 2
        return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204);
96
    }
97
98
    /**
99
     * Method to get the coordinators for a language team in a project.
100
     *
101
     * @param string $project  The project to retrieve details for
102
     * @param string $langCode The language code to retrieve details for
103
     *
104
     * @return \Joomla\Http\Response
105
     */
106 2 View Code Duplication
    public function getCoordinators($project, $langCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
107
    {
108
        // Build the request path.
109 2
        $path = '/project/' . $project . '/language/' . $langCode . '/coordinators/';
110
111
        // Send the request.
112 2
        return $this->processResponse($this->client->get($this->fetchUrl($path)));
113
    }
114
115
    /**
116
     * Method to get information about a given language in a project.
117
     *
118
     * @param string $project  The project to retrieve details for
119
     * @param string $langCode The language code to retrieve details for
120
     *
121
     * @return \Joomla\Http\Response
122
     */
123 2 View Code Duplication
    public function getLanguage($project, $langCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
124
    {
125
        // Build the request path.
126 2
        $path = '/project/' . $project . '/language/' . $langCode . '/';
127
128
        // Send the request.
129 2
        return $this->processResponse($this->client->get($this->fetchUrl($path)));
130
    }
131
132
    /**
133
     * Method to get a list of languages for a specified project.
134
     *
135
     * @param string $project The project to retrieve details for
136
     *
137
     * @return \Joomla\Http\Response
138
     */
139 2 View Code Duplication
    public function getLanguages($project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
140
    {
141
        // Build the request path.
142 2
        $path = '/project/' . $project . '/languages/';
143
144
        // Send the request.
145 2
        return $this->processResponse($this->client->get($this->fetchUrl($path)));
146
    }
147
148
    /**
149
     * Method to get the reviewers for a language team in a project.
150
     *
151
     * @param string $project  The project to retrieve details for
152
     * @param string $langCode The language code to retrieve details for
153
     *
154
     * @return \Joomla\Http\Response
155
     */
156 2 View Code Duplication
    public function getReviewers($project, $langCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
157
    {
158
        // Build the request path.
159 2
        $path = '/project/' . $project . '/language/' . $langCode . '/reviewers/';
160
161
        // Send the request.
162 2
        return $this->processResponse($this->client->get($this->fetchUrl($path)));
163
    }
164
165
    /**
166
     * Method to get the translators for a language team in a project.
167
     *
168
     * @param string $project  The project to retrieve details for
169
     * @param string $langCode The language code to retrieve details for
170
     *
171
     * @return \Joomla\Http\Response
172
     */
173 2 View Code Duplication
    public function getTranslators($project, $langCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
174
    {
175
        // Build the request path.
176 2
        $path = '/project/' . $project . '/language/' . $langCode . '/translators/';
177
178
        // Send the request.
179 2
        return $this->processResponse($this->client->get($this->fetchUrl($path)));
180
    }
181
182
    /**
183
     * Method to update the coordinators for a language team in a project.
184
     *
185
     * @param string $project             The project to retrieve details for
186
     * @param string $langCode            The language code to retrieve details for
187
     * @param array  $coordinators        An array of coordinators for the language
188
     * @param bool   $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames
189
     *
190
     * @return \Joomla\Http\Response
191
     */
192 3
    public function updateCoordinators($project, $langCode, array $coordinators, $skipInvalidUsername = false)
193
    {
194 3
        return $this->updateTeam($project, $langCode, $coordinators, $skipInvalidUsername, 'coordinators');
195
    }
196
197
    /**
198
     * Method to update a language within a project.
199
     *
200
     * @param string $slug         The slug for the project
201
     * @param string $langCode     The language code for the new language
202
     * @param array  $coordinators An array of coordinators for the language
203
     * @param array  $options      Optional additional params to send with the request
204
     *
205
     * @return \Joomla\Http\Response
206
     *
207
     * @throws \InvalidArgumentException
208
     */
209 3
    public function updateLanguage($slug, $langCode, array $coordinators, array $options = [])
210
    {
211
        // Make sure the $coordinators array is not empty
212 3
        if (count($coordinators) < 1) {
213 1
            throw new \InvalidArgumentException('The coordinators array must contain at least one username.');
214
        }
215
216
        // Build the request path.
217 2
        $path = '/project/' . $slug . '/language/' . $langCode . '/';
218
219
        // Build the required request data.
220 2
        $data = ['coordinators' => $coordinators];
221
222
        // Set the translators if present
223 2
        if (isset($options['translators'])) {
224 1
            $data['translators'] = $options['translators'];
225 1
        }
226
227
        // Set the reviewers if present
228 2
        if (isset($options['reviewers'])) {
229 1
            $data['reviewers'] = $options['reviewers'];
230 1
        }
231
232
        // Send the request.
233 2
        return $this->processResponse(
234 2
            $this->client->put(
235 2
                $this->fetchUrl($path),
236 2
                json_encode($data),
237 2
                ['Content-Type' => 'application/json']
238 2
            )
239 2
        );
240
    }
241
242
    /**
243
     * Method to update the reviewers for a language team in a project.
244
     *
245
     * @param string $project             The project to retrieve details for
246
     * @param string $langCode            The language code to retrieve details for
247
     * @param array  $reviewers           An array of reviewers for the language
248
     * @param bool   $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames
249
     *
250
     * @return \Joomla\Http\Response
251
     */
252 3
    public function updateReviewers($project, $langCode, array $reviewers, $skipInvalidUsername = false)
253
    {
254 3
        return $this->updateTeam($project, $langCode, $reviewers, $skipInvalidUsername, 'reviewers');
255
    }
256
257
    /**
258
     * Base method to update a given language team in a project.
259
     *
260
     * @param string $project             The project to retrieve details for
261
     * @param string $langCode            The language code to retrieve details for
262
     * @param array  $members             An array of the team members for the language
263
     * @param bool   $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames
264
     * @param string $team                The team to update
265
     *
266
     * @return \Joomla\Http\Response
267
     *
268
     * @throws \InvalidArgumentException
269
     */
270 9
    protected function updateTeam($project, $langCode, array $members, $skipInvalidUsername, $team)
271
    {
272
        // Make sure the $members array is not empty
273 9
        if (count($members) < 1) {
274 3
            throw new \InvalidArgumentException('The ' . $team . ' array must contain at least one username.');
275
        }
276
277
        // Build the request path.
278 6
        $path = '/project/' . $project . '/language/' . $langCode . '/' . $team . '/';
279
280
        // Check if invalid usernames should be skipped
281 6
        if ($skipInvalidUsername) {
282 3
            $path .= '?skip_invalid_username';
283 3
        }
284
285
        // Send the request.
286 6
        return $this->processResponse(
287 6
            $this->client->put(
288 6
                $this->fetchUrl($path),
289 6
                json_encode($members),
290 6
                ['Content-Type' => 'application/json']
291 6
            )
292 6
        );
293
    }
294
295
    /**
296
     * Method to update the translators for a language team in a project.
297
     *
298
     * @param string $project             The project to retrieve details for
299
     * @param string $langCode            The language code to retrieve details for
300
     * @param array  $translators         An array of translators for the language
301
     * @param bool   $skipInvalidUsername If true, the API call does not fail and instead will return a list of invalid usernames
302
     *
303
     * @return \Joomla\Http\Response
304
     */
305 3
    public function updateTranslators($project, $langCode, array $translators, $skipInvalidUsername = false)
306
    {
307 3
        return $this->updateTeam($project, $langCode, $translators, $skipInvalidUsername, 'translators');
308
    }
309
}
310