Passed
Push — master ( 5b49ac...957a79 )
by SignpostMarv
02:45
created

Application::CollectApplicationWithCommands()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 3
dl 0
loc 13
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework\Symfony\Console;
8
9
use BadMethodCallException;
10
use SignpostMarv\DaftFramework\AttachDaftFramework;
11
use SignpostMarv\DaftFramework\Framework;
12
use SignpostMarv\DaftFramework\Symfony\Console\Command\Command;
13
use SignpostMarv\DaftInterfaceCollector\StaticMethodCollector;
14
use Symfony\Component\Console\Application as Base;
15
use Symfony\Component\Console\Command\Command as BaseCommand;
16
17
class Application extends Base
18
{
19
    use AttachDaftFramework;
20
21
    public function add(BaseCommand $command)
22
    {
23
        return $this->addStrict($command);
24
    }
25
26
    public function addStrict(BaseCommand $command) : ? BaseCommand
27
    {
28
        $out = parent::add($command);
29
30
        if ($out instanceof Command) {
31
            $maybeFramework = $this->GetDaftFramework();
32
33
            if ( ! ($maybeFramework instanceof Framework)) {
34
                throw new BadMethodCallException(
35
                    'Cannot add a daft framework command without a framework being attached!'
36
                );
37
            }
38
39
            $out->AttachDaftFramework($maybeFramework);
40
        } elseif ($command instanceof Command) {
41
            $command->DetachDaftFramework();
42
        }
43
44
        return $out;
45
    }
46
47
    public function GetCommandCollector() : StaticMethodCollector
48
    {
49
        return new StaticMethodCollector(
50
            [
51
                DaftConsoleSource::class => [
52
                    'DaftFrameworkConsoleSources' => [
53
                        DaftConsoleSource::class,
54
                        BaseCommand::class,
55
                        Command::class,
56
                    ],
57
                ],
58
            ],
59
            [
60
                BaseCommand::class,
61
                Command::class,
62
            ]
63
        );
64
    }
65
66
    public function CollectCommands(string ...$sources) : void
67
    {
68
        $framework = $this->GetDaftFramework();
69
70
        if ( ! ($framework instanceof Framework)) {
71
            throw new BadMethodCallException(
72
                'Cannot collect commands without an attached framework instance!'
73
            );
74
        }
75
76
        foreach ($this->GetCommandCollector()->Collect(...$sources) as $implementation) {
77
            if (is_a($implementation, BaseCommand::class, true)) {
78
                /**
79
                * @var BaseCommand $command
80
                */
81
                $command = new $implementation($implementation::getDefaultName());
82
83
                $this->add($command);
84
            }
85
        }
86
    }
87
88
    /**
89
    * @return static
90
    */
91
    public static function CollectApplicationWithCommands(
92
        string $name,
93
        string $version,
94
        Framework $framework
95
    ) : self {
96
        $application = new static($name, $version);
97
        $application->AttachDaftFramework($framework);
98
99
        $config = ($framework->ObtainConfig()[DaftConsoleSource::class] ?? []);
100
101
        $application->CollectCommands(...array_values(is_array($config) ? $config : []));
102
103
        return $application;
104
    }
105
}
106