Newscoop\Tools\Console\Command\EventsNotifierCommand::execute PHP Method

execute() protected method

See also: Console\Command\Command
protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
    {
        $this->em = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getManager();
        $etcDirectory = APPLICATION_PATH . '/../conf/';
        $notifierTemplate = '_events_notifier.tpl';
        $message = array();
        if (!FilesystemService::isReadable($etcDirectory . '/install_conf.php')) {
            exit;
        }
        // includes installation configuration file
        require_once $etcDirectory . '/install_conf.php';
        // includes campsite initialisation
        require_once APPLICATION_PATH . '/../include/campsite_init.php';
        if (!is_file($etcDirectory . '/database_conf.php')) {
            $output->writeln('Database configuration file is missing!');
            return;
        }
        $message['reply'] = $this->getReplyAddress();
        $autoId = $this->getAutoId();
        $logTimestamp = $autoId->getLogTimestamp();
        $logs = $this->em->getRepository('Newscoop\\Entity\\Log')->createQueryBuilder('l')->select('l, e, u')->leftJoin('l.eventId', 'e')->leftJoin('l.userId', 'u')->where('l.eventId = e.id')->andWhere('l.userId = u.id')->andWhere('e.notify = :notify')->andWhere('l.created > :logTimestamp')->getQuery()->setParameters(array('logTimestamp' => $logTimestamp, 'notify' => 'Y'))->getResult();
        if (count($logs) == 0) {
            return false;
        }
        $tpl = $this->initSmarty();
        $recipients = $this->getApplication()->getKernel()->getContainer()->getService('notification')->findRecipients();
        $lastTimestamp = null;
        if ($input->getOption('verbose')) {
            $output->writeln('<info>Number of found logs: ' . count($logs) . '.<info>');
        }
        foreach ($logs as $log) {
            $lastTimestamp = $log->getCreated();
            $tpl->assign('user_real_name', $log->getUser()->getFirstName());
            $tpl->assign('user_name', $log->getUser()->getUsername());
            $tpl->assign('user_email', $log->getUser()->getEmail());
            $tpl->assign('event_text', $log->getMessage());
            $tpl->assign('event_timestamp', $log->getCreated()->format('Y-m-d h:i:s'));
            $message['text'] = $tpl->fetch($notifierTemplate);
            if (count($recipients) <= 0) {
                if ($input->getOption('verbose')) {
                    $output->writeln('<error>There is no recipients.<error>');
                }
                continue;
            }
            $message['recipients'] = $recipients;
            $message['subject'] = $log->getEvent()->getName();
            $this->sendEmail($message);
            if ($input->getOption('verbose')) {
                $output->writeln('<info>Send message for event: ' . $log->getEvent()->getName() . '.<info>');
            }
        }
        if ($lastTimestamp != null) {
            $autoId->setLogTimestamp($lastTimestamp);
        }
        $this->em->flush();
    }