From 590b9b82b475d9c517a6cb2f5815b95929abd593 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 22 Oct 2019 05:51:43 +0000 Subject: [PATCH 1/4] Add geofence variable to env.dist. Add checking to APIController if geofence is on or not. #271 --- .env.dist | 3 +++ src/Controller/APIController.php | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.env.dist b/.env.dist index 4e10dffc..87f679ac 100644 --- a/.env.dist +++ b/.env.dist @@ -42,3 +42,6 @@ POLICY_MOBILE=insertmobilepolicyidhere # OTP OTP_MODE=settotestorrandom + +# geofence +GEOFENCE=settotrueorfalse diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 075879bb..aefb8792 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -839,18 +839,24 @@ class APIController extends Controller $long = $req->request->get('long'); $lat = $req->request->get('lat'); - /* // geofence - $is_covered = $geo->isCovered($long, $lat); - if (!$is_covered) - { - // TODO: put geofence error message in config file somewhere - $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to Metro Manila only. We will update you as soon as we are able to cover your area'); - return $res->getReturnResponse(); - } - */ + // check if geofence is on + $dotenv = new Dotenv(); + $dotenv->loadEnv(__DIR__.'/../../.env'); + $geo_flag = $_ENV['GEOFENCE']; + + if ($geo_flag == 'true') + { + $is_covered = $geo->isCovered($long, $lat); + if (!$is_covered) + { + // TODO: put geofence error message in config file somewhere + $res->setError(true) + ->setErrorMessage('Oops! Our service is limited to Metro Manila, Baguio, and Laguna only. We will update you as soon as we are able to cover your area'); + return $res->getReturnResponse(); + } + } $jo = new JobOrder(); $jo->setSource(TransactionOrigin::MOBILE_APP) From e169766efd91bd493db8b92b62688257a49be0b3 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 22 Oct 2019 07:07:03 +0000 Subject: [PATCH 2/4] Modify the error message if delivery is not in the coverage areas. #271 --- src/Controller/APIController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index aefb8792..225f81c8 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -853,7 +853,7 @@ class APIController extends Controller { // TODO: put geofence error message in config file somewhere $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to Metro Manila, Baguio, and Laguna only. We will update you as soon as we are able to cover your area'); + ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); return $res->getReturnResponse(); } } From f6dce241ac999ca33ec66db4fd7d7806efb93876 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 23 Oct 2019 05:30:12 +0000 Subject: [PATCH 3/4] Move retrieval of env variable to the geofence service. #271 --- .env.dist | 2 +- config/services.yaml | 4 ++++ src/Controller/APIController.php | 21 ++++++--------------- src/Service/GeofenceTracker.php | 28 ++++++++++++++++++---------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/.env.dist b/.env.dist index 87f679ac..ca93d2d2 100644 --- a/.env.dist +++ b/.env.dist @@ -44,4 +44,4 @@ POLICY_MOBILE=insertmobilepolicyidhere OTP_MODE=settotestorrandom # geofence -GEOFENCE=settotrueorfalse +GEOFENCE_ENABLE=settotrueorfalse diff --git a/config/services.yaml b/config/services.yaml index 8c8024f5..c6f2f877 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -87,6 +87,10 @@ services: $password: "%env(REDIS_CLIENT_PASSWORD)%" $env_flag: "dev" + App\Service\GeofenceTracker: + arguments: + $geofence_flag: "%env(GEOFENCE_ENABLE)%" + Catalyst\APIBundle\Security\APIKeyUserProvider: arguments: $em: "@doctrine.orm.entity_manager" diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 225f81c8..f8a247cd 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -840,22 +840,13 @@ class APIController extends Controller $lat = $req->request->get('lat'); // geofence - // check if geofence is on - $dotenv = new Dotenv(); - $dotenv->loadEnv(__DIR__.'/../../.env'); - - $geo_flag = $_ENV['GEOFENCE']; - - if ($geo_flag == 'true') + $is_covered = $geo->isCovered($long, $lat); + if (!$is_covered) { - $is_covered = $geo->isCovered($long, $lat); - if (!$is_covered) - { - // TODO: put geofence error message in config file somewhere - $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); - return $res->getReturnResponse(); - } + // TODO: put geofence error message in config file somewhere + $res->setError(true) + ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); + return $res->getReturnResponse(); } $jo = new JobOrder(); diff --git a/src/Service/GeofenceTracker.php b/src/Service/GeofenceTracker.php index b2052e02..5acc7564 100644 --- a/src/Service/GeofenceTracker.php +++ b/src/Service/GeofenceTracker.php @@ -11,25 +11,33 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point; class GeofenceTracker { protected $em; + protected $geofence_flag; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, $geofence_flag) { $this->em = $em; + $this->geofence_flag = $geofence_flag; } public function isCovered($long, $lat) { - // see if the point is in any of the polygons - $query = $this->em->createQuery('SELECT count(s) from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true') - ->setParameter('long', $long) - ->setParameter('lat', $lat); + // check if geofence is enabled + if ($this->geofence_flag == 'true') + { + // see if the point is in any of the polygons + $query = $this->em->createQuery('SELECT count(s) from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true') + ->setParameter('long', $long) + ->setParameter('lat', $lat); - // number of polygons that contain the point - $count = $query->getSingleScalarResult(); + // number of polygons that contain the point + $count = $query->getSingleScalarResult(); - if ($count > 0) - return true; + if ($count > 0) + return true; - return false; + return false; + } + + return true; } } From ed0486fbf59ffa2e062c79f1c98335caee270fb2 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 23 Oct 2019 05:58:05 +0000 Subject: [PATCH 4/4] Improve geofence error message. #271 --- src/Controller/APIController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index f8a247cd..75dcb9d0 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -845,7 +845,7 @@ class APIController extends Controller { // TODO: put geofence error message in config file somewhere $res->setError(true) - ->setErrorMessage('Oops! Our service is limited to some areas in Laguna and Baguio, and Metro Manila only. We will update you as soon as we are able to cover your area'); + ->setErrorMessage('Oops! Our service is limited to some areas in Metro Manila, Laguna, and Baguio only. We will update you as soon as we are able to cover your area'); return $res->getReturnResponse(); }