Passed
Push — master ( a4f5c1...9bc941 )
by Alexander
02:11
created

CommandLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 50
ccs 12
cts 15
cp 0.8
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 13 3
A has() 0 3 2
A getNames() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console;
6
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
10
use Symfony\Component\Console\Exception\CommandNotFoundException;
11
12
final class CommandLoader implements CommandLoaderInterface
13
{
14
    private ContainerInterface $container;
15
16
    /**
17
     * @var array<string, string>
18
     */
19
    private array $commandMap;
20
21
    /**
22
     * @param array<string, string> $commandMap An array with command names as keys and service ids as values
23
     */
24 31
    public function __construct(ContainerInterface $container, array $commandMap)
25
    {
26 31
        $this->container = $container;
27 31
        $this->commandMap = $commandMap;
28 31
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 8
    public function get(string $name)
34
    {
35 8
        if (!$this->has($name)) {
36
            throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
37
        }
38
39
        /** @var Command $command */
40 8
        $command = $this->container->get($this->commandMap[$name]);
41 8
        if ($command->getName() !== $name) {
42 1
            $command->setName($name);
43
        }
44
45 8
        return $command;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 8
    public function has(string $name)
52
    {
53 8
        return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getNames()
60
    {
61
        return array_keys($this->commandMap);
62
    }
63
}
64