GetCommands::constructOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace HotRodCli\Api;
4
5
use HotRodCli\AppContainer;
6
use HotRodCli\Commands\BaseCommand;
7
8
class GetCommands
9
{
10
    protected $container;
11
12
    public function __construct(AppContainer $appContainer)
13
    {
14
        $this->container = $appContainer;
15
    }
16
17
    public function __invoke($args = [])
18
    {
19
        $result = [];
20
21
        foreach ($this->container->get('commands') as $code => $className) {
22
            $command = $this->container->resolve($className);
23
24
            $result[$code] = [
25
                'name' => $command->getName(),
26
                'description' => $command->getDescription(),
27
                'help' => $command->getHelp(),
28
                'arguments' => $this->constructArguments($command),
29
                'options' => $this->constructOptions($command),
30
                'info' => $command->getInfo()
31
            ];
32
        }
33
34
        return json_encode($result);
35
    }
36
37
    public function constructArguments(BaseCommand $command)
38
    {
39
        $result = [];
40
41
        foreach ($command->getDefinition()->getArguments() as $argument) {
42
            $result[] = [
43
                'name' => $argument->getName(),
44
                'description' => $argument->getDescription()
45
            ];
46
        }
47
48
        return $result;
49
    }
50
51
    public function constructOptions(BaseCommand $command)
52
    {
53
        $result = [];
54
55
        foreach ($command->getDefinition()->getOptions() as $option) {
56
            $result[] = [
57
                'name' => $option->getName(),
58
                'description' => $option->getDescription()
59
            ];
60
        }
61
62
        return $result;
63
    }
64
}
65