Completed
Push — master ( 08753a...42665f )
by Sascha-Oliver
05:38
created

Key::search()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 27
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 48
nop 3
crap 72
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\Key\KeyCreated;
13
use FAPI\PhraseApp\Model\Key\KeySearchResults;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * @author Sascha-Oliver Prolic <[email protected]>
18
 */
19
class Key extends HttpApi
20
{
21
    /**
22
     * Create a new key.
23
     *
24
     * @param string $projectKey
25
     * @param string $localeId
0 ignored issues
show
Bug introduced by
There is no parameter named $localeId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     * @param array  $params
27
     *
28
     * @return mixed|ResponseInterface
29
     */
30 View Code Duplication
    public function create(string $projectKey, string $name, 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...
31
    {
32
        $params['name'] = $name;
33
34
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/keys', $projectKey), $params);
35
36
        if (!$this->hydrator) {
37
            return $response;
38
        }
39
40
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
41
            $this->handleErrors($response);
42
        }
43
44
        return $this->hydrator->hydrate($response, KeyCreated::class);
45
    }
46
47
    /**
48
     * Search keys.
49
     *
50
     * @param string $projectKey
51
     * @param string $localeId
52
     * @param array  $params
53
     *
54
     * @return mixed|ResponseInterface
55
     */
56
    public function search(string $projectKey, string $localeId, array $params = [])
57
    {
58
        $q = '';
59
60
        if (isset($params['tags'])) {
61
            $q .= 'tags:'.$params['tags'].' ';
62
        }
63
64
        if (isset($params['name'])) {
65
            $q .= 'name:'.$params['name'].' ';
66
        }
67
68
        if (isset($params['ids'])) {
69
            $q .= 'ids:'.$params['ids'].' ';
70
        }
71
72
        $params = [
73
            'locale_id' => $localeId,
74
        ];
75
76
        if (!empty($q)) {
77
            $params['q'] = $q;
78
        }
79
80
        $response = $this->httpPost(sprintf('/api/v2/projects/%s/keys/search', $projectKey), $params);
81
82
        if (!$this->hydrator) {
83
            return $response;
84
        }
85
86
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
87
            $this->handleErrors($response);
88
        }
89
90
        return $this->hydrator->hydrate($response, KeySearchResults::class);
91
    }
92
93
    /**
94
     * Delete a key.
95
     *
96
     * @param string $projectKey
97
     * @param string $keyId
98
     *
99
     * @return bool|ResponseInterface
100
     */
101
    public function delete(string $projectKey, string $keyId)
102
    {
103
        $response = $this->httpDelete(sprintf('/api/v2/projects/%s/keys/%s', $projectKey, $keyId));
104
105
        if ($response->getStatusCode() !== 204) {
106
            $this->handleErrors($response);
107
        }
108
109
        return true;
110
    }
111
}
112