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

RunActionCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 32.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 59
ccs 11
cts 34
cp 0.3235
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
B execute() 0 41 3
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