<?php
namespace App\EventListener;
use App\Entity\Vehicle;
use App\Event\VehicleChangedEvent;
use Symfony\Component\Process\Process;
/**
* Listener acting on "VehicleChangedEvents".
*/
class VehicleStatusListener
{
public function __construct(private readonly string $projectDir)
{
}
/**
* Called when a lot is closed but not sold.
*/
public function onUnsold(VehicleChangedEvent $args): void
{
$vehicle = $args->getEntity();
$this->sendAlertSms($vehicle);
if (Vehicle::SALING_STATE_RETIRE === $vehicle->getSalingState() || null !== $vehicle->getWinner()) {
$this->generateFile($vehicle, 'Live');
}
}
/**
* Called when a lot is closed and sold.
*/
public function onSold(VehicleChangedEvent $args): void
{
$vehicle = $args->getEntity();
$this->sendAlertSms($vehicle);
if (Vehicle::SALING_STATE_RETIRE === $vehicle->getSalingState() || null !== $vehicle->getWinner()) {
$this->generateFile($vehicle, 'Live');
}
}
/**
* Called when a lot is reassigned.
*/
public function onReassign(VehicleChangedEvent $args): void
{
$vehicle = $args->getEntity();
$this->generateFile($vehicle, 'Affectation');
}
protected function sendAlertSms(Vehicle $vehicle)
{
$process = Process::fromShellCommandline('nohup php '.$this->projectDir.'/bin/console vpauto:live:sms:prealert '.$vehicle->getId().' --env=prod > /dev/null 2>&1 &');
$process->run();
}
protected function generateFile(Vehicle $vehicle, $prefix)
{
$process = Process::fromShellCommandline(sprintf('nohup php %s/bin/console vpauto:sap:adjudication:file vehicle %s %s --env=prod > /dev/null 2>&1 &', $this->projectDir, $vehicle->getId(), $prefix));
$process->run();
}
}