Passed
Pull Request — master (#103)
by Sergei
02:51 queued 57s
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
    private array $commandMap;
16
17
    /**
18
     * @param array $commandMap An array with command names as keys and service ids as values
19
     */
20 31
    public function __construct(ContainerInterface $container, array $commandMap)
21
    {
22 31
        $this->container = $container;
23 31
        $this->commandMap = $commandMap;
24 31
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 8
    public function get(string $name)
30
    {
31 8
        if (!$this->has($name)) {
32
            throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
33
        }
34
35
        /** @var Command $command */
36 8
        $command = $this->container->get($this->commandMap[$name]);
37 8
        if ($command->getName() !== $name) {
38 1
            $command->setName($name);
39
        }
40
41 8
        return $command;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function has(string $name)
48
    {
49 8
        return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getNames()
56
    {
57
        return array_keys($this->commandMap);
58
    }
59
}
60