Completed
Push — master ( 5ac2a2...32410d )
by Kirill
06:44
created

RunCronsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 45
ccs 5
cts 25
cp 0.2
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 34 4
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 1
    protected function configure()
13
    {
14
        $this
15 1
            ->setName('home:run-crons')
16 1
            ->setDescription('Runs cronned actions for devices')
17 1
            ->setHelp("This command takes all actions, that are marked as crons and runs it.");
18 1
    }
19
20
    protected function execute(InputInterface $input, OutputInterface $output)
21
    {
22
23
        $doctrine = $this->getContainer()->get('doctrine');
24
25
        $crons = $doctrine->getManager()->getRepository('AppBundle:Action')->findBy(['type'=>'cron']);
26
27
        /** @var Action $cronAction */
28
        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('Running '.$cronAction->getExecutor().' for "'.
37
                    $cronAction->getDevice()->getName().'" device');
38
                $this->
39
                    getContainer()->
40
                    get('actions')->
41
                    executeReal(
42
                        $cronAction,
43
                        'cron',
44
                        ['container'=>$this->getContainer()]
45
                    );
46
                $args['last'] = time();
47
                $cronAction->setArguments(json_encode($args));
48
                $doctrine->getManager()->persist($cronAction);
49
            }
50
        }
51
52
        $doctrine->getManager()->flush();
53
    }
54
}
55