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

CommandLoader::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 10
cc 3
nc 3
nop 1
crap 3.0261
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