resq/src/Service/RiderTracker.php

57 lines
1.3 KiB
PHP

<?php
namespace App\Service;
use App\Entity\Rider;
use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use Predis\Client as PredisClient;
class RiderTracker
{
const RIDER_PREFIX_KEY = 'rider.location';
protected $em;
protected $redis;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
// TODO: make it read redis settings from config
// build a service maybe?
$this->redis = new PredisClient();
}
protected function getRiderKey($rider_id)
{
return self::RIDER_PREFIX_KEY . '.' . $rider_id;
}
public function getRiderLocation($rider_id)
{
$coordinates = new Point(0,0);
$key = $this->getRiderKey($rider_id);
// check redis cache for rider information
if (($this->redis->hexists($key, 'longitude')) &&
($this->redis->hexists($key, 'latitude')))
{
$long = $this->redis->hget($key, 'longitude');
$lat = $this->redis->hget($key, 'latitude');
$coordinates = new Point($long, $lat);
}
else
{
$rider = $this->em->getRepository(Rider::class)->find($rider_id);
$coordinates = $rider->getHub()->getCoordinates();
}
return $coordinates;
}
}