RunActionCommand::execute()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
ccs 0
cts 24
cp 0
rs 8.8571
cc 3
eloc 26
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 2
    protected function configure()
15
    {
16
        $this
17 2
            ->setName('home:run')
18 2
            ->setDescription('Runs action')
19 2
            ->setHelp("This runs one action separately, passes it given arguments")
20 2
            ->addArgument('action', InputArgument::REQUIRED, 'Who action do you want to execute?')
21 2
            ->addOption(
22 2
                'arg',
23 2
                'a',
24 2
                InputOption::VALUE_OPTIONAL,
25 2
                'Arguments to the command'
26
            );
27 2
    }
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(
46
            'Running '.$action->getExecutor().' for "'.
47
            $action->getDevice()->getName().'" device'
48
        );
49
50
        $additionalParams = [];
51
52
        if ($input->getOption('arg')) {
53
            $additionalParams = json_decode($input->getOption('arg'), true);
54
        }
55
56
        $additionalParams['container'] = $this->getContainer();
57
58
        $this->
59
            getContainer()
60
            ->get('actions')
61
            ->executeReal(
62
                $action,
63
                'onetimecli',
64
                $additionalParams
65
            );
66
        $args['last'] = time();
67
        $action->setArguments(json_encode($args));
68
        $doctrine->getManager()->persist($action);
69
70
        $doctrine->getManager()->flush();
71
    }
72
}
73