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

RunActionCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 41
ccs 0
cts 23
cp 0
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 25
nc 3
nop 2
crap 12
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\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class RunActionCommand extends ContainerAwareCommand
13
{
14 1
    protected function configure()
15
    {
16
        $this
17 1
            ->setName('home:run')
18 1
            ->setDescription('Runs action')
19 1
            ->setHelp("This runs one action separately, passes it given arguments")
20 1
            ->addArgument('action', InputArgument::REQUIRED, 'Who action do you want to execute?')
21 1
            ->addOption(
22 1
                'arg',
23 1
                'a',
24 1
                InputOption::VALUE_OPTIONAL,
25 1
                'Arguments to the command'
26
            );
27 1
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
32
        $actionAlias = $input->getArgument('action');
33
34
        $doctrine = $this->getContainer()->get('doctrine');
35
36
        $action = $doctrine->getManager()->getRepository('AppBundle:Action')->findOneBy(['alias'=>$actionAlias]);
37
38
        if (!$action) {
39
            $output->writeln('No action "'.$actionAlias."' found :(");
40
            return;
41
        }
42
43
        $args = json_decode($action->getArguments(), true);
44
45
        $output->writeln('Running '.$action->getExecutor().' for "'.
46
            $action->getDevice()->getName().'" device');
47
48
        $additionalParams = [];
49
50
        if ($input->getOption('arg')) {
51
            $additionalParams = json_decode($input->getOption('arg'), true);
52
        }
53
54
        $additionalParams['container'] = $this->getContainer();
55
56
        $this->
57
            getContainer()->
58
            get('actions')->
59
            executeReal(
60
                $action,
61
                'onetimecli',
62
                $additionalParams
63
            );
64
        $args['last'] = time();
65
        $action->setArguments(json_encode($args));
66
        $doctrine->getManager()->persist($action);
67
68
        $doctrine->getManager()->flush();
69
    }
70
}
71