Passed
Pull Request — master (#56)
by Wilmer
14:36
created

ApplicationProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
c 1
b 0
f 0
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 26 3
A __construct() 0 5 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
    public function __construct(array $commands = [], string $name = '', string $version = '')
21
    {
22
        $this->commands = $commands;
23
        $this->name = $name;
24
        $this->version = $version;
25
    }
26
27
    /**
28
     * @suppress PhanAccessMethodProtected
29
     */
30
    public function register(Container $container): void
31
    {
32
        $container->set(Application::class, function (ContainerInterface $container) {
33
            $application = new Application();
34
35
            $dispatcher = $container->get(SymfonyEventDispatcher::class);
36
            $application->setDispatcher($dispatcher);
37
38
            $loader = new ContainerCommandLoader(
39
                $container,
40
                $this->commands
41
            );
42
43
            $application->setCommandLoader($loader);
44
45
            if ($this->name !== '') {
46
                $application->setName($this->name);
47
            }
48
49
            if ($this->version !== '') {
50
                $application->setVersion($this->version);
51
            }
52
53
            $application->setAutoExit(false);
54
55
            return $application;
56
        });
57
    }
58
}
59