Created
February 11, 2017 16:36
-
-
Save djmarland/40b40cb39167a5889a736fc8f3fa2b79 to your computer and use it in GitHub Desktop.
StatusUpdateCommand.php from https://github.com/hammerspacecouk/tubealert.co.uk-php/blob/master/src/AppBundle/Controller/NotifyController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ConsoleBundle\Command; | |
use DateTime; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use TubeService\Domain\Entity\Line; | |
class StatusUpdateCommand extends Command | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('tube:status:update') | |
->setDescription('Fetch data from TFL and populate the database'); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$output->writeln('Starting...'); | |
$output->writeln('Fetching data from TFL'); | |
$tflLines = $this->getContainer()->get('console.services.tfl')->fetchLineStatuses(); | |
if (!$tflLines) { | |
$output->writeln('Error getting data'); | |
return 1; | |
} | |
// get the current lines and statuses | |
$result = $this->getContainer()->get('console.services.line')->findAllWithStatus(); | |
if ($result->isEmpty()) { | |
$output->writeln('Unable to fetch lines from database'); | |
return 1; | |
} | |
$lines = $result->getDomainModels(); | |
$updateService = $this->getContainer()->get('console.services.update'); | |
foreach ($lines as $line) { | |
$output->writeln('---'); | |
$output->writeln('Checking ' . $line->getName()); | |
$tflLine = $this->getTFLLine($tflLines, $line->getTFLKey()); | |
$latestStatus = $line->getLatestStatus(); | |
$previousStatusTitle = null; // for first population | |
$previousStatusDescriptions = null; | |
if ($latestStatus) { | |
$previousStatusTitle = $latestStatus->getTitle(); | |
$previousStatusDescriptions = $latestStatus->getDescriptions(); | |
} | |
$currentStatusTitle = $tflLine->getStatusTitle(); | |
$currentStatusDescriptions = $tflLine->getStatusDescriptions(); | |
if ( | |
$previousStatusTitle == $currentStatusTitle && | |
$previousStatusDescriptions == $currentStatusDescriptions | |
) { | |
// refresh the "updated" time | |
$updateService->refreshStatus($latestStatus); | |
$output->writeln('Status unchanged. Saving updated time'); | |
continue; | |
} | |
$output->writeln('Status has changed. Saving new status'); | |
// update the statuses | |
$status = $updateService->addNewStatus($line, $tflLine); | |
if ($this->getContainer()->getParameter('enable_notifications')) { | |
$output->writeln('Notifying subscribed users'); | |
$this->notifyUsers($line, $status); | |
// we sent out notifications. Sleep for 2 seconds before sending out more | |
sleep(2); | |
} else { | |
$output->writeln('Notifications disabled'); | |
} | |
} | |
$output->writeln(''); | |
$output->writeln('Done'); | |
return true; | |
} | |
private function notifyUsers(Line $line, $status) | |
{ | |
$subscriptionService = $this->getContainer()->get('console.services.subscription'); | |
$now = new DateTime(); | |
$day = (int) $now->format('N'); | |
$hour = (int) $now->format('H'); | |
$result = $subscriptionService->findAllForLineAndTime( | |
$line, | |
$day, | |
$hour | |
); | |
// split the results by type | |
$subscriptions = $result->getDomainModels(); | |
return $this->notify( | |
$line, | |
$status->getShortTitle(), | |
$subscriptions | |
); | |
} | |
private function getTFLLine($lines, $id) | |
{ | |
foreach($lines as $line) { | |
if ($line->getTFLKey() == $id) { | |
return $line; | |
} | |
} | |
throw new \Exception('Invalid Data'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment