Asset   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 40
c 1
b 1
f 0
dl 0
loc 116
ccs 0
cts 62
cp 0
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B patch() 0 33 8
A create() 0 23 4
A tag() 0 16 3
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\Asset\Asset as AssetModel;
13
use Psr\Http\Message\ResponseInterface;
14
use FAPI\Localise\Exception;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
class Asset extends HttpApi
20
{
21
    /**
22
     * Create an asset.
23
     * {@link https://localise.biz/api/docs/assets/createasset}.
24
     *
25
     * @param string $projectKey
26
     * @param string $id
27
     *
28
     * @return AssetModel|ResponseInterface
29
     *
30
     * @throws Exception
31
     */
32
    public function create(string $projectKey, string $id)
33
    {
34
        $param = [
35
            'name' => $id,
36
            'id' => $id,
37
            'type' => 'text',
38
            'default' => 'untranslated',
39
        ];
40
41
        $response = $this->httpPost(sprintf('/api/assets?key=%s', $projectKey), $param);
42
        if (!$this->hydrator) {
43
            return $response;
44
        }
45
46
        if (409 === $response->getStatusCode()) {
47
            throw Exception\Domain\AssetConflictException::create($id);
48
        }
49
50
        if (201 !== $response->getStatusCode()) {
51
            $this->handleErrors($response);
52
        }
53
54
        return $this->hydrator->hydrate($response, AssetModel::class);
55
    }
56
57
    /**
58
     * Tag an asset.
59
     * {@link https://localise.biz/api/docs/assets/tagasset}.
60
     *
61
     * @param string $projectKey
62
     * @param string $id
63
     * @param string $tag
64
     *
65
     * @return AssetModel|ResponseInterface
66
     *
67
     * @throws Exception\DomainException
68
     */
69
    public function tag(string $projectKey, string $id, string $tag)
70
    {
71
        $param = [
72
            'name' => $tag,
73
        ];
74
75
        $response = $this->httpPost(sprintf('/api/assets/%s/tags?key=%s', $id, $projectKey), $param);
76
        if (!$this->hydrator) {
77
            return $response;
78
        }
79
80
        if ($response->getStatusCode() >= 400) {
81
            $this->handleErrors($response);
82
        }
83
84
        return $this->hydrator->hydrate($response, AssetModel::class);
85
    }
86
87
    /**
88
     * Patch an asset.
89
     * {@link https://localise.biz/api/docs/assets/patchasset}.
90
     *
91
     * @param string $projectKey
92
     * @param string $id
93
     * @param string $type
94
     * @param string $name
95
     * @param string $context
96
     * @param string $notes
97
     *
98
     * @return AssetModel|ResponseInterface
99
     *
100
     * @throws Exception
101
     */
102
    public function patch(string $projectKey, string $id, $type = null, $name = null, $context = null, $notes = null)
103
    {
104
        $param = [
105
            'id' => $id,
106
        ];
107
108
        if ($type) {
109
            $param['type'] = $type;
110
        }
111
        if ($name) {
112
            $param['name'] = $name;
113
        }
114
        if ($context) {
115
            $param['context'] = $context;
116
        }
117
        if ($notes) {
118
            $param['notes'] = $notes;
119
        }
120
121
        $response = $this->httpPatch(sprintf('/api/assets/%s.json?key=%s', $id, $projectKey), $param);
122
        if (!$this->hydrator) {
123
            return $response;
124
        }
125
126
        if (409 === $response->getStatusCode()) {
127
            throw Exception\Domain\AssetConflictException::create($id);
128
        }
129
130
        if ($response->getStatusCode() >= 400) {
131
            $this->handleErrors($response);
132
        }
133
134
        return $this->hydrator->hydrate($response, AssetModel::class);
135
    }
136
}
137