RunCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 12
rs 9.8333
c 1
b 0
f 0
1
<?php
2
3
namespace ByJG\Daemon\Console;
4
5
use ByJG\Daemon\Runner;
6
use Symfony\Component\Console\Command\Command;
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 RunCommand extends Command
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('run')
18
            ->setDescription('Run a PHP class without become a daemon')
19
            ->addArgument(
20
                'classname',
21
                InputArgument::REQUIRED,
22
                'The PHP class and method like ClassName::Method'
23
            )
24
            ->addOption(
25
                'bootstrap',
26
                'b',
27
                InputOption::VALUE_OPTIONAL,
28
                'The relative path from root directory for the bootstrap file, like ./vendor/autoload.php',
29
                'vendor/autoload.php'
30
            )
31
            ->addOption(
32
                'rootdir',
33
                'r',
34
                InputOption::VALUE_OPTIONAL,
35
                'The root path where your application is installed',
36
                getcwd()
37
            )
38
            ->addOption(
39
                '--http-get',
40
                "-g",
41
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
42
                'is an optional arguments for your class',
43
                []
44
            )
45
            ->addOption(
46
                'daemon',
47
                'd',
48
                InputOption::VALUE_NONE,
49
                'Run as a daemon'
50
            );
51
    }
52
53
    protected function execute(InputInterface $input, OutputInterface $output): int
54
    {
55
        $className = $input->getArgument('classname');
56
        $rootPath = $input->getOption('rootdir');
57
        $bootstrap = $rootPath . "/" . $input->getOption('bootstrap');
58
59
        if (!file_exists($rootPath)) {
60
            throw new \Exception("The rootpath '$bootstrap' does not exists. Use absolute path or relative path from current directory.");
61
        }
62
63
        if (!file_exists($bootstrap)) {
64
            throw new \Exception("The bootstrap file '$bootstrap' does not exists. Use a relative path from the root path.");
65
        }
66
67
        chdir($rootPath);
68
        require_once $bootstrap;
69
70
        parse_str(implode('&', $input->getOption('http-get')), $httpGet);
71
        $runner = new Runner($className, $httpGet, $input->getOption('daemon'));
72
        $runner->execute();
73
74
        return 0;
75
    }
76
}
77