resq/src/Service/RiderAssignmentHandler/CMBRiderAssignmentHandler.php
2023-06-13 20:33:58 +08:00

111 lines
3.4 KiB
PHP

<?php
namespace App\Service\RiderAssignmentHandler;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RiderAssignmentHandlerInterface;
use App\Service\MQTTClient;
use App\Service\MQTTClientApiv2;
use App\Service\FCMSender;
use App\Service\APNSClient;
use App\Entity\JobOrder;
use App\Entity\Rider;
use App\Ramcar\JOStatus;
class CMBRiderAssignmentHandler implements RiderAssignmentHandlerInterface
{
protected $em;
protected $aclient;
protected $mclient;
protected $mclientv2;
protected $fcmclient;
public function __construct(EntityManagerInterface $em, MQTTClient $mclient, MQTTClientApiv2 $mclientv2, FCMSender $fcmclient, APNSClient $aclient)
{
$this->em = $em;
$this->mclient = $mclient;
$this->mclientv2 = $mclientv2;
$this->fcmclient = $fcmclient;
$this->aclient = $aclient;
}
// assign job order to rider
public function assignJobOrder(JobOrder $obj, Rider $rider)
{
// create the payload
$payload = [
'event' => 'driver_assigned'
];
// send event
$this->mclient->sendEvent($obj, $payload);
// NOTE: for resq2 app
$this->mclientv2->sendEvent($obj, $payload);
$this->fcmclient->sendJoEvent($obj, "jo_fcm_title_driver_assigned", "jo_fcm_body_driver_assigned");
// check if rider is available
if ($rider->isAvailable())
{
error_log('set rider availability to false');
// set rider to unavailable
$rider->setAvailable(false);
// send event to rider
$this->mclient->sendRiderEvent($obj, $payload);
// send push notification
$this->aclient->sendPush($obj, "A RESQ rider is on his way to you.");
}
}
// complete job order
public function fulfillJobOrder(JobOrder $obj, string $image_url, Rider $rider)
{
// send to mqtt
$payload = [
'event' => 'fulfilled',
'jo_id' => $obj->getID(),
'driver_image' => $image_url,
'driver_name' => $rider->getFullName(),
'driver_id' => $rider->getID(),
];
$this->mclient->sendEvent($obj, $payload);
// NOTE: for resq2 app
$this->mclientv2->sendEvent($obj, $payload);
$this->fcmclient->sendJoEvent($obj, "jo_fcm_title_fulfilled", "jo_fcm_body_fulfilled");
// send fulfill/complete event to rider
$this->mclient->sendRiderEvent($obj, $payload);
// search for the JO assigned to rider with JOStatus::ASSIGNED and sort by assign date
$jo_results = $this->em->getRepository(JobOrder::class)->findBy(['status' => JOStatus::ASSIGNED, 'rider' => $rider->getID()],
['date_assign' => 'ASC']);
// check if jo_results is empty
if (!empty($jo_results))
{
error_log('rider has another JO in queue');
// get first entry
$jo = current($jo_results);
// form the payload for the next job order
$jo_payload = [
'event' => 'driver_assigned'
];
// set rider to unavailable
$rider->setAvailable(false);
// send event to rider
$this->mclient->sendRiderEvent($jo, $jo_payload);
// send push notification
$this->aclient->sendPush($obj, "A RESQ rider is on his way to you.");
}
}
}