1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Kong\Http\Request; |
6
|
|
|
|
7
|
|
|
use CCT\Kong\Config; |
8
|
|
|
use CCT\Kong\Exception\InvalidParameterException; |
9
|
|
|
use CCT\Kong\Form\Normalizer\PluginFormNormalizer; |
10
|
|
|
use CCT\Kong\Http\Definition\QueryParams; |
11
|
|
|
use CCT\Kong\Http\Request; |
12
|
|
|
use CCT\Kong\Http\ResponseInterface; |
13
|
|
|
use CCT\Kong\Model\Plugin; |
14
|
|
|
use JMS\Serializer\SerializationContext; |
15
|
|
|
|
16
|
|
|
class PluginRequest extends Request |
17
|
|
|
{ |
18
|
13 |
|
protected function setUp() |
19
|
|
|
{ |
20
|
13 |
|
if (false === $this->config->has(Config::URI_PREFIX)) { |
21
|
13 |
|
$this->config->set(Config::URI_PREFIX, '/plugins/'); |
22
|
|
|
} |
23
|
13 |
|
} |
24
|
|
|
|
25
|
2 |
|
public function list(QueryParams $queryParams = null): ResponseInterface |
26
|
|
|
{ |
27
|
2 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('read')); |
28
|
|
|
|
29
|
2 |
|
return parent::requestGet($this->getUri(), $queryParams); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function listEnabled(): ResponseInterface |
33
|
|
|
{ |
34
|
1 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('read')); |
35
|
1 |
|
$this->config->set(Config::RESPONSE_TRANSFORMERS, []); |
36
|
|
|
|
37
|
1 |
|
return parent::requestGet($this->appendToUri('/enabled/'), null); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function listByApi(string $apiId): ResponseInterface |
41
|
|
|
{ |
42
|
1 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('read')); |
43
|
|
|
|
44
|
1 |
|
return parent::requestGet($this->getUriByApiId($apiId), null); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public function create(Plugin $plugin, string $apiId = null): ResponseInterface |
48
|
|
|
{ |
49
|
2 |
|
if (null === $plugin->getApiId() && null === $apiId) { |
50
|
|
|
throw new InvalidParameterException('The API ID is required to create a new Plugin.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('create')); |
54
|
2 |
|
$this->changeFormNormalizer(); |
55
|
|
|
|
56
|
2 |
|
$apiId = $plugin->getApiId() ?: $apiId; |
57
|
2 |
|
$uri = $this->getUriByApiId($apiId); |
|
|
|
|
58
|
|
|
|
59
|
2 |
|
return parent::requestPost($uri, $plugin, null); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
public function retrieve(string $apiId, string $pluginId): ResponseInterface |
63
|
|
|
{ |
64
|
5 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('read')); |
65
|
5 |
|
$uri = $this->getUriByApiId($apiId); |
66
|
|
|
|
67
|
5 |
|
return parent::requestGet($this->appendToUri($pluginId, $uri), null); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function retrieveSchema(string $pluginName): ResponseInterface |
71
|
|
|
{ |
72
|
1 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('read')); |
73
|
1 |
|
$this->config->set(Config::RESPONSE_TRANSFORMERS, []); |
74
|
|
|
|
75
|
1 |
|
$uri = $this->appendToUri(sprintf( |
76
|
1 |
|
'/schema/%s/', |
77
|
1 |
|
$pluginName |
78
|
|
|
)); |
79
|
|
|
|
80
|
1 |
|
return parent::requestGet($uri, null); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function update(Plugin $plugin): ResponseInterface |
84
|
|
|
{ |
85
|
1 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('update')); |
86
|
1 |
|
$this->changeFormNormalizer(); |
87
|
|
|
|
88
|
1 |
|
$uri = $this->getUriByApiId($plugin->getApiId()); |
89
|
|
|
|
90
|
1 |
|
return parent::requestPatch($this->appendToUri($plugin->getId(), $uri), $plugin, null); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function updateOrCreate(Plugin $plugin, string $apiId = null): ResponseInterface |
94
|
|
|
{ |
95
|
1 |
|
if (null === $plugin->getApiId() && null === $apiId) { |
96
|
|
|
throw new InvalidParameterException('The API ID is required to create a new Plugin.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups([ 'update', 'create' ])); |
100
|
1 |
|
$this->changeFormNormalizer(); |
101
|
|
|
|
102
|
1 |
|
$apiId = $plugin->getApiId() ?: $apiId; |
103
|
1 |
|
$uri = $this->getUriByApiId($apiId); |
|
|
|
|
104
|
|
|
|
105
|
1 |
|
return parent::requestPut($uri, $plugin, null); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
public function delete(Plugin $plugin): ResponseInterface |
109
|
|
|
{ |
110
|
1 |
|
$this->validateObjectId($plugin); |
111
|
1 |
|
$this->config->set('serialization_context', SerializationContext::create()->setGroups('delete')); |
112
|
|
|
|
113
|
1 |
|
$uri = $this->getUriByApiId($plugin->getApiId()); |
114
|
|
|
|
115
|
1 |
|
return parent::requestDelete($this->appendToUri($plugin->getId(), $uri), null); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $apiId |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
8 |
|
protected function getUriByApiId(string $apiId): string |
124
|
|
|
{ |
125
|
8 |
|
return sprintf( |
126
|
8 |
|
'/apis/%s/%s', |
127
|
8 |
|
$apiId, |
128
|
8 |
|
ltrim($this->getUri(), '/') |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the FormNormalizer to use one specific for Plugin. |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
4 |
|
protected function changeFormNormalizer(): void |
138
|
|
|
{ |
139
|
4 |
|
$this->config->set( |
140
|
4 |
|
Config::FORM_NORMALIZER, |
141
|
4 |
|
new PluginFormNormalizer( |
142
|
4 |
|
$this->serializer, |
143
|
4 |
|
$this->config->get('serialization_context') |
144
|
|
|
) |
145
|
|
|
); |
146
|
4 |
|
} |
147
|
|
|
} |
148
|
|
|
|