Translation::create()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 7
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 3
nop 5
crap 20
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\Exception\DomainException;
13
use FAPI\PhraseApp\Model\Translation\Index;
14
use FAPI\PhraseApp\Model\Translation\TranslationCreated;
15
use FAPI\PhraseApp\Model\Translation\TranslationUpdated;
16
use Psr\Http\Client\ClientExceptionInterface;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * @author Sascha-Oliver Prolic <[email protected]>
21
 */
22
class Translation extends HttpApi
23
{
24
    /**
25
     * Index a locale.
26
     *
27
     * @param string $projectKey
28
     * @param string $localeId
29
     * @param array  $params
30
     *
31
     * @throws ClientExceptionInterface
32
     * @throws DomainException
33
     *
34
     * @return Index|ResponseInterface
35
     */
36 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...
37
    {
38
        if (isset($params['tags'])) {
39
            $params['q'] = 'tags:'.$params['tags'];
40
            unset($params['tags']);
41
        }
42
43
        $response = $this->httpGet(sprintf('/api/v2/projects/%s/locales/%s/translations', $projectKey, $localeId), $params);
44
45
        if (!$this->hydrator) {
46
            return $response;
47
        }
48
49
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
50
            $this->handleErrors($response);
51
        }
52
53
        return $this->hydrator->hydrate($response, Index::class);
54
    }
55
56
    /**
57
     * Create a translation.
58
     *
59
     * @param string $projectKey
60
     * @param string $localeId
61
     * @param string $keyId
62
     * @param string $content
63
     * @param array  $params
64
     *
65
     * @throws ClientExceptionInterface
66
     * @throws DomainException
67
     *
68
     * @return TranslationCreated|ResponseInterface
69
     */
70
    public function create(string $projectKey, string $localeId, string $keyId, string $content, array $params = [])
71
    {
72
        $params['locale_id'] = $localeId;
73
        $params['key_id'] = $keyId;
74
        $params['content'] = $content;
75
76
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/translations', $projectKey), $params);
77
78
        if (!$this->hydrator) {
79
            return $response;
80
        }
81
82
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
83
            $this->handleErrors($response);
84
        }
85
86
        return $this->hydrator->hydrate($response, TranslationCreated::class);
87
    }
88
89
    /**
90
     * Update a translation.
91
     *
92
     * @param string $projectKey
93
     * @param string $translationId
94
     * @param string $content
95
     * @param array  $params
96
     *
97
     * @throws ClientExceptionInterface
98
     * @throws DomainException
99
     *
100
     * @return TranslationUpdated|ResponseInterface
101
     */
102 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...
103
    {
104
        $params['content'] = $content;
105
106
        $response = $this->httpPatch(sprintf('/api/v2/projects/%s/translations/%s', $projectKey, $translationId), $params);
107
108
        if (!$this->hydrator) {
109
            return $response;
110
        }
111
112
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
113
            $this->handleErrors($response);
114
        }
115
116
        return $this->hydrator->hydrate($response, TranslationUpdated::class);
117
    }
118
119
    /**
120
     * List translations for a specific key.
121
     *
122
     * @param string $projectKey
123
     * @param string $keyId
124
     * @param array  $params
125
     *
126
     * @throws ClientExceptionInterface
127
     * @throws DomainException
128
     *
129
     * @return Index|ResponseInterface
130
     */
131 View Code Duplication
    public function indexKey(string $projectKey, string $keyId, 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...
132
    {
133
        if (isset($params['tags'])) {
134
            $params['q'] = 'tags:'.$params['tags'];
135
            unset($params['tags']);
136
        }
137
138
        $response = $this->httpGet(sprintf('/api/v2/projects/%s/keys/%s/translations', $projectKey, $keyId), $params);
139
140
        if (!$this->hydrator) {
141
            return $response;
142
        }
143
144
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
145
            $this->handleErrors($response);
146
        }
147
148
        return $this->hydrator->hydrate($response, Index::class);
149
    }
150
}
151