Test Failed
Push — master ( 1c53d0...36f19e )
by Vsevolods
11:19
created

AbstractServiceProvider::name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php declare(strict_types = 1);
2
3
namespace Venta\ServiceProvider;
4
5
use Venta\Contracts\Config\Config;
6
use Venta\Contracts\Config\ConfigFactory;
7
use Venta\Contracts\Console\CommandCollection;
8
use Venta\Contracts\Container\Container;
9
use Venta\Contracts\ServiceProvider\ServiceProvider;
10
11
/**
12
 * Class AbstractServiceProvider.
13
 *
14
 * @package Venta\ServiceProvider
15
 */
16
abstract class AbstractServiceProvider implements ServiceProvider
17
{
18
    /**
19
     * Service provider name.
20
     *
21
     * @var string
22
     */
23
    protected static $name;
24
25
    /**
26
     * Container instance.
27
     *
28
     * @var Container
29
     */
30
    protected $container;
31
32
    /**
33
     * Application config.
34
     *
35
     * @var Config
36
     */
37
    private $appConfig;
38
39
    /**
40
     * AbstractServiceProvider constructor.
41
     *
42
     * @param Container $container
43
     * @param Config $appConfig
44
     */
45
    public function __construct(Container $container, Config $appConfig)
46
    {
47
        $this->container = $container;
48
        $this->appConfig = $appConfig;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public static function dependencies(): array
55
    {
56
        return [];
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    final public static function name(): string
63
    {
64
        return static::$name ?: static::class;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    abstract public function boot();
71
72
    /**
73
     * Merges config params from service provider with the global configuration.
74
     *
75
     * @param string[] ...$configFiles
76
     */
77
    protected function loadConfigFromFiles(string ...$configFiles)
78
    {
79
        /** @var Config $config */
80
        $config = $this->container->get(Config::class);
81
82
        /** @var ConfigFactory $configFactory */
83
        $configFactory = $this->container->get(ConfigFactory::class);
84
        foreach ($configFiles as $configFile) {
85
            $config = $config->merge($configFactory->createFromFile($configFile));
86
        }
87
88
        $config = $config->merge($this->appConfig);
89
90
        $this->container->bindInstance(Config::class, $config);
91
92
    }
93
94
    /**
95
     * Registers commands exposed by service provider.
96
     *
97
     * @param string[] ...$commandClasses
98
     */
99
    protected function provideCommands(string ...$commandClasses)
100
    {
101
        /** @var CommandCollection $commandCollector */
102
        $commandCollector = $this->container->get(CommandCollection::class);
103
        foreach ($commandClasses as $commandClass) {
104
            $commandCollector->add($commandClass);
105
        }
106
    }
107
}