Passed
Pull Request — master (#103)
by Sergei
02:51 queued 57s
created

CommandLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
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 46
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
    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