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

CronStatusCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
c 4
b 1
f 0
lcom 0
cbo 5
dl 0
loc 66
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
C execute() 0 56 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
        $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