98 lines
2.1 KiB
PHP
98 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\Service\RedisClientProvider;
|
|
use App\Entity\Rider;
|
|
|
|
class RiderCache
|
|
{
|
|
protected $redis;
|
|
protected $loc_key;
|
|
protected $status_key;
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis_prov, $loc_key, $status_key)
|
|
{
|
|
$this->redis = $redis_prov->getRedisClient();
|
|
$this->loc_key = $loc_key;
|
|
$this->status_key = $status_key;
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function addActiveRider($id, $lat, $lng)
|
|
{
|
|
$this->redis->geoadd(
|
|
$this->loc_key,
|
|
$lng,
|
|
$lat,
|
|
$id
|
|
);
|
|
}
|
|
|
|
public function getAllActiveRiders()
|
|
{
|
|
$all_riders = $this->redis->georadius(
|
|
$this->loc_key,
|
|
0,
|
|
0,
|
|
41000,
|
|
'km',
|
|
['WITHCOORD' => true]
|
|
);
|
|
|
|
$locs = [];
|
|
foreach ($all_riders as $data)
|
|
{
|
|
$id = $data[0];
|
|
$lng = $data[1][0];
|
|
$lat = $data[1][1];
|
|
|
|
// get rider details so we can check for availability
|
|
$rider = $this->getRiderDetails($id);
|
|
if ($rider != null)
|
|
{
|
|
$locs[$id] = [
|
|
'longitude' => $lng,
|
|
'latitude' => $lat,
|
|
];
|
|
}
|
|
}
|
|
|
|
// error_log(print_r($all_riders, true));
|
|
return $locs;
|
|
}
|
|
|
|
public function removeActiveRider($id)
|
|
{
|
|
$this->redis->zrem(
|
|
$this->loc_key,
|
|
$id
|
|
);
|
|
}
|
|
|
|
public function incJobOrderCount($id, $status)
|
|
{
|
|
$this->redis->hincrby($this->status_key, $id, 1);
|
|
}
|
|
|
|
public function decJobOrderCount($id, $status)
|
|
{
|
|
$this->redis->hincrby($this->status_key, $id, -1);
|
|
}
|
|
|
|
protected function getRiderDetails($id)
|
|
{
|
|
$rider = $this->em->getRepository(Rider::class)->find($id);
|
|
if ($rider == null)
|
|
return null;
|
|
|
|
// return only if available
|
|
if ($rider->isAvailable())
|
|
return $rider;
|
|
|
|
return null;
|
|
}
|
|
}
|