CronScanCommand::execute()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
rs 7.4119
cc 8
eloc 32
nc 12
nop 2

How to fix   Long Method   

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\Annotation\CronJob as CronJobAnno;
6
use Alpixel\Bundle\CronBundle\Entity\CronJob;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class CronScanCommand extends ContainerAwareCommand
14
{
15
    protected function configure()
16
    {
17
        $this->setName('cron:scan')
18
             ->setDescription('Scans for any new or deleted cron jobs')
19
             ->addOption('keep-deleted', 'k', InputOption::VALUE_NONE, 'If set, deleted cron jobs will not be removed')
20
             ->addOption('default-disabled', 'd', InputOption::VALUE_NONE, 'If set, new jobs will be disabled by default');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $keepDeleted = $input->getOption('keep-deleted');
26
        $defaultDisabled = $input->getOption('default-disabled');
27
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
28
29
        // Enumerate the known jobs
30
        $jobRepo = $em->getRepository('CronBundle:CronJob');
31
        $knownJobs = $jobRepo->getKnownJobs();
32
        $knownJobs = array_fill_keys($knownJobs, true);
33
34
        // Enumerate all the jobs currently loaded
35
        $reader = $this->getContainer()->get('annotation_reader');
36
37
        foreach ($this->getApplication()->all() as $command) {
38
            // Check for an @CronJob annotation
39
            $reflClass = new \ReflectionClass($command);
40
            foreach ($reader->getClassAnnotations($reflClass) as $anno) {
41
                if ($anno instanceof CronJobAnno) {
42
                    $job = $command->getName();
43
                    if (array_key_exists($job, $knownJobs)) {
44
                        // Clear it from the known jobs so that we don't try to delete it
45
                        unset($knownJobs[$job]);
46
47
                        // Update the job if necessary
48
                        $currentJob = $jobRepo->findOneByCommand($job);
49
                        $currentJob->setDescription($command->getDescription());
50
                        if ($currentJob->getInterval() != $anno->value) {
51
                            $newTime = new \DateTime();
52
                            $newTime = $newTime->add(new \DateInterval($anno->value));
53
54
                            $currentJob->setInterval($anno->value);
55
                            $currentJob->setNextRun($newTime);
56
                            $output->writeln("Updated interval for $job to {$anno->value}");
57
                        }
58
                    } else {
59
                        $this->newJobFound($output, $command, $anno, $defaultDisabled);
60
                    }
61
                }
62
            }
63
        }
64
65
        // Clear any jobs that weren't found
66
        if (!$keepDeleted) {
67
            foreach (array_keys($knownJobs) as $deletedJob) {
68
                $output->writeln("Deleting job: $deletedJob");
69
                $jobToDelete = $jobRepo->findOneByCommand($deletedJob);
70
                $em->remove($jobToDelete);
71
            }
72
        }
73
74
        $em->flush();
75
        $output->writeln('Finished scanning for cron jobs');
76
    }
77
78
    protected function newJobFound(OutputInterface $output, Command $command, CronJobAnno $anno, $defaultDisabled = false)
79
    {
80
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
81
        $newJob = new CronJob();
82
        $newJob->setCommand($command->getName());
83
        $newJob->setDescription($command->getDescription());
84
        $newJob->setInterval($anno->value);
85
86
        if ($anno->startTime === null) {
87
            $newJob->setNextRun(new \DateTime());
0 ignored issues
show
Documentation introduced by
new \DateTime() is of type object<DateTime>, but the function expects a object<Alpixel\Bundle\CronBundle\Entity\datetime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
        } else {
89
            $newJob->setNextRun(new \DateTime($anno->startTime));
0 ignored issues
show
Documentation introduced by
new \DateTime($anno->startTime) is of type object<DateTime>, but the function expects a object<Alpixel\Bundle\CronBundle\Entity\datetime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
        }
91
92
        $newJob->setEnabled(!$defaultDisabled);
93
94
        $output->writeln('Added the job '.$newJob->getCommand().' with interval '.$newJob->getInterval());
95
        $em->persist($newJob);
96
    }
97
}
98