DirectoryCommandLoader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 15
c 0
b 0
f 0
dl 0
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 3 1
A has() 0 8 3
A getNames() 0 7 2
A get() 0 9 3
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure\Console;
13
14
use Slick\Di\ContainerInterface;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
17
use Symfony\Component\Console\Exception\CommandNotFoundException;
18
19
/**
20
 * DirectoryCommandLoader
21
 *
22
 * @package Slick\WebStack\Infrastructure\Console
23
 */
24
final class DirectoryCommandLoader implements CommandLoaderInterface
25
{
26
    /** @var array<ConsoleCommandLoader>  */
27
    private array $loaders = [];
28
29
    public function __construct(
30
        private readonly ContainerInterface $container
31
    ) {
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function get(string $name): Command
38
    {
39
        foreach ($this->loaders as $loader) {
40
            if ($loader->has($name)) {
41
                return $loader->get($name);
42
            }
43
        }
44
45
        throw new CommandNotFoundException("Command '$name' not found.");
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function has(string $name): bool
52
    {
53
        foreach ($this->loaders as $loader) {
54
            if ($loader->has($name)) {
55
                return true;
56
            }
57
        }
58
        return false;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getNames(): array
65
    {
66
        $names = [];
67
        foreach ($this->loaders as $loader) {
68
            $names = array_merge($names, $loader->getNames());
69
        }
70
        return $names;
71
    }
72
73
    public function add(string $sourcePath): void
74
    {
75
        $this->loaders[] = new ConsoleCommandLoader($this->container, $sourcePath);
76
    }
77
}
78