Middleware::loadEnvironment()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 7
cts 11
cp 0.6364
rs 9.2
cc 4
eloc 11
nc 4
nop 0
crap 4.7691
1
<?php
2
3
namespace Rs\Stack\PhpDotEnv;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\HttpKernelInterface;
7
8
class Middleware implements HttpKernelInterface
9
{
10
    /**
11
     * @var HttpKernelInterface
12
     */
13
    private $app;
14
    /**
15
     * @var
16
     */
17
    private $dir;
18
    /**
19
     * @var
20
     */
21
    private $filename = '.env';
22
23 3
    public function __construct(HttpKernelInterface $app, $dir, $filename = '.env')
24
    {
25 3
        $this->app = $app;
26 3
        $this->dir = $dir;
27 3
        $this->filename = $filename;
28 3
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
34
    {
35 2
        $this->loadEnvironment();
36
37 2
        return $this->app->handle($request, $type, $catch);
38 1
    }
39
40 2
    private function loadEnvironment()
41
    {
42 2
        if (!file_exists(realpath($this->dir) . '/' . $this->filename)) {
43 1
            return false;
44
        }
45
46 1
        if (class_exists('Dotenv\Dotenv')) { //2.x branch
47 1
            $dotenv = new \Dotenv\Dotenv($this->dir, $this->filename);
48 1
            $dotenv->load();
49
50 1
            return true;
51
        } elseif (class_exists('DotEnv')) {
52
            \Dotenv::load($this->dir, $this->filename);
53
54
            return true;
55
        }
56
57
        throw new \RuntimeException('no suitable .env loader found');
58
    }
59
}
60