RunCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 63
ccs 0
cts 50
cp 0
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

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