RunCronsCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 9.8312

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 6
cts 21
cp 0.2857
rs 8.5806
cc 4
eloc 22
nc 4
nop 2
crap 9.8312
1
<?php
2
3
namespace AppBundle\Command;
4
5
use AppBundle\Entity\Action;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class RunCronsCommand extends ContainerAwareCommand
11
{
12 2
    protected function configure()
13
    {
14
        $this
15 2
            ->setName('home:run-crons')
16 2
            ->setDescription('Runs cronned actions for devices')
17 2
            ->setHelp("This command takes all actions, that are marked as crons and runs it.");
18 2
    }
19
20 1
    protected function execute(InputInterface $input, OutputInterface $output)
21
    {
22
23 1
        $doctrine = $this->getContainer()->get('doctrine');
24
25 1
        $crons = $doctrine->getManager()->getRepository('AppBundle:Action')->findBy(['type'=>'cron']);
26
27
        /** @var Action $cronAction */
28 1
        foreach ($crons as $cronAction) {
29
            $args = json_decode($cronAction->getArguments(), true);
30
31
            if (isset($args['disabled'])) {
32
                continue;
33
            }
34
35
            if (time() - $args['last'] >= $args['every']) {
36
                $output->writeln(
37
                    'Running '.$cronAction->getExecutor().' for "'.
38
                    $cronAction->getDevice()->getName().'" device'
39
                );
40
                $this->
41
                    getContainer()
42
                    ->get('actions')
43
                    ->executeReal(
44
                        $cronAction,
45
                        'cron',
46
                        ['container'=>$this->getContainer()]
47
                    );
48
                $args['last'] = time();
49
                $cronAction->setArguments(json_encode($args));
50
                $doctrine->getManager()->persist($cronAction);
51
            }
52
        }
53
54 1
        $doctrine->getManager()->flush();
55 1
    }
56
}
57