resq/src/Command/WarrantySMSCommand.php

119 lines
3.4 KiB
PHP

<?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 Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RisingTideGateway;
use App\Entity\Warranty;
use DateTime;
class WarrantySMSCommand extends Command
{
protected $gateway;
protected $em;
protected $translator;
protected function configure()
{
$this->setName('warranty:sms')
->setDescription('Sends an SMS message to users whose warranty expired 45 days ago.')
->setHelp('Sends warranty SMS.')
->addArgument('date', InputArgument::OPTIONAL, 'Date to use as basis of expiration. Defaults to current date.');
}
public function __construct(EntityManagerInterface $em, RisingTideGateway $gateway, TranslatorInterface $translator)
{
$this->em = $em;
$this->gateway = $gateway;
$this->translator = $translator;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$date_string = $input->getArgument('date');
// if date is specified
if (!empty($date_string))
$date = DateTime::createFromFormat('Ymd', $date_string);
else
$date = new DateTime();
// -1 month
$date->modify('-45 day');
$warrs = $this->em->getRepository(Warranty::class)->findBy(['date_expire' => $date]);
$valid_numbers = [];
foreach ($warrs as $w)
{
error_log($w->getID() . ' - ' . $w->getMobileNumber());
// check valid number
$num = trim($w->getMobileNumber());
// should be 10 digits
if (strlen($num) != 10)
continue;
// should start with '9'
if ($num[0] != '9')
continue;
// should be numeric
if (!is_numeric($num))
continue;
// should not be 9900000000
if ($num == '9900000000')
continue;
// add 63 prefix
$num = '63' . $num;
// get battery model and size
$bsize = $w->getBatterySize();
$bmodel = $w->getBatteryModel();
if ($bsize == null || $bmodel == null)
$batt = '';
else
$batt = $bsize->getName() . ' ' . $bmodel->getName();
$valid_numbers[] = [
'number' => $num,
'name' => $w->getFirstName(),
'plate' => $w->getPlateNumber(),
'batt' => $batt,
];
}
error_log(print_r($valid_numbers, true));
foreach ($valid_numbers as $wdata)
{
$msg = 'Hi ' . $wdata['name'] . ', the warranty for the ' . $wdata['batt'] . ' installed in your car(' . $wdata['plate'] . $this->translator->trans('message.partial_warrantysms');
error_log($wdata['number'] . ' - sending ' . $msg);
$this->gateway->sendSMS($wdata['number'], $this->translator->trans('message.battery_brand_allcaps'), $msg);
}
/*
error_log('sending sms to ' . $number);
$msg = 'This is a test.';
$this->gateway->sendSMS($number, 'MOTOLITE', $msg);
return 0;
*/
}
}