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

CronStatusCommand::execute()   C

Complexity

Conditions 11
Paths 63

Size

Total Lines 56
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 56
rs 6.5481
cc 11
eloc 43
nc 63
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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