Completed
Push — feature/database-migrations ( b550b6 )
by Avtandil
02:27
created

LoadEnvironmentVariables   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 8 2
A createDotenv() 0 8 1
A writeErrorAndDie() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Longman\TelegramBot\Bootstrap;
5
6
use Dotenv\Dotenv;
7
use Dotenv\Environment\Adapter\EnvConstAdapter;
8
use Dotenv\Environment\Adapter\PutenvAdapter;
9
use Dotenv\Environment\Adapter\ServerConstAdapter;
10
use Dotenv\Environment\DotenvFactory;
11
use Exception;
12
use Longman\TelegramBot\Application;
13
14
class LoadEnvironmentVariables
15
{
16
    public function bootstrap(Application $app)
17
    {
18
        try {
19
            $this->createDotenv($app)->load();
20
        } catch (Exception $e) {
21
            $this->writeErrorAndDie($e);
22
        }
23
    }
24
25
    protected function createDotenv(Application $app): Dotenv
26
    {
27
        return Dotenv::create(
28
            $app->environmentPath(),
29
            $app->environmentFile(),
30
            new DotenvFactory([new EnvConstAdapter, new ServerConstAdapter, new PutenvAdapter])
31
        );
32
    }
33
34
    protected function writeErrorAndDie(Exception $e)
35
    {
36
        $message = 'The environment file is invalid! ' . $e->getMessage();
37
        die($message);
38
    }
39
}
40