Completed
Push — master ( 289c61...45ac24 )
by Kirill
03:32
created

RunCronsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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