Skip to content

Commit f5cebf0

Browse files
committed
Public methods for verified endpoints
1 parent c26d86e commit f5cebf0

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

src/VerifierServer/Endpoints/SS14VerifiedEndpoint.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ protected function post(ServerRequestInterface|string $request, int|string &$res
9898

9999
switch ($methodType) {
100100
case 'delete':
101-
$existingIndex = array_search($ss14, array_column($list, 'ss14'));
102-
if ($existingIndex === false) $existingIndex = array_search($discord, array_column($list, 'discord'));
103101
$this->delete(
104-
$existingIndex,
102+
$this->getIndex($discord, $ss14),
105103
$list,
106104
$response,
107105
$headers,
@@ -147,16 +145,38 @@ protected function __post(array &$list, string $ss14, string $discord, int|strin
147145
return;
148146
}
149147

148+
$this->add($discord, $ss14);
149+
150+
$headers = ['Content-Type' => 'application/json'];
151+
$headers['Content-Length'] = ($body = $this->__content())
152+
? strlen($body)
153+
: 0;
154+
}
155+
156+
public function add(string $discord, string $ss14): void
157+
{
158+
$list = &$this->state->getVerifyList();
150159
$list[] = [
151160
'discord' => $discord,
152161
'ss14' => $ss14,
153162
'create_time' => date('Y-m-d H:i:s')
154163
];
155164
$this->state::writeJson($this->state->getJsonPath(), $list);
156165
$this->state->setVerifyList($list);
157-
$headers = ['Content-Type' => 'application/json'];
158-
$headers['Content-Length'] = ($body = $this->__content())
159-
? strlen($body)
160-
: 0;
166+
}
167+
168+
public function remove(string $discord, string $ss14): ?array
169+
{
170+
$existingIndex = $this->getIndex($discord, $ss14);
171+
if ($existingIndex === false) return null;
172+
return $this->removeIndex($existingIndex);
173+
}
174+
175+
public function getIndex(string $discord, string $ss14): int|string|false
176+
{
177+
$list = &$this->state->getVerifyList();
178+
$existingIndex = array_search($discord, array_column($list, 'discord'));
179+
if ($existingIndex === false) $existingIndex = array_search($ss14, array_column($list, 'ss14'));
180+
return $existingIndex;
161181
}
162182
}

src/VerifierServer/Endpoints/VerifiedEndpoint.php

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ protected function post(ServerRequestInterface|string $request, int|string &$res
169169

170170
switch ($methodType) {
171171
case 'delete':
172-
$existingIndex = array_search($ckey, array_column($list, 'ss13'));
173-
if ($existingIndex === false) $existingIndex = array_search($discord, array_column($list, 'discord'));
174172
$this->delete(
175-
$existingIndex,
173+
$this->getIndex($ckey, $discord),
176174
$list,
177175
$response,
178176
$headers,
@@ -218,13 +216,8 @@ protected function __post(array &$list, string $ckey, string $discord, int|strin
218216
return;
219217
}
220218

221-
$list[] = [
222-
'ss13' => $ckey,
223-
'discord' => $discord,
224-
'create_time' => date('Y-m-d H:i:s')
225-
];
226-
$this->state::writeJson($this->state->getJsonPath(), $list);
227-
$this->state->setVerifyList($list);
219+
$this->add($ckey, $discord);
220+
228221
$headers = ['Content-Type' => 'application/json'];
229222
$headers['Content-Length'] = ($body = $this->__content())
230223
? strlen($body)
@@ -248,13 +241,49 @@ protected function delete(int|string|false $existingIndex, array &$list, int|str
248241
$body = 'Not Found';
249242
return;
250243
}
251-
$splice = array_splice($list, $existingIndex, 1);
252-
$this->state::writeJson($this->state->getJsonPath(), $list);
253-
$this->state->setVerifyList($list);
244+
245+
$splice = $this->removeIndex($existingIndex);
246+
254247
$response = Response::STATUS_OK;
255248
$headers = ['Content-Type' => 'application/json'];
256249
$headers['Content-Length'] = ($content = json_encode($splice))
257250
? strlen($body = $content)
258251
: 0;
259252
}
253+
254+
public function add(string $ckey, string $discord): void
255+
{
256+
$list = &$this->state->getVerifyList();
257+
$list[] = [
258+
'ss13' => $ckey,
259+
'discord' => $discord,
260+
'create_time' => date('Y-m-d H:i:s')
261+
];
262+
$this->state::writeJson($this->state->getJsonPath(), $list);
263+
$this->state->setVerifyList($list);
264+
}
265+
266+
public function remove(string $ckey, string $discord): ?array
267+
{
268+
$existingIndex = $this->getIndex($ckey, $discord);
269+
if ($existingIndex === false) return null;
270+
return $this->removeIndex($existingIndex);
271+
}
272+
273+
public function getIndex(string $ckey, string $discord): int|string|false
274+
{
275+
$list = &$this->state->getVerifyList();
276+
$existingIndex = array_search($ckey, array_column($list, 'ss13'));
277+
if ($existingIndex === false) $existingIndex = array_search($discord, array_column($list, 'discord'));
278+
return $existingIndex;
279+
}
280+
281+
public function removeIndex(int|string $existingIndex): array
282+
{
283+
$list = &$this->state->getVerifyList();
284+
$splice = array_splice($list, (int)$existingIndex, 1);
285+
$this->state::writeJson($this->state->getJsonPath(), $list);
286+
$this->state->setVerifyList($list);
287+
return $splice;
288+
}
260289
}

0 commit comments

Comments
 (0)