Completed
Push — master ( ffecda...e79dfa )
by Benjamin
02:28
created

CronStatusCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Alpixel\Bundle\CronBundle\Command;
4
5
use Alpixel\Bundle\CronBundle\Entity\CronJob;
6
use Alpixel\Bundle\CronBundle\Entity\CronJobResult;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CronStatusCommand extends ContainerAwareCommand
13
{
14
    protected function configure()
15
    {
16
        $this->setName('cron:status')
17
             ->setDescription('Displays the current status of cron jobs')
18
             ->addArgument('job', InputArgument::OPTIONAL, 'Show information for only this job');
19
    }
20
21
    protected function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
24
        $output->writeln('Cron job statuses:');
25
26
        $cronJobs = [];
27
        if ($jobName = $input->getArgument('job')) {
28
            try {
29
                $cronJobs = [$jobRepo->findOneByCommand($jobName)];
0 ignored issues
show
Bug introduced by
The variable $jobRepo does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
            } catch (\Exception $e) {
31
                $output->writeln("Couldn't find a job by the name of $jobName");
32
33
                return CronJobResult::FAILED;
34
            }
35
        } else {
36
            $cronJobs = $em->getRepository('CronBundle:CronJob')->findAll();
37
        }
38
39
        foreach ($cronJobs as $cronJob) {
40
            $output->write(' - '.$cronJob->getCommand());
41
            if (!$cronJob->getEnabled()) {
42
                $output->write(' (disabled)');
43
            }
44
            $output->writeln('');
45
            $output->writeln('   Description: '.$cronJob->getDescription());
46
            if (!$cronJob->getEnabled()) {
47
                $output->writeln('   Not scheduled');
48
            } else {
49
                $output->write('   Scheduled for: ');
50
                $now = new \DateTime();
51
                if ($cronJob->getNextRun() <= $now) {
52
                    $output->writeln('Next run');
53
                } else {
54
                    $output->writeln(strftime('%c', $cronJob->getNextRun()->getTimestamp()));
55
                }
56
            }
57
            if ($cronJob->getMostRecentRun()) {
58
                $status = 'Unknown';
59
                switch ($cronJob->getMostRecentRun()->getResult()) {
60
                    case CronJobResult::SUCCEEDED:
61
                        $status = 'Successful';
62
                        break;
63
                    case CronJobResult::SKIPPED:
64
                        $status = 'Skipped';
65
                        break;
66
                    case CronJobResult::FAILED:
67
                        $status = 'Failed';
68
                        break;
69
                }
70
                $output->writeln("   Last run was: $status");
71
            } else {
72
                $output->writeln('   This job has not yet been run');
73
            }
74
            $output->writeln('');
75
        }
76
    }
77
}
78