Passed
Push — master ( 9638a7...1ba969 )
by Alexander
02:32
created

ApplicationProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console\Provider;
6
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
9
use Yiisoft\Di\Container;
10
use Yiisoft\Di\Support\ServiceProvider;
11
use Yiisoft\Yii\Console\Application;
12
use Yiisoft\Yii\Console\SymfonyEventDispatcher;
13
14
final class ApplicationProvider extends ServiceProvider
15
{
16
    private array $commands;
17
    private string $name;
18
    private string $version;
19
20 2
    public function __construct(array $commands = [], string $name = '', string $version = '')
21
    {
22 2
        $this->commands = $commands;
23 2
        $this->name = $name;
24 2
        $this->version = $version;
25 2
    }
26
27
    /**
28
     * @suppress PhanAccessMethodProtected
29
     */
30 2
    public function register(Container $container): void
31
    {
32
        $container->set(Application::class, function (ContainerInterface $container) {
33 2
            $application = new Application();
34
35 2
            $dispatcher = $container->get(SymfonyEventDispatcher::class);
36 2
            $application->setDispatcher($dispatcher);
37
38 2
            $loader = new ContainerCommandLoader(
39 2
                $container,
40 2
                $this->commands
41
            );
42
43 2
            $application->setCommandLoader($loader);
44
45 2
            if ($this->name !== '') {
46 2
                $application->setName($this->name);
47
            }
48
49 2
            if ($this->version !== '') {
50 2
                $application->setVersion($this->version);
51
            }
52
53 2
            $application->setAutoExit(false);
54
55 2
            return $application;
56 2
        });
57 2
    }
58
}
59