Create rider tracker service. Create a test command to test rider tracker service. #180

This commit is contained in:
Korina Cordero 2019-04-23 11:02:12 +00:00
parent d1b41ca36c
commit 76b1f07feb
2 changed files with 104 additions and 0 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Service\RiderTracker;
class TestRiderTrackerCommand extends Command
{
protected function configure()
{
$this->setName('test:ridertracker')
->setDescription('Test the rider tracker service')
->setHelp('Test the rider tracker service.')
->addArgument('rider_id', InputArgument::REQUIRED, 'Rider ID');
}
public function __construct(RiderTracker $rtracker)
{
$this->rtracker = $rtracker;
parent::__construct();
}
public function execute(InputInterface $input, OutputInterface $output)
{
$rider_id = $input->getArgument('rider_id');
$coordinates = $this->rtracker->getRiderLocation($rider_id);
if ($coordinates != null)
{
echo "Rider Location: " . $coordinates->getLongitude() . "," . $coordinates->getLatitude() . "\n";
}
else
{
echo "Invalid rider id." . "\n";
}
}
}

View file

@ -0,0 +1,58 @@
<?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)
{
// check if rider id exists or is valid
$rider = $this->em->getRepository(Rider::class)->find($rider_id);
if ($rider != null)
{
$coordinates = $rider->getHub()->getCoordinates();
$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);
}
return $coordinates;
}
}
}