From 4d104d38e1198540a7391c300a0174b288ca3a51 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 30 Jan 2020 07:47:34 +0000 Subject: [PATCH] Move the register function from controller to service. #311 --- config/services.yaml | 6 + src/Controller/RAPIController.php | 67 ++-------- .../RiderAPIHandler/CMBRiderAPIHandler.php | 119 ++++++++++++++++++ .../RiderAPIHandler/ResqRiderAPIHandler.php | 119 ++++++++++++++++++ src/Service/RiderAPIHandlerInterface.php | 10 ++ 5 files changed, 263 insertions(+), 58 deletions(-) create mode 100644 src/Service/RiderAPIHandler/CMBRiderAPIHandler.php create mode 100644 src/Service/RiderAPIHandler/ResqRiderAPIHandler.php create mode 100644 src/Service/RiderAPIHandlerInterface.php diff --git a/config/services.yaml b/config/services.yaml index 5364d19a..b818c40f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -196,6 +196,12 @@ services: # rider assignment interface App\Service\RiderAssignmentHandlerInterface: "@App\\Service\\RiderAssignmentHandler\\CMBRiderAssignmentHandler" + # rider API service + App\Service\RiderAPIHandler\CMBRiderAPIHandler: ~ + + # rider API interface + App\Service\RiderAPIHandlerInterface: "@App\\Service\\RiderAPIHandler\\CMBRiderAPIHandler" + # map manager #App\Service\GISManager\Bing: ~ App\Service\GISManager\OpenStreet: ~ diff --git a/src/Controller/RAPIController.php b/src/Controller/RAPIController.php index 0097f202..746153ba 100644 --- a/src/Controller/RAPIController.php +++ b/src/Controller/RAPIController.php @@ -32,6 +32,7 @@ use App\Service\MQTTClient; use App\Service\WarrantyHandler; use App\Service\RedisClientProvider; use App\Service\RiderCache; +use App\Service\RiderAPIHandlerInterface; use App\Entity\RiderSession; use App\Entity\Customer; @@ -142,74 +143,24 @@ class RAPIController extends Controller return $res; } - public function register(Request $req, RedisClientProvider $redis) + public function register(Request $req, RiderAPIHandlerInterface $rapi_handler) { $res = new APIResult(); - // confirm parameters - $required_params = [ - 'phone_number', - 'device_push_id' - ]; + $data = $rapi_handler->register($req); - $missing = $this->checkMissingParameters($req, $required_params); - if (count($missing) > 0) + if (isset($data['error'])) { - $params = implode(', ', $missing); + $message = $data['error']; + $res->setError(true) - ->setErrorMessage('Missing parameter(s): ' . $params); - return $res->getReturnResponse(); + ->setErrorMessage($message); } - - $em = $this->getDoctrine()->getManager(); - - // retry until we get a unique id - while (true) + else { - 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(); - - // create redis entry for the session - $redis_client = $redis->getRedisClient(); - $redis_key = 'rider.id.' . $sess->getID(); - error_log('redis_key: ' . $redis_key); - $redis_client->set($redis_key, ''); - } - catch (DBALException $e) - { - error_log($e->getMessage()); - // delay one second and try again - sleep(1); - continue; - } - - break; + $res->setData($data); } - // return data - $data = [ - 'session_id' => $sess->getID() - ]; - $res->setData($data); - - // response return $res->getReturnResponse(); } diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php new file mode 100644 index 00000000..8a964fc3 --- /dev/null +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -0,0 +1,119 @@ +em = $em; + $this->redis = $redis; + } + + public function register(Request $req) + { + // confirm parameters + $required_params = [ + 'phone_number', + 'device_push_id' + ]; + + $missing = $this->checkMissingParameters($req, $required_params); + if (count($missing) > 0) + { + $params = implode(', ', $missing); + $data = [ + 'error' => 'Missing parameter(s): ' . $params + ]; + return $data; + } + + // 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 (!$this->em->isOpen()) + { + $this->em = $this->em->create( + $this->em->getConnection(), + $this->em->getConfiguration() + ); + } + + // save + $this->em->persist($sess); + $this->em->flush(); + + // create redis entry for the session + $redis_client = $this->redis->getRedisClient(); + $redis_key = 'rider.id.' . $sess->getID(); + error_log('redis_key: ' . $redis_key); + $redis_client->set($redis_key, ''); + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + + break; + } + + // return data + $data = [ + 'session_id' => $sess->getID() + ]; + + return $data; + } + + 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 ($check == null) + $missing[] = $param; + } + else if ($req->getMethod() == 'POST') + { + $check = $req->request->get($param); + if ($check == null) + $missing[] = $param; + } + else + return $params; + } + + return $missing; + } + +} diff --git a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php new file mode 100644 index 00000000..c1f81959 --- /dev/null +++ b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php @@ -0,0 +1,119 @@ +em = $em; + $this->redis = $redis; + } + + public function register(Request $req) + { + // confirm parameters + $required_params = [ + 'phone_number', + 'device_push_id' + ]; + + $missing = $this->checkMissingParameters($req, $required_params); + if (count($missing) > 0) + { + $params = implode(', ', $missing); + $data = [ + 'error' => 'Missing parameter(s): ' . $params + ]; + return $data; + } + + // 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 (!$this->em->isOpen()) + { + $this->em = $this->em->create( + $this->em->getConnection(), + $this->em->getConfiguration() + ); + } + + // save + $this->em->persist($sess); + $this->em->flush(); + + // create redis entry for the session + $redis_client = $this->redis->getRedisClient(); + $redis_key = 'rider.id.' . $sess->getID(); + error_log('redis_key: ' . $redis_key); + $redis_client->set($redis_key, ''); + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + + break; + } + + // return data + $data = [ + 'session_id' => $sess->getID() + ]; + + return $data; + } + + 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 ($check == null) + $missing[] = $param; + } + else if ($req->getMethod() == 'POST') + { + $check = $req->request->get($param); + if ($check == null) + $missing[] = $param; + } + else + return $params; + } + + return $missing; + } + +} diff --git a/src/Service/RiderAPIHandlerInterface.php b/src/Service/RiderAPIHandlerInterface.php new file mode 100644 index 00000000..c3ff85c0 --- /dev/null +++ b/src/Service/RiderAPIHandlerInterface.php @@ -0,0 +1,10 @@ +