CronStatusCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
C execute() 0 58 11
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
        $jobRepo = $em->getRepository(CronJob::class);
25
        
26
        $output->writeln('Cron job statuses:');
27
28
        $cronJobs = [];
29
        if ($jobName = $input->getArgument('job')) {
30
            try {
31
                $cronJobs = [$jobRepo->findOneByCommand($jobName)];
32
            } catch (\Exception $e) {
33
                $output->writeln("Couldn't find a job by the name of $jobName");
34
35
                return CronJobResult::FAILED;
36
            }
37
        } else {
38
            $cronJobs = $em->getRepository('CronBundle:CronJob')->findAll();
39
        }
40
41
        foreach ($cronJobs as $cronJob) {
42
            $output->write(' - '.$cronJob->getCommand());
43
            if (!$cronJob->getEnabled()) {
44
                $output->write(' (disabled)');
45
            }
46
            $output->writeln('');
47
            $output->writeln('   Description: '.$cronJob->getDescription());
48
            if (!$cronJob->getEnabled()) {
49
                $output->writeln('   Not scheduled');
50
            } else {
51
                $output->write('   Scheduled for: ');
52
                $now = new \DateTime();
53
                if ($cronJob->getNextRun() <= $now) {
54
                    $output->writeln('Next run');
55
                } else {
56
                    $output->writeln(strftime('%c', $cronJob->getNextRun()->getTimestamp()));
57
                }
58
            }
59
            if ($cronJob->getMostRecentRun()) {
60
                $status = 'Unknown';
61
                switch ($cronJob->getMostRecentRun()->getResult()) {
62
                    case CronJobResult::SUCCEEDED:
63
                        $status = 'Successful';
64
                        break;
65
                    case CronJobResult::SKIPPED:
66
                        $status = 'Skipped';
67
                        break;
68
                    case CronJobResult::FAILED:
69
                        $status = 'Failed';
70
                        break;
71
                }
72
                $output->writeln("   Last run was: $status");
73
            } else {
74
                $output->writeln('   This job has not yet been run');
75
            }
76
            $output->writeln('');
77
        }
78
    }
79
}
80