Passed
Push — master ( 1c6be6...e4f6f0 )
by Jonas
15:53 queued 01:01
created

LoadEnvironmentVariables::writeErrorAndDie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace LumenWpApp;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Exception\InvalidFileException;
7
use Illuminate\Support\Env;
8
use Symfony\Component\Console\Output\ConsoleOutput;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\ConsoleOutput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class LoadEnvironmentVariables
11
{
12
    /**
13
     * The directory containing the environment file.
14
     *
15
     * @var string
16
     */
17
    protected $filePath;
18
19
    /**
20
     * The name of the environment file.
21
     *
22
     * @var string|null
23
     */
24
    protected $fileName;
25
26
    /**
27
     * Create a new loads environment variables instance.
28
     *
29
     * @param  string  $path
30
     * @param  string|null  $name
31
     * @return void
32
     */
33
    public function __construct($path, $name = null)
34
    {
35
        $this->filePath = $path;
36
        $this->fileName = $name;
37
    }
38
39
    /**
40
     * Setup the environment variables.
41
     *
42
     * If no environment file exists, we continue silently.
43
     *
44
     * @return void
45
     */
46
    public function bootstrap()
47
    {
48
        try {
49
            $this->createDotenv()->safeLoad();
50
        } catch (InvalidFileException $e) {
51
            $this->writeErrorAndDie([
52
                'The environment file is invalid!',
53
                $e->getMessage(),
54
            ]);
55
        }
56
    }
57
58
    /**
59
     * Create a Dotenv instance.
60
     *
61
     * @return \Dotenv\Dotenv
62
     */
63
    protected function createDotenv()
64
    {
65
        return Dotenv::create(
66
            Env::getRepository(),
67
            $this->filePath,
68
            $this->fileName
69
        );
70
    }
71
72
    /**
73
     * Write the error information to the screen and exit.
74
     *
75
     * @param  string[]  $errors
76
     * @return void
77
     */
78
    protected function writeErrorAndDie(array $errors)
79
    {
80
        $output = (new ConsoleOutput)->getErrorOutput();
81
82
        foreach ($errors as $error) {
83
            $output->writeln($error);
84
        }
85
86
        exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
87
    }
88
}
89