From 9d8a3fe6e4371b3e84fa3625caaa1429bcf283ae Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Tue, 22 May 2018 20:59:24 +0800 Subject: [PATCH] Add session register rider api call #119 --- config/routes/rider_api.yaml | 22 +++ src/Controller/RAPIController.php | 215 ++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 config/routes/rider_api.yaml create mode 100644 src/Controller/RAPIController.php diff --git a/config/routes/rider_api.yaml b/config/routes/rider_api.yaml new file mode 100644 index 00000000..95d5563c --- /dev/null +++ b/config/routes/rider_api.yaml @@ -0,0 +1,22 @@ +# rider app api + +rapi_register: + path: /rapi/register + controller: App\Controller\RAPIController::register + methods: [POST] + +rapi_login: + path: /rapi/login + controller: App\Controller\RAPIController::login + methods: [POST] + +rapi_get_status: + path: /rapi/status + controller: App\Controller\RAPIController::getStatus + methods: [GET] + +rapi_set_status: + path: /rapi/status + controller: App\Controller\RAPIController::setStatus + methods: [POST] + diff --git a/src/Controller/RAPIController.php b/src/Controller/RAPIController.php new file mode 100644 index 00000000..1f69a286 --- /dev/null +++ b/src/Controller/RAPIController.php @@ -0,0 +1,215 @@ +session = null; + } + + protected function checkMissingParameters(Request $req, $params = []) + { + $missing = []; + + // check if parameters are there + foreach ($params as $param) + { + if ($req->getMethod() == 'GET') + { + $check = $req->query->get($param); + if (empty($check)) + $missing[] = $param; + } + else if ($req->getMethod() == 'POST') + { + $check = $req->request->get($param); + if (empty($check)) + $missing[] = $param; + } + else + return $params; + } + + return $missing; + } + + // TODO: type hint entity manager + protected function checkAPIKey($em, $api_key) + { + // find the api key (session id) + $session = $em->getRepository(RiderSession::class)->find($api_key); + if ($session == null) + return null; + + return $session; + } + + protected function checkParamsAndKey(Request $req, $em, $params) + { + // returns APIResult object + $res = new APIResult(); + + // check for api_key in query string + $api_key = $req->query->get('api_key'); + if (empty($api_key)) + { + $res->setError(true) + ->setErrorMessage('Missing API key'); + return $res; + } + + // check missing parameters + $missing = $this->checkMissingParameters($req, $params); + if (count($missing) > 0) + { + $miss_string = implode(', ', $missing); + $res->setError(true) + ->setErrorMessage('Missing parameter(s): ' . $miss_string); + return $res; + } + + // check api key + $sess = $this->checkAPIKey($em, $req->query->get('api_key')); + if ($sess == null) + { + $res->setError(true) + ->setErrorMessage('Invalid API Key'); + return $res; + } + + // store session + $this->session = $sess; + + return $res; + } + + public function register(Request $req) + { + $res = new APIResult(); + + // confirm parameters + $required_params = [ + 'phone_number', + 'device_push_id' + ]; + + $missing = $this->checkMissingParameters($req, $required_params); + if (count($missing) > 0) + { + $params = implode(', ', $missing); + $res->setError(true) + ->setErrorMessage('Missing parameter(s): ' . $params); + return $res->getReturnResponse(); + } + + $em = $this->getDoctrine()->getManager(); + + // retry until we get a unique id + while (true) + { + try + { + // instantiate session + $sess = new RiderSession(); + $sess->setPhoneNumber($req->request->get('phone_number')) + ->setDevicePushID($req->request->get('device_push_id')); + + // reopen in case we get an exception + if (!$em->isOpen()) + { + $em = $em->create( + $em->getConnection(), + $em->getConfiguration() + ); + } + + // save + $em->persist($sess); + $em->flush(); + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + + break; + } + + // return data + $data = [ + 'session_id' => $sess->getID() + ]; + $res->setData($data); + + + // response + return $res->getReturnResponse(); + } + + public function login(Request $req) + { + $required_params = [ + 'user', + 'pass', + ]; + $em = $this->getDoctrine()->getManager(); + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + return $res->getReturnResponse(); + } + + public function getStatus() + { + } + + public function setStatus(Request $req) + { + } +}