resq/src/Service/RiderTracker.php

55 lines
1.3 KiB
PHP

<?php
namespace App\Service;
use App\Entity\Rider;
use Doctrine\ORM\EntityManagerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
class RiderTracker
{
const RIDER_PREFIX_KEY = 'rider.location';
protected $em;
protected $redis;
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis_client)
{
$this->em = $em;
$this->redis = $redis_client->getRedisClient();
}
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');
return new Point($long, $lat);
}
// not in cache, get hub
$rider = $this->em->getRepository(Rider::class)->find($rider_id);
$hub = $rider->getHub();
// no hub
// TODO: return valid coordinate
if ($hub == null)
return new Point(0, 0);
return $hub->getCoordinates();
}
}