protected function execute(InputInterface $input, OutputInterface $output) { $date = new \DateTime($input->getArgument('time')); // Get departure/arrival stops $departure = $this->cff->getStop($input->getArgument('departure')); $arrival = $this->cff->getStop($input->getArgument('arrival')); $output->write(sprintf('%s -> %s at %s : ', $departure['value'], $arrival['value'], $date->format('H:s'))); $times = $this->cff->query($departure, $arrival, $date); $times = array_filter($times, function ($value) use($date) { return new \DateTime($value['departure']) == $date; }); if ($infos = current($times)['infos']) { $output->writeln(sprintf('<comment>%s</comment>', $infos)); // Create the Transport $transport = \Swift_SmtpTransport::newInstance($input->getOption('host'), $input->getOption('port'), $input->getOption('security'))->setUsername($input->getOption('username'))->setPassword($input->getOption('password')); $mailer = \Swift_Mailer::newInstance($transport); // Create a message $message = \Swift_Message::newInstance($infos)->setFrom(array($input->getOption('username') => 'CFFie'))->setTo(array($input->getOption('to')))->setBody(sprintf("%s -> %s at %s\nCFFie", $departure['value'], $arrival['value'], $date->format('H:m'))); // Send the message $result = $mailer->send($message); if ($result) { $this->output->success($input->getOption('to') . ' alerted !'); } } else { $output->writeln('<info>ok</info>'); } }
protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $date = new \DateTime($input->getArgument('datetime')); // Get departure/arrival stops $departure = $this->getStop($input->getArgument('departure'), 'Which departure ?'); $arrival = $this->getStop($input->getArgument('arrival'), 'Which arrival ?'); $io->title(sprintf('%s -> %s (%s)', $departure['value'], $arrival['value'], $date->format('d.m.y H:i'))); // Query $times = $this->cff->query($departure, $arrival, $date); // Compute delays $delays = array(); $now = new \DateTime(); foreach ($times as $time) { $diff = $now->diff(\DateTime::createFromFormat('d.m.y H:i', $time['date'] . ' ' . $time['departure'])); $delays[] = (int) ($diff->format('%r') . ($diff->h * 60 + $diff->i)); } // Show output table $formattedTimes = array_map(function ($v, $d) { unset($v['date']); $v = array('in' => $d . '´') + $v; $v['infos'] = '<error>' . $v['infos'] . '</error>'; return $v; }, $times, $delays); $io->table(array('In', 'Dep.', 'Arr.', 'Dur.', 'Chg.', 'With', 'Infos'), $formattedTimes); // Show notification if ($input->getOption('notify')) { $notifier = NotifierFactory::create(); $body = ''; foreach ($times as $t => $time) { $body .= sprintf("%s - %s | %s | %s chg. | %s | in %s´ %s\r\n", $time['departure'], $time['arrival'], $time['duration'], $time['change'], $time['product'], $delays[$t], $time['infos']); } $notification = (new Notification())->setTitle(sprintf("%s -> %s", $departure['value'], $arrival['value']))->setBody($body); $notifier->send($notification); } }