Passed
Push — master ( ad913f...0acd97 )
by Vladimir
02:46
created

AppServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Application;
6
7
use Dotenv\Dotenv;
8
use League\Container\ServiceProvider\AbstractServiceProvider;
9
use League\Container\ServiceProvider\BootableServiceProviderInterface;
10
11
abstract class AppServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
12
{
13
    protected $provides = [
14
        Config::class,
15
        'env',
16
        'base_path',
17
        'resources_path',
18
    ];
19
20
    /**
21
     * Determine environment where application is currently is running on.
22
     *
23
     * @return string
24
     */
25
    abstract public function environment(): string;
26
27
    /**
28
     * Base path of the application.
29
     *
30
     * @return string
31
     */
32
    abstract public function basePath(): string;
33
34
    /**
35
     * Path to the "resources folder".
36
     *
37
     * @return string
38
     */
39
    abstract public function resourcesPath(): string;
40
41
    /**
42
     * Method will be invoked on registration of a service provider implementing
43
     * this interface. Provides ability for eager loading of Service Providers.
44
     *
45
     * @return void
46
     */
47
    public function boot(): void
48
    {
49
        $dotenv = new Dotenv($this->basePath());
50
        $variables = $dotenv->load();
51
52
        $this->getContainer()->share(Config::class, function () use ($variables) {
53
            return new Config($variables);
54
        });
55
    }
56
57
    /**
58
     * Use the register method to register items with the container via the
59
     * protected $this->container property or the `getContainer` method
60
     * from the ContainerAwareTrait.
61
     *
62
     * @return void
63
     */
64
    public function register(): void
65
    {
66
        $this->getContainer()->share('env', $this->environment());
67
        $this->getContainer()->share('base_path', $this->basePath());
68
        $this->getContainer()->share('resources_path', $this->resourcesPath());
69
    }
70
}
71