src/AppBundle/Controller/JobSearchController.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace AppBundle\Controller;
  4. use AppBundle\Service\Geocoding\GeocodingSearchService;
  5. use Exception;
  6. use Pimcore\Db;
  7. use Pimcore\Model\DataObject\JobOffer\Listing;
  8. use Pimcore\Translation\Translator;
  9. use Symfony\Component\HttpFoundation\Request;
  10. class JobSearchController extends BaseController
  11. {
  12.     private GeocodingSearchService $searchService;
  13.     public function __construct(GeocodingSearchService $searchService)
  14.     {
  15.         $this->searchService $searchService;
  16.     }
  17.     public function defaultAction(Request $requestTranslator $translator): array
  18.     {
  19.         $radius = (int) $request->get('radius');
  20.         $job $request->get('job');
  21.         $city $request->get('location');
  22.         $result = [];
  23.         if ($city && $radius) {
  24.             $result $this->searchJobOffersWithLocation($job$city$radius$translator);
  25.         } elseif ($job && !$city) {
  26.             $result $this->searchJobOffers($job$translator);
  27.         } else {
  28.             $result['message'] = $translator->trans('app.general.gemini.career.search.error.nocriteria');
  29.         }
  30.         return [
  31.             'job' => $job,
  32.             'city' => $city,
  33.             'radius' => $radius,
  34.             'jobOffers' => $result['jobOffers'] ?? null,
  35.             'message' => $result['message'] ?? null,
  36.         ];
  37.     }
  38.     protected function searchJobOffers(string $jobTranslator $translator): array
  39.     {
  40.         $jobOffers = new Listing();
  41.         $jobOffers->setOrderKey('title');
  42.         $jobOffers->setOrder('asc');
  43.         $jobOffers->setCondition('title LIKE :title', ['title' => '%' $job '%']);
  44.         if (== $jobOffers->getTotalCount()) {
  45.             $jobOffers null;
  46.             $message $translator->trans('app.general.gemini.career.search.error.noresults');
  47.         }
  48.         return [
  49.             'jobOffers' => $jobOffers,
  50.             'message' => $message,
  51.         ];
  52.     }
  53.     protected function searchJobOffersWithLocation(string $jobstring $cityint $radiusTranslator $translator): array
  54.     {
  55.         $jobOffers null;
  56.         $message null;
  57.         try {
  58.             $cords $this->getCityCords($city$translator);
  59.             $resultsArray $this->queryJobs($cords[0], $cords[1], $radius$job$translator);
  60.             $ids $this->extractIds($resultsArray$translator);
  61.             $jobOffers $this->getJobOffers($ids$translator);
  62.         } catch (Exception $ex) {
  63.             $message $ex->getMessage();
  64.         }
  65.         return [
  66.             'jobOffers' => $jobOffers,
  67.             'message' => $message,
  68.         ];
  69.     }
  70.     /**
  71.      * @throws Exception
  72.      */
  73.     protected function queryJobs(float $latfloat $lonint $radiusstring $job '', ?Translator $translator null): array
  74.     {
  75.         $distanceString '( 6371 * acos( cos( radians(' $lat ') ) * cos( radians( coordinates__latitude ) ) * cos( radians( coordinates__longitude ) - radians(' $lon ') ) + sin( radians(' $lat ') ) * sin( radians( coordinates__latitude ) ) ) )';
  76.         $sql 'SELECT oo_id, coordinates__latitude, title, coordinates__longitude, ' $distanceString ' AS distance FROM object_query_1 '
  77.                 "WHERE title LIKE '%" $job "%' "
  78.                 'AND ' $distanceString ' < ' $radius ' ';
  79.         $db Db::get();
  80.         try {
  81.             $results $db->fetchAll($sql);
  82.         } catch (Exception $ex) {
  83.             throw new Exception($translator->trans('app.general.gemini.career.search.error.searcherror1'), $ex->getCode(), $ex);
  84.         }
  85.         return $results;
  86.     }
  87.     /**
  88.      * @throws Exception
  89.      */
  90.     protected function getCityCords(string $cityTranslator $translator): array
  91.     {
  92.         $result $this->searchService->search($city);
  93.         if (empty($result['error'])) {
  94.             return $result;
  95.         }
  96.         throw new Exception($translator->trans('app.general.gemini.career.search.error.searcherror2'));
  97.     }
  98.     /**
  99.      * @throws Exception
  100.      */
  101.     protected function extractIds(array $arrayTranslator $translator): array
  102.     {
  103.         if ([] === $array) {
  104.             throw new Exception($translator->trans('app.general.gemini.career.search.error.noresults'));
  105.         }
  106.         $res = [];
  107.         foreach ($array as $item) {
  108.             $res[] = $item['oo_id'];
  109.         }
  110.         return $res;
  111.     }
  112.     /**
  113.      * @return array
  114.      * @throws Exception
  115.      */
  116.     protected function getJobOffers(array $idsArrayTranslator $translator): Listing
  117.     {
  118.         if ([] === $idsArray) {
  119.             throw new Exception($translator->trans('app.general.gemini.career.search.error.noresults'));
  120.         }
  121.         $jobOffers = new Listing();
  122.         $jobOffers->setOrderKey('title');
  123.         $jobOffers->setOrder('asc');
  124.         $jobOffers->setCondition('oo_id IN (:ids)', ['ids' => $idsArray]);
  125.         return $jobOffers;
  126.     }
  127. }