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\Exception\DomainException; |
13
|
|
|
use FAPI\PhraseApp\Model\Key\KeyCreated; |
14
|
|
|
use FAPI\PhraseApp\Model\Key\KeySearchResults; |
15
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Key extends HttpApi |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Create a new key. |
25
|
|
|
* |
26
|
|
|
* @param string $projectKey |
27
|
|
|
* @param string $name |
28
|
|
|
* @param array $params |
29
|
|
|
* |
30
|
|
|
* @throws DomainException |
31
|
|
|
* @throws ClientExceptionInterface |
32
|
|
|
* |
33
|
|
|
* @return KeyCreated|ResponseInterface |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function create(string $projectKey, string $name, array $params = []) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$params['name'] = $name; |
38
|
|
|
|
39
|
|
|
$response = $this->httpPost(sprintf('/api/v2/projects/%s/keys', $projectKey), $params); |
40
|
|
|
|
41
|
|
|
if (!$this->hydrator) { |
42
|
|
|
return $response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
46
|
|
|
$this->handleErrors($response); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->hydrator->hydrate($response, KeyCreated::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Search keys. |
54
|
|
|
* |
55
|
|
|
* @param string $projectKey |
56
|
|
|
* @param array $params |
57
|
|
|
* |
58
|
|
|
* @throws ClientExceptionInterface |
59
|
|
|
* @throws DomainException |
60
|
|
|
* |
61
|
|
|
* @return KeySearchResults|ResponseInterface |
62
|
|
|
*/ |
63
|
|
|
public function search(string $projectKey, array $params = []) |
64
|
|
|
{ |
65
|
|
|
$q = ''; |
66
|
|
|
|
67
|
|
|
if (isset($params['tags'])) { |
68
|
|
|
$q .= 'tags:'.$params['tags'].' '; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (isset($params['name'])) { |
72
|
|
|
$q .= 'name:'.$params['name'].' '; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($params['ids'])) { |
76
|
|
|
$q .= 'ids:'.$params['ids'].' '; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!empty($q)) { |
80
|
|
|
$params['q'] = $q; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$response = $this->httpPost(sprintf('/api/v2/projects/%s/keys/search', $projectKey), $params); |
84
|
|
|
|
85
|
|
|
if (!$this->hydrator) { |
86
|
|
|
return $response; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
90
|
|
|
$this->handleErrors($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->hydrator->hydrate($response, KeySearchResults::class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Delete a key. |
98
|
|
|
* |
99
|
|
|
* @param string $projectKey |
100
|
|
|
* @param string $keyId |
101
|
|
|
* |
102
|
|
|
* @throws ClientExceptionInterface |
103
|
|
|
* @throws DomainException |
104
|
|
|
* |
105
|
|
|
* @return bool|ResponseInterface |
106
|
|
|
*/ |
107
|
|
|
public function delete(string $projectKey, string $keyId) |
108
|
|
|
{ |
109
|
|
|
$response = $this->httpDelete(sprintf('/api/v2/projects/%s/keys/%s', $projectKey, $keyId)); |
110
|
|
|
|
111
|
|
|
if (!$this->hydrator) { |
112
|
|
|
return $response; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($response->getStatusCode() !== 204) { |
116
|
|
|
$this->handleErrors($response); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.