Translation::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 10
1
<?php
2
3
/*
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\Localise\Api;
11
12
use FAPI\Localise\Model\Translation\TranslationDeleted;
13
use FAPI\Localise\Model\Translation\Translation as TranslationModel;
14
use Psr\Http\Message\ResponseInterface;
15
use FAPI\Localise\Exception;
16
17
/**
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
class Translation extends HttpApi
21
{
22
    /**
23
     * Get a translation.
24
     * {@link https://localise.biz/api/docs/translations/gettranslation}.
25
     *
26
     * @param string $projectKey
27
     * @param string $id
28
     * @param string $locale
29
     *
30
     * @return TranslationModel|ResponseInterface
31
     *
32
     * @throws Exception
33
     */
34
    public function get(string $projectKey, string $id, string $locale)
35
    {
36
        $response = $this->httpGet(sprintf('/api/translations/%s/%s?key=%s', rawurlencode($id), $locale, $projectKey));
37
38
        if (!$this->hydrator) {
39
            return $response;
40
        }
41
42
        if (200 !== $response->getStatusCode()) {
43
            $this->handleErrors($response);
44
        }
45
46
        return $this->hydrator->hydrate($response, TranslationModel::class);
47
    }
48
49
    /**
50
     * Create a new translation.
51
     * {@link https://localise.biz/api/docs/translations/translate}.
52
     *
53
     * @param string $projectKey
54
     * @param string $id
55
     * @param string $locale
56
     * @param string $translation
57
     *
58
     * @return TranslationModel|ResponseInterface
59
     *
60
     * @throws Exception
61
     */
62
    public function create(string $projectKey, string $id, string $locale, string $translation)
63
    {
64
        $response = $this->httpPostRaw(sprintf('/api/translations/%s/%s?key=%s', rawurlencode($id), $locale, $projectKey), $translation);
65
        if (!$this->hydrator) {
66
            return $response;
67
        }
68
69
        if ($response->getStatusCode() >= 400) {
70
            $this->handleErrors($response);
71
        }
72
73
        return $this->hydrator->hydrate($response, TranslationModel::class);
74
    }
75
76
    /**
77
     * Delete translation
78
     * {@link https://localise.biz/api/docs/translations/untranslate}.
79
     *
80
     * @param string $projectKey
81
     * @param string $id
82
     * @param string $locale
83
     *
84
     * @return TranslationDeleted|ResponseInterface
85
     *
86
     * @throws Exception
87
     */
88
    public function delete(string $projectKey, string $id, string $locale)
89
    {
90
        $response = $this->httpDelete(sprintf('/api/translations/%s/%s?key=%s', rawurlencode($id), $locale, $projectKey));
91
        if (!$this->hydrator) {
92
            return $response;
93
        }
94
95
        if (200 !== $response->getStatusCode()) {
96
            $this->handleErrors($response);
97
        }
98
99
        return $this->hydrator->hydrate($response, TranslationDeleted::class);
100
    }
101
}
102