44use VerifierServer \PersistentState ;
55
66class VerifiedEndpoint {
7- private PersistentState $ state ;
7+ public function __construct (private PersistentState $ state )
8+ {}
89
9- public function __construct (PersistentState $ state ) {
10- $ this ->state = $ state ;
11- }
12-
13- public function handleRequest ($ method , $ request , &$ response ) {
10+ /**
11+ * Handles the incoming HTTP request and generates the appropriate response.
12+ *
13+ * @param string $method The HTTP method of the request (e.g., 'GET', 'POST').
14+ * @param string $request The request payload, typically used for 'POST' requests.
15+ * @param string &$response The variable to store the generated response.
16+ */
17+ public function handleRequest (string $ method , string $ request , string &$ response ): void
18+ {
1419 switch ($ method ) {
1520 case 'GET ' :
1621 $ this ->handleGet ($ response );
@@ -24,12 +29,36 @@ public function handleRequest($method, $request, &$response) {
2429 }
2530 }
2631
27- private function handleGet (&$ response ) {
32+ /**
33+ * Handles the GET request and prepares the response.
34+ *
35+ * @param string &$response The response string to be sent back to the client.
36+ *
37+ * This method sets the HTTP status code to 200 OK and the Content-Type to application/json.
38+ * It then appends the JSON-encoded verification list to the response.
39+ */
40+ private function handleGet (string &$ response ): void
41+ {
2842 $ response = "HTTP/1.1 200 OK " . PHP_EOL . "Content-Type: application/json " . PHP_EOL . PHP_EOL ;
2943 $ response .= json_encode ($ this ->state ->getVerifyList ());
3044 }
3145
32- private function handlePost ($ request , &$ response ) {
46+ /**
47+ * Handles POST requests by parsing the request data and performing actions based on the method type.
48+ *
49+ * @param string $request The raw HTTP request string.
50+ * @param string &$response The response string to be modified based on the request handling.
51+ *
52+ * The function performs the following steps:
53+ * 1. Extracts the raw data from the request.
54+ * 2. Parses the raw data into an associative array.
55+ * 3. Retrieves the method type, ckey, discord, and token from the parsed data.
56+ * 4. Checks if the provided token matches the expected token. If not, sets the response to 401 Unauthorized.
57+ * 5. Retrieves the verification list from the state.
58+ * 6. Based on the method type, either deletes an entry from the list or handles the default case.
59+ */
60+ private function handlePost (string $ request , string &$ response ): void
61+ {
3362 $ rawData = explode (PHP_EOL . PHP_EOL , $ request , 2 )[1 ];
3463 parse_str ($ rawData , $ formData );
3564
@@ -44,19 +73,26 @@ private function handlePost($request, &$response) {
4473 }
4574
4675 $ list = $ this ->state ->getVerifyList ();
47- $ existingIndex = array_search ($ discord , array_column ($ list , 'discord ' ));
4876
4977 switch ($ methodType ) {
5078 case 'delete ' :
51- $ this ->handleDelete ($ existingIndex , $ list , $ response );
79+ $ this ->handleDelete (array_search ( $ discord , array_column ( $ list , ' discord ' )) , $ list , $ response );
5280 break ;
5381 default :
54- $ this ->handleDefault ($ existingIndex , $ list , $ ckey , $ discord , $ response );
82+ $ this ->handleDefault ($ list , $ ckey , $ discord , $ response );
5583 break ;
5684 }
5785 }
5886
59- private function handleDelete ($ existingIndex , &$ list , &$ response ) {
87+ /**
88+ * Handles the deletion of an item from the list.
89+ *
90+ * @param int|string|false $existingIndex The index of the item to delete, or false if the item does not exist.
91+ * @param array &$list The list from which the item will be deleted.
92+ * @param string &$response The HTTP response message to be returned.
93+ */
94+ private function handleDelete (int |string |false $ existingIndex , array &$ list , string &$ response ): void
95+ {
6096 if ($ existingIndex !== false ) {
6197 array_splice ($ list , $ existingIndex , 1 );
6298 PersistentState::writeJson ("verify.json " , $ list );
@@ -67,7 +103,21 @@ private function handleDelete($existingIndex, &$list, &$response) {
67103 }
68104 }
69105
70- public function handleDefault ($ existingIndex , &$ list , $ ckey , $ discord , &$ response ) {
106+ /**
107+ * Handles the default verification process.
108+ *
109+ * This method checks if the provided `ckey` or `discord` already exists in the list.
110+ * If either exists, it sets the response to a 403 Forbidden status.
111+ * If neither exists, it adds the new entry to the list, writes the updated list to a JSON file,
112+ * updates the state, and sets the response to a 200 OK status.
113+ *
114+ * @param array $list The list of existing entries.
115+ * @param string $ckey The ckey to be verified.
116+ * @param string $discord The discord identifier to be verified.
117+ * @param string &$response The response message to be set based on the verification result.
118+ */
119+ public function handleDefault (array &$ list , string $ ckey , string $ discord , string &$ response ): void
120+ {
71121 $ existingCkeyIndex = array_search ($ ckey , array_column ($ list , 'ss13 ' ));
72122 $ existingDiscordIndex = array_search ($ discord , array_column ($ list , 'discord ' ));
73123
0 commit comments