Passed
Push — master ( d93a6d...5a06fd )
by Dmitrij
03:06
created

GetCommands   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 2
A __construct() 0 3 1
A constructArguments() 0 12 2
A constructOptions() 0 12 2
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
            ];
31
        }
32
33
        return json_encode($result);
34
    }
35
36
    public function constructArguments(BaseCommand $command)
37
    {
38
        $result = [];
39
40
        foreach ($command->getDefinition()->getArguments() as $argument) {
41
            $result[] = [
42
                'name' => $argument->getName(),
43
                'description' => $argument->getDescription()
44
            ];
45
        }
46
47
        return $result;
48
    }
49
50
    public function constructOptions(BaseCommand $command)
51
    {
52
        $result = [];
53
54
        foreach ($command->getDefinition()->getOptions() as $option) {
55
            $result[] = [
56
                'name' => $option->getName(),
57
                'description' => $option->getDescription()
58
            ];
59
        }
60
61
        return $result;
62
    }
63
}
64