1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the KongAdminApi package. |
5
|
|
|
* |
6
|
|
|
* (c) Unikorp <https://github.com/unikorp> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Unikorp\KongAdminApi; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* abstract node |
16
|
|
|
* |
17
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractNode implements NodeInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* json content type |
23
|
|
|
* @const array JSON_CONTENT_TYPE |
24
|
|
|
*/ |
25
|
|
|
const JSON_CONTENT_TYPE = [ |
26
|
|
|
'Content-Type' => 'application/json', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* client |
31
|
|
|
* @var \Unikorp\KongAdminApi\Client $client |
32
|
|
|
*/ |
33
|
|
|
private $client = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
* |
38
|
|
|
* @param \Unikorp\KongAdminApi\Client $client |
39
|
|
|
*/ |
40
|
5 |
|
public function __construct(Client $client) |
41
|
|
|
{ |
42
|
5 |
|
$this->client = $client; |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* get |
47
|
|
|
* |
48
|
|
|
* @param string $endpoint |
49
|
|
|
* |
50
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
51
|
|
|
*/ |
52
|
12 |
|
protected function get($endpoint) |
53
|
|
|
{ |
54
|
12 |
|
return $this->client->getHttpClient()->get($endpoint); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* post |
59
|
|
|
* |
60
|
|
|
* @param string $endpoint |
61
|
|
|
* @param DocumentInterface $document |
62
|
|
|
* |
63
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
64
|
|
|
*/ |
65
|
3 |
View Code Duplication |
protected function post($endpoint, DocumentInterface $document = null) |
|
|
|
|
66
|
|
|
{ |
67
|
3 |
|
return $this->client->getHttpClient()->post( |
68
|
3 |
|
$endpoint, |
69
|
3 |
|
self::JSON_CONTENT_TYPE, |
70
|
3 |
|
$document ? $document->toJson() : '[]' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* put |
76
|
|
|
* |
77
|
|
|
* @param string $endpoint |
78
|
|
|
* @param DocumentInterface $document |
79
|
|
|
* |
80
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
81
|
|
|
*/ |
82
|
3 |
View Code Duplication |
protected function put($endpoint, DocumentInterface $document = null) |
|
|
|
|
83
|
|
|
{ |
84
|
3 |
|
return $this->client->getHttpClient()->put( |
85
|
3 |
|
$endpoint, |
86
|
3 |
|
self::JSON_CONTENT_TYPE, |
87
|
3 |
|
$document ? $document->toJson() : '[]' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* patch |
93
|
|
|
* |
94
|
|
|
* @param string $endpoint |
95
|
|
|
* @param DocumentInterface $document |
96
|
|
|
* |
97
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
98
|
|
|
*/ |
99
|
3 |
View Code Duplication |
protected function patch($endpoint, DocumentInterface $document = null) |
|
|
|
|
100
|
|
|
{ |
101
|
3 |
|
return $this->client->getHttpClient()->patch( |
102
|
3 |
|
$endpoint, |
103
|
3 |
|
self::JSON_CONTENT_TYPE, |
104
|
3 |
|
$document ? $document->toJson() : '[]' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* delete |
110
|
|
|
* |
111
|
|
|
* @param string $endpoint |
112
|
|
|
* @param DocumentInterface $document |
113
|
|
|
* |
114
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
115
|
|
|
*/ |
116
|
4 |
View Code Duplication |
protected function delete($endpoint, DocumentInterface $document = null) |
|
|
|
|
117
|
|
|
{ |
118
|
4 |
|
return $this->client->getHttpClient()->delete( |
119
|
4 |
|
$endpoint, |
120
|
4 |
|
self::JSON_CONTENT_TYPE, |
121
|
4 |
|
$document ? $document->toJson() : '[]' |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.