diff --git a/src/Service/HubFilter/BaseHubFilter.php b/src/Service/HubFilter/BaseHubFilter.php index dbc8c699..7ad6c0b2 100644 --- a/src/Service/HubFilter/BaseHubFilter.php +++ b/src/Service/HubFilter/BaseHubFilter.php @@ -72,33 +72,6 @@ class BaseHubFilter @file_put_contents(__DIR__ . $filename, $entry, FILE_APPEND); } - public function isExemptedByArea(): bool - { - $point = $this->crit->getPoint(); - $long = $point->getLongitude(); - $lat = $point->getLatitude(); - - // get supported area given a set of coordinates - $query = $this->em->createQuery('SELECT s from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true'); - $area = $query->setParameter('long', $long) - ->setParameter('lat', $lat) - ->setMaxResults(1) - ->getOneOrNullResult(); - - if ($area !== null) { - // get all exceptions - $exceptions = $area->getHubFilterExceptions(); - - if (isset($exceptions[$this->id])) { - // disable this filter for this area - return true; - } - } - - // filter is in place - return false; - } - protected function createRejectionEntry($hub, $reason, $remarks = ""): JORejection { $jo_id = $this->crit->getJobOrderId(); diff --git a/src/Service/HubSelector.php b/src/Service/HubSelector.php index 66fce2e9..c0ed4b8a 100644 --- a/src/Service/HubSelector.php +++ b/src/Service/HubSelector.php @@ -16,6 +16,7 @@ use App\Service\HubFilterLogger; use App\Service\RisingTideGateway; use App\Ramcar\HubCriteria; +use App\Service\HubFilter\HubFilterInterface; class HubSelector { @@ -50,7 +51,7 @@ class HubSelector return $enabled_filters; } - public function find(HubCriteria $criteria) + public function find(HubCriteria $criteria): array { // get all the hubs $hubs = $this->getHubList($criteria->getPoint()); @@ -58,6 +59,9 @@ class HubSelector // get customer record $cust = $this->em->getRepository(Customer::class)->find($criteria->getCustomerId()); + // get all areas that cover the JO location + $areas = $this->getAreaCoverage($criteria); + // loop through all enabled filters foreach ($this->getActiveFilters() as $hub_filter) { // no hubs left to filter @@ -65,15 +69,16 @@ class HubSelector break; } + // initialize the filter $f = $this->container->get($hub_filter); $f->initialize($criteria, $cust); // check if supported area is exempted from this filter - if ($f->isExemptedByArea()) { + if ($this->isExemptedByArea($areas, $f)) { continue; } - // filter hub list + // run the filter $hubs = $f->filter($hubs, $criteria); } @@ -82,6 +87,50 @@ class HubSelector return $hubs; } + protected function isExemptedByArea(array $areas, HubFilterInterface $filter): bool + { + $is_exempted = false; + + if (!empty($areas)) { + // check if at least one area has this filter enabled + $has_support = false; + + foreach ($areas as $area) { + // get all exceptions + $exceptions = $area->getHubFilterExceptions(); + + // if any area has this filter enabled, consider it enabled and move on + if (!isset($exceptions[$filter->getID()])) { + $has_support = true; + break; + } + } + + // none of the areas have this filter enabled, consider it exempted + if (!$has_support) { + error_log("skipping filter " . $filter->getID() . " due to exempted area"); + $is_exempted = true; + } + } + + // filter is in place + return $is_exempted; + } + + protected function getAreaCoverage(HubCriteria $criteria): array + { + $point = $criteria->getPoint(); + $long = $point->getLongitude(); + $lat = $point->getLatitude(); + + // get supported area given a set of coordinates + $query = $this->em->createQuery('SELECT s from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true'); + + return $query->setParameter('long', $long) + ->setParameter('lat', $lat) + ->getResult(); + } + protected function getHubList(Point $point): array { // get closest hubs based on st_distance function from db