Completed
Push — master ( 9e1b08...fbe734 )
by Sascha-Oliver
02:17
created

Translation   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 46.55 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 54
loc 116
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B indexLocale() 19 19 5
A create() 0 18 4
A update() 16 16 4
B indexKey() 19 19 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        if (isset($params['tags'])) {
34
            $params['q'] = 'tags:'.$params['tags'];
35
            unset($params['tags']);
36
        }
37
38
        $response = $this->httpGet(sprintf('/api/v2/projects/%s/locales/%s/translations', $projectKey, $localeId), $params);
39
40
        if (!$this->hydrator) {
41
            return $response;
42
        }
43
44
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
45
            $this->handleErrors($response);
46
        }
47
48
        return $this->hydrator->hydrate($response, Index::class);
49
    }
50
51
    /**
52
     * Create a translation.
53
     *
54
     * @param string $projectKey
55
     * @param string $keyId
56
     * @param string $content
57
     * @param array  $params
58
     *
59
     * @return mixed|ResponseInterface
60
     */
61
    public function create(string $projectKey, string $localeId, string $keyId, string $content, array $params = [])
62
    {
63
        $params['locale_id'] = $localeId;
64
        $params['key_id'] = $keyId;
65
        $params['content'] = $content;
66
67
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/translations', $projectKey), $params);
68
69
        if (!$this->hydrator) {
70
            return $response;
71
        }
72
73
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
74
            $this->handleErrors($response);
75
        }
76
77
        return $this->hydrator->hydrate($response, TranslationCreated::class);
78
    }
79
80
    /**
81
     * Update a translation.
82
     *
83
     * @param string $projectKey
84
     * @param string $translationId
85
     * @param string $content
86
     * @param array  $params
87
     *
88
     * @return mixed|ResponseInterface
89
     */
90 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...
91
    {
92
        $params['content'] = $content;
93
94
        $response = $this->httpPatch(sprintf('/api/v2/projects/%s/translations/%s', $projectKey, $translationId), $params);
95
96
        if (!$this->hydrator) {
97
            return $response;
98
        }
99
100
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
101
            $this->handleErrors($response);
102
        }
103
104
        return $this->hydrator->hydrate($response, TranslationUpdated::class);
105
    }
106
107
    /**
108
     * List translations for a specific key.
109
     *
110
     * @param string $projectKey
111
     * @param string $keyId
112
     * @param array  $params
113
     *
114
     * @return mixed|ResponseInterface
115
     */
116 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...
117
    {
118
        if (isset($params['tags'])) {
119
            $params['q'] = 'tags:'.$params['tags'];
120
            unset($params['tags']);
121
        }
122
123
        $response = $this->httpGet(sprintf('/api/v2/projects/%s/keys/%s/translations', $projectKey, $keyId), $params);
124
125
        if (!$this->hydrator) {
126
            return $response;
127
        }
128
129
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
130
            $this->handleErrors($response);
131
        }
132
133
        return $this->hydrator->hydrate($response, Index::class);
134
    }
135
}
136