Passed
Push — master ( a2b216...16e3eb )
by Koldo
02:29
created

ConsoleFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Cli\Container;
6
7
use Antidot\Cli\Application\Console;
8
use Psr\Container\ContainerInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
11
12
use function array_walk;
13
14
class ConsoleFactory
15
{
16 1
    public function __invoke(ContainerInterface $container): Console
17
    {
18 1
        $config = $container->get('config')['console'];
19
        /** @var Console $console */
20 1
        $console = new Console();
21 1
        $commands = $config['commands'] ?? [];
22 1
        $lazyCommands = [];
23 1
        foreach ($commands as $name => $command) {
24
            $lazyCommands[$name] = static function () use ($command, $container): Command {
25 1
                return $container->get($command);
26 1
            };
27
        }
28 1
        $console->setCommandLoader(new FactoryCommandLoader($lazyCommands));
29 1
        array_walk(
30 1
            $config['helper-sets'],
31
            static function (string $helperSet) use ($console, $container): void {
32 1
                $console->setHelperSet($container->get($helperSet));
33 1
            }
34
        );
35
36 1
        return $console;
37
    }
38
}
39