Completed
Push — master ( 9d0b1f...3a7434 )
by Tobias
10:39
created

Asset::patch()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 29
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 19
nc 64
nop 6
crap 72
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 ($response->getStatusCode() !== 201) {
47
            $this->handleErrors($response);
48
        }
49
50
        if ($response->getStatusCode() === 409) {
51
            throw Exception\Domain\AssetConflictException::create($id);
52
        }
53
54
        return $this->hydrator->hydrate($response, AssetModel::class);
55
    }
56
57
    /**
58
     * Patch an asset.
59
     * {@link https://localise.biz/api/docs/assets/patchasset}.
60
     *
61
     * @param string $projectKey
62
     * @param string $id
63
     * @param string $type
64
     * @param string $name
65
     * @param string $context
66
     * @param string $notes
67
     *
68
     * @return AssetModel|ResponseInterface
69
     *
70
     * @throws Exception
71
     */
72
    public function patch(string $projectKey, string $id, $type = null, $name = null, $context = null, $notes = null)
73
    {
74
        $param = [
75
            'id' => $id,
76
        ];
77
78
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
            $param['type'] = $type;
80
        }
81
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
82
            $param['name'] = $name;
83
        }
84
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
85
            $param['context'] = $context;
86
        }
87
        if ($notes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notes of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
            $param['notes'] = $notes;
89
        }
90
91
        $response = $this->httpPatch(sprintf('/api/assets/%s.json?key=%s', $id, $projectKey), $param);
92
        if (!$this->hydrator) {
93
            return $response;
94
        }
95
96
        if ($response->getStatusCode() === 409) {
97
            throw Exception\Domain\AssetConflictException::create($id);
98
        }
99
100
        if ($response->getStatusCode() >= 400) {
101
            $this->handleErrors($response);
102
        }
103
104
        return $this->hydrator->hydrate($response, AssetModel::class);
105
    }
106
}
107