apiUtil = $apiUtil; $this->legacyAdminCredentials = $legacyAdminCredentials; $this->sentryLogger = $sentryLogger; } /** * @Route("/{endpoint}", requirements={"endpoint"=".*"}, methods={"GET", "POST"}) */ public function handleEndpoint(Request $request, string $endpoint): JsonResponse { $endpoint = trim(trim(mb_strtolower($endpoint), '/')); switch ($endpoint) { case 'paauth': return $this->auth($request); case 'pavouchers/tryusevoucher': return $this->tryUseVoucher($request); case 'pavouchers/usevoucher': return $this->useVoucher($request); case 'pavouchers/rechargevoucher': return $this->rechargeVoucher($request); } if (preg_match('/pavouchers\/(?.+)(\/)?$/i', $endpoint, $match)) { if (!empty($match['code'])) { return $this->getVoucher($request, $match['code']); } } return $this->getJsonResponse(['message' => 'Endpoint not found'], 404); } public function auth(Request $request): JsonResponse { $result = $this->apiUtil->auth( $this->getRequestData($request) ); if (!$result) { return $this->getJsonResponse([], 401); } return $this->getJsonResponse($result); } /** * Nacteni informaci o poukazu. */ public function getVoucher(Request $request, string $code): JsonResponse { return $this->doApiCall($request, function (array $data) use ($code) { $data['number'] = $code; $result = $this->apiUtil->useVoucher($data, true, true); if (empty($result['voucher'])) { throw new PompoApiException('Voucher not found', 404); } return $result['voucher']; }); } /** * Chova se skoro stejne jako UseVoucher, jen to nakonci voucher neuplatni, pouze to vrati co by se stalo, kdyby se uplatnil. */ public function tryUseVoucher(Request $request): JsonResponse { return $this->doApiCall($request, function (array $data) { return $this->apiUtil->useVoucher($data, true); }); } /** * Uplatni kupon, pokud je to mozne. */ public function useVoucher(Request $request): JsonResponse { return $this->doApiCall($request, function (array $data) { return $this->apiUtil->useVoucher($data); }); } /** * Zmeni stav voucheru. */ public function rechargeVoucher(Request $request): JsonResponse { return $this->doApiCall($request, function (array $data) { $this->apiUtil->rechargeVoucher($data); return ['success' => 1]; }); } private function doApiCall(Request $request, callable $apiCall): JsonResponse { if (!($auth = $request->headers->get('authorization'))) { return $this->getAuthErrorResponse('Missing authorization header'); } if (!preg_match('/Bearer\s(.*)/', $auth, $matches)) { return $this->getAuthErrorResponse('Missing bearer token'); } $hash = $matches[1] ?? ''; if (!$this->legacyAdminCredentials->loginByHash($hash)) { return $this->getAuthErrorResponse('Given token is not valid'); } try { return $this->getJsonResponse( $apiCall( $this->getRequestData($request) ) ); } catch (PompoApiException $e) { return $this->getJsonResponse( ['message' => $e->getMessage()], $e->getResponseCode() ); } catch (\Throwable $e) { if (isDevelopment()) { throw $e; } $this->sentryLogger->captureException($e); return $this->getJsonResponse( ['message' => 'Some error happened'], 500 ); } } private function getAuthErrorResponse(string $message): JsonResponse { return $this->getJsonResponse(['message' => $message], 401); } private function getJsonResponse(array $data, int $status = 200): JsonResponse { return new JsonResponse($data, $status, [ 'Content-Type' => 'application/json; charset=utf-8', ]); } private function getRequestData(Request $request): array { $data = json_decode($request->getContent() ?? '', true) ?? []; // projdu data a nastavim jim maly pismenko na zacatek, protoze DRS jsou kreteni a posilaji to s velkyma return $this->withLowerArrayKeysRecursive($data); } private function withLowerArrayKeysRecursive(array $data): array { $result = []; foreach ($data as $key => $value) { $newKey = is_numeric($key) ? $key : mb_strtolower($key); if (is_array($value)) { $result[$newKey] = $this->withLowerArrayKeysRecursive($value); } else { $result[$newKey] = $value; } } return $result; } }