RequestHandler.php
183 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Api\V1; |
| 4 | |
| 5 | use FapiMember\Api\V2\Endpoints\MembershipsController; |
| 6 | use FapiMember\Container\Container; |
| 7 | use FapiMember\FapiMemberPlugin; |
| 8 | use FapiMember\Model\Enums\Keys\OptionKey; |
| 9 | use FapiMember\Repository\LevelRepository; |
| 10 | use FapiMember\Service\ApiService; |
| 11 | use FapiMember\Service\EmailService; |
| 12 | use FapiMember\Service\LevelService; |
| 13 | use FapiMember\Service\MembershipService; |
| 14 | use FapiMember\Service\UserService; |
| 15 | use Throwable; |
| 16 | use WP_REST_Request; |
| 17 | |
| 18 | class RequestHandler |
| 19 | { |
| 20 | private ApiService $apiService; |
| 21 | private LevelService $levelService; |
| 22 | private LevelRepository $levelRepository; |
| 23 | private MembershipsController $membershipsController; |
| 24 | |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->apiService = Container::get(ApiService::class); |
| 28 | $this->levelService = Container::get(LevelService::class); |
| 29 | $this->levelRepository = Container::get(LevelRepository::class); |
| 30 | $this->membershipsController = Container::get(MembershipsController::class); |
| 31 | } |
| 32 | |
| 33 | public function handleApiSections(WP_REST_Request $request): void |
| 34 | { |
| 35 | $sections = $this->levelRepository->getAllSections(); |
| 36 | $array = []; |
| 37 | |
| 38 | foreach ($sections as $section) { |
| 39 | $array[] = $section->toArray(); |
| 40 | } |
| 41 | |
| 42 | wp_send_json($array); |
| 43 | } |
| 44 | |
| 45 | public function handleApiSectionsSimple(WP_REST_Request $request): void |
| 46 | { |
| 47 | $params = $request->get_query_params(); |
| 48 | $limit = null; |
| 49 | |
| 50 | if (isset($params['limit']) && is_numeric($params['limit'])) { |
| 51 | $limit = (int) $params['limit']; |
| 52 | } |
| 53 | |
| 54 | $sections = $this->levelRepository->getAllSections(); |
| 55 | $simplifiedSections = []; |
| 56 | |
| 57 | $iterator = 0; |
| 58 | |
| 59 | foreach ($sections as $section) { |
| 60 | if ($iterator === $limit) { |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | $iterator++; |
| 65 | |
| 66 | $simplifiedSections[] = [ |
| 67 | 'id' => $section->getId(), |
| 68 | 'name' => $section->getName(), |
| 69 | ]; |
| 70 | |
| 71 | foreach ($section->getLevels() as $level) { |
| 72 | $simplifiedSections[] = [ |
| 73 | 'id' => $level->getId(), |
| 74 | 'name' => $section->getName() . ' - ' . $level->getName(), |
| 75 | ]; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | wp_send_json($simplifiedSections); |
| 80 | } |
| 81 | |
| 82 | public function handleApiCallback(WP_REST_Request $request): void |
| 83 | { |
| 84 | $params = $request->get_params(); |
| 85 | $body = $request->get_body(); |
| 86 | $data = []; |
| 87 | parse_str($body, $data); |
| 88 | |
| 89 | if (isset($params['days'])) { |
| 90 | $data['days'] = $params['days']; |
| 91 | } |
| 92 | |
| 93 | if (isset($params['level'])) { |
| 94 | $data['level'] = $params['level']; |
| 95 | } |
| 96 | |
| 97 | $this->membershipsController->create($data, false); |
| 98 | } |
| 99 | |
| 100 | public function handleApiCheckConnectionCallback(WP_REST_Request $request) { |
| 101 | $body = $request->get_body(); |
| 102 | $data = []; |
| 103 | parse_str($body, $data); |
| 104 | |
| 105 | $token = get_option(OptionKey::TOKEN); |
| 106 | |
| 107 | if (!isset($data['token'])) { |
| 108 | $this->apiService->callbackError([ |
| 109 | 'class' => self::class, |
| 110 | 'description' => 'Missing token.', |
| 111 | ]); |
| 112 | } |
| 113 | |
| 114 | if ($token !== $data['token']) { |
| 115 | $this->apiService->callbackError([ |
| 116 | 'class' => self::class, |
| 117 | 'description' => 'Invalid token provided. Check token correctness.', |
| 118 | ]); |
| 119 | } |
| 120 | |
| 121 | wp_send_json_success(); |
| 122 | } |
| 123 | |
| 124 | public function handleApiUsernamesCallback(WP_REST_Request $request): void |
| 125 | { |
| 126 | $credentials = json_decode(get_option(OptionKey::API_CREDENTIALS)); |
| 127 | foreach ($credentials as $credential) { |
| 128 | $usernames[] = [ |
| 129 | 'label' => $credential->username, |
| 130 | 'value' => $credential->username, |
| 131 | ]; |
| 132 | } |
| 133 | wp_send_json(json_encode($usernames)); |
| 134 | } |
| 135 | |
| 136 | public function handleApiListFormsCallback(WP_REST_Request $request): void |
| 137 | { |
| 138 | $user = (($request->get_param('user') === 'all') |
| 139 | ? 'all' |
| 140 | : is_email($request->get_param('user'))) |
| 141 | ? $request->get_param('user') |
| 142 | : null; |
| 143 | |
| 144 | $forms = []; |
| 145 | $out = []; |
| 146 | $apiClients = $this->apiService->getApiClients(); |
| 147 | |
| 148 | if ($user === 'all' || empty($user)) { |
| 149 | foreach ($apiClients as $apiClient) { |
| 150 | array_push($forms, $apiClient->getForms()); |
| 151 | } |
| 152 | |
| 153 | foreach ($forms as $singleClientForms) { |
| 154 | foreach ($singleClientForms as $form) { |
| 155 | $out[] = array( |
| 156 | 'label' => $form['name'], |
| 157 | 'value' => $form['path'], |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | wp_send_json($out); |
| 163 | exit; |
| 164 | } |
| 165 | |
| 166 | foreach ($apiClients as $apiClient) { |
| 167 | if ($apiClient->getConnection()->getApiUser() === $user) { |
| 168 | $forms = $apiClient->getForms(); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | foreach ($forms as $form) { |
| 174 | $out[] = array( |
| 175 | 'label' => $form['name'], |
| 176 | 'value' => $form['path'], |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | wp_send_json($out); |
| 181 | } |
| 182 | } |
| 183 |