Move the register function from controller to service. #311
This commit is contained in:
parent
74c239cfd6
commit
4d104d38e1
5 changed files with 263 additions and 58 deletions
|
|
@ -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: ~
|
||||
|
|
|
|||
|
|
@ -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,73 +143,23 @@ 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;
|
||||
}
|
||||
|
||||
// return data
|
||||
$data = [
|
||||
'session_id' => $sess->getID()
|
||||
];
|
||||
$res->setData($data);
|
||||
|
||||
}
|
||||
|
||||
// response
|
||||
return $res->getReturnResponse();
|
||||
|
|
|
|||
119
src/Service/RiderAPIHandler/CMBRiderAPIHandler.php
Normal file
119
src/Service/RiderAPIHandler/CMBRiderAPIHandler.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service\RiderAPIHandler;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use App\Ramcar\CMBServiceType;
|
||||
use App\Ramcar\CMBTradeInType;
|
||||
|
||||
use App\Service\RiderAPIHandlerInterface;
|
||||
use App\Service\RedisClientProvider;
|
||||
|
||||
use App\Entity\RiderSession;
|
||||
|
||||
class CMBRiderAPIHandler implements RiderAPIHandlerInterface
|
||||
{
|
||||
protected $em;
|
||||
protected $redis;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
119
src/Service/RiderAPIHandler/ResqRiderAPIHandler.php
Normal file
119
src/Service/RiderAPIHandler/ResqRiderAPIHandler.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service\RiderAPIHandler;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use App\Ramcar\ServiceType;
|
||||
use App\Ramcar\TradeInType;
|
||||
|
||||
use App\Service\RiderAPIHandlerInterface;
|
||||
use App\Service\RedisClientProvider;
|
||||
|
||||
use App\Entity\RiderSession;
|
||||
|
||||
class ResqRiderAPIHandler implements RiderAPIHandlerInterface
|
||||
{
|
||||
protected $em;
|
||||
protected $redis;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
10
src/Service/RiderAPIHandlerInterface.php
Normal file
10
src/Service/RiderAPIHandlerInterface.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
interface RiderAPIHandlerInterface
|
||||
{
|
||||
public function register(Request $req);
|
||||
}
|
||||
Loading…
Reference in a new issue