Completed
Push — master ( 08753a...42665f )
by Sascha-Oliver
05:38
created

Translation::indexKey()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 6
nop 3
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license.  See the LICENSE file for details.
8
 */
9
10
namespace FAPI\PhraseApp\Api;
11
12
use FAPI\PhraseApp\Model\Translation\Index;
13
use FAPI\PhraseApp\Model\Translation\TranslationCreated;
14
use FAPI\PhraseApp\Model\Translation\TranslationUpdated;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @author Sascha-Oliver Prolic <[email protected]>
19
 */
20
class Translation extends HttpApi
21
{
22
    /**
23
     * Index a locale.
24
     *
25
     * @param string $projectKey
26
     * @param string $localeId
27
     * @param array  $params
28
     *
29
     * @return mixed|ResponseInterface
30
     */
31 View Code Duplication
    public function indexLocale(string $projectKey, string $localeId, array $params = [])
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...
32
    {
33
        $response = $this->httpGet(sprintf('/api/v2/projects/%s/locales/%s/translations', $projectKey, $localeId), $params);
34
35
        if (!$this->hydrator) {
36
            return $response;
37
        }
38
39
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
40
            $this->handleErrors($response);
41
        }
42
43
        return $this->hydrator->hydrate($response, Index::class);
44
    }
45
46
    /**
47
     * Create a translation.
48
     *
49
     * @param string $projectKey
50
     * @param string $keyId
51
     * @param string $content
52
     * @param array  $params
53
     *
54
     * @return mixed|ResponseInterface
55
     */
56
    public function create(string $projectKey, string $localeId, string $keyId, string $content, array $params = [])
57
    {
58
        $params['locale_id'] = $localeId;
59
        $params['key_id'] = $keyId;
60
        $params['content'] = $content;
61
62
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/translations', $projectKey), $params);
63
64
        if (!$this->hydrator) {
65
            return $response;
66
        }
67
68
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
69
            $this->handleErrors($response);
70
        }
71
72
        return $this->hydrator->hydrate($response, TranslationCreated::class);
73
    }
74
75
    /**
76
     * Update a translation.
77
     *
78
     * @param string $projectKey
79
     * @param string $translationId
80
     * @param string $content
81
     * @param array  $params
82
     *
83
     * @return mixed|ResponseInterface
84
     */
85 View Code Duplication
    public function update(string $projectKey, string $translationId, string $content, array $params = [])
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...
86
    {
87
        $params['content'] = $content;
88
89
        $response = $this->httpPatch(sprintf('/api/v2/projects/%s/translations/%s', $projectKey, $translationId), $params);
90
91
        if (!$this->hydrator) {
92
            return $response;
93
        }
94
95
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
96
            $this->handleErrors($response);
97
        }
98
99
        return $this->hydrator->hydrate($response, TranslationUpdated::class);
100
    }
101
102
    /**
103
     * List translations for a specific key.
104
     *
105
     * @param string $projectKey
106
     * @param string $keyId
107
     * @param array  $params
108
     *
109
     * @return mixed|ResponseInterface
110
     */
111
    public function indexKey(string $projectKey, string $keyId, array $params = [])
112
    {
113
        if (isset($params['tags'])) {
114
            $params['q'] = 'tags:'.$params['tags'];
115
            unset($params['tags']);
116
        }
117
118
        $response = $this->httpGet(sprintf('/api/v2/projects/%s/keys/%s/translations', $projectKey, $keyId), $params);
119
120
        if (!$this->hydrator) {
121
            return $response;
122
        }
123
124
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
125
            $this->handleErrors($response);
126
        }
127
128
        return $this->hydrator->hydrate($response, Index::class);
129
    }
130
}
131