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

RunCronsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
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