Passed
Push — master ( 3252a3...420639 )
by Andreas
23:53
created

exec::interact()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 10
nop 2
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.console
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\console\command;
10
11
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use midcom\console\loginhelper;
17
use midcom_services_auth;
18
use midcom_helper__componentloader;
19
20
/**
21
 * CLI wrapper for midcom-exec calls
22
 *
23
 * @package midcom.console
24
 */
25
class exec extends Command
26
{
27
    use loginhelper;
28
29
    private midcom_services_auth $auth;
30
31
    private midcom_helper__componentloader $loader;
32
33
    private string $projectdir;
34
35
    public function __construct(midcom_services_auth $auth, midcom_helper__componentloader $loader, string $projectdir)
36
    {
37
        $this->auth = $auth;
38
        $this->loader = $loader;
39
        $this->projectdir = $projectdir;
40
        parent::__construct();
41
    }
42
43
    protected function configure()
44
    {
45
        $this->setName('midcom:exec')
46
            ->setAliases(['exec'])
47
            ->setDescription('Runs a script in midcom context')
48
            ->addArgument('file', InputArgument::OPTIONAL, 'The file to run (leave empty to list available files)')
49
            ->addArgument('get', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Additional GET parameters (key=value pairs, space-separated)')
50
            ->addOption('login', 'l', InputOption::VALUE_NONE, 'Use Midgard authorization');
51
    }
52
53
    protected function interact(InputInterface $input, OutputInterface $output)
54
    {
55
        if ($input->getOption('login')) {
56
            $dialog = $this->getHelperSet()->get('question');
57
            $this->login($dialog, $input, $output);
58
        }
59
        $get = $input->getArgument('get');
60
61
        if (is_array($get)) {
62
            foreach ($get as $data) {
63
                $input_data = explode('=', $data);
64
                if (count($input_data) == 1) {
65
                    $_GET[] = $input_data;
66
                } elseif (count($input_data) == 2) {
67
                    $_GET[$input_data[0]] = $input_data[1];
68
                } else {
69
                    throw new \RuntimeException('Could not parse input ' . $data);
70
                }
71
            }
72
        }
73
    }
74
75
    protected function execute(InputInterface $input, OutputInterface $output) : int
76
    {
77
        $file = $input->getArgument('file');
78
79
        if (empty($file)) {
80
            $this->list_files($output);
81
            return 0;
82
        }
83
84
        $basedir = $this->projectdir . '/';
85
        if (!file_exists($basedir . $file)) {
86
            throw new \midcom_error('File not found');
87
        }
88
89
        if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
90
            $output->writeln('Running ' . $file);
91
        }
92
93
        $this->auth->request_sudo('midcom.exec');
94
95
        try {
96
            require $basedir . $file;
97
        } catch (\midcom_error_forbidden $e) {
98
            $dialog = $this->getHelperSet()->get('question');
99
            $this->login($dialog, $input, $output);
100
            require $basedir . $file;
101
        }
102
103
        $this->auth->drop_sudo();
104
        return 0;
105
    }
106
107
    private function list_files(OutputInterface $output)
108
    {
109
        $output->writeln("\n<comment>Available exec files:</comment>\n");
110
111
        foreach ($this->loader->get_manifests() as $manifest) {
112
            $exec_dir = $this->loader->path_to_snippetpath($manifest->name) . '/exec';
113
114
            if (is_dir($exec_dir)) {
115
                foreach (glob($exec_dir . '/*.php') as $file) {
116
                    $path = preg_replace('/^' . preg_quote($this->projectdir, '/') . '/', '', $file);
117
                    $parts = pathinfo($path);
118
                    $path = '  <info>' . $parts['dirname'] . '/</info>' . $parts['filename'] . '<info>.' . $parts['extension'] . '</info>';
119
120
                    $output->writeln($path);
121
                }
122
            }
123
        }
124
    }
125
}
126