Completed
Pull Request — master (#84)
by Maarten
03:23
created

EnvVariablesExists::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use RegexIterator;
10
11
class EnvVariablesExists implements Check
12
{
13
    /**
14
     * Stores processed var names
15
     *
16
     * @var array
17
     */
18
    private $processed = [];
19
20
    /**
21
     * The empty results of performed scan
22
     *
23
     * @var array
24
     */
25
    public $undefined = 0;
26
27
    /**
28
     * The name of the check.
29
     *
30
     * @param array $config
31
     * @return string
32
     */
33
    public function name(array $config): string
34
    {
35
        return trans('self-diagnosis::checks.env_variables_exist.name');
36
    }
37
38
    /**
39
     * The error message to display in case the check does not pass.
40
     *
41
     * @param array $config
42
     * @return string
43
     */
44
    public function message(array $config): string
45
    {
46
        return trans('self-diagnosis::checks.env_variables_exist.message', [
47
            'undefined' => $this->undefined,
48
        ]);
49
    }
50
51
    /**
52
     * Perform the actual verification of this check.
53
     *
54
     * @param array $config
55
     * @return bool
56
     * @throws \Exception
57
     */
58 4
    public function check(array $config): bool
59
    {
60 4
        $paths = Collection::make(Arr::get($config, 'directories', []));
61
62 4
        foreach ($paths as $path) {
63 4
            $files = $this->recursiveDirSearch($path, '/.*?.php/');
64
65 4
            foreach ($files as $file) {
66 4
                preg_match_all(
67 4
                    '# env\((.*?)\)#',
68 4
                    str_replace(["\n", "\r"], '', file_get_contents($file)),
69 3
                    $values
70
                );
71
72 4
                if (is_array($values)) {
73 4
                    foreach ($values[1] as $value) {
74 4
                        $result = $this->getResult(
75 4
                            explode(',', str_replace(["'", '"', ' '], '', $value))
76
                        );
77
78 4
                        if (!$result) {
79 4
                            continue;
80
                        }
81
82 4
                        $this->storeResult($result);
83
                    }
84
                }
85
            }
86
        }
87
88 4
        return $this->undefined === 0;
89
    }
90
91
    /**
92
     * Get result based on comma separated parsed env() parameters
93
     *
94
     * @param array $values
95
     * @return object|bool
96
     */
97 4
    private function getResult(array $values)
98
    {
99 4
        $envVar = $values[0];
100
101 4
        if (in_array($envVar, $this->processed)) {
102 4
            return false;
103
        }
104
105 4
        $this->processed[] = $envVar;
106
107
        return (object)[
108 4
            'envVar' => $envVar,
109 4
            'hasValue' => env($envVar) !== null,
110 4
            'hasDefault' => isset($values[1]),
111
        ];
112
    }
113
114
    /**
115
     * Store result and optional runtime output
116
     *
117
     * @param $result
118
     */
119 4
    private function storeResult($result)
120
    {
121 4
        if (!$result->hasValue && !$result->hasDefault) {
122 4
            $this->undefined++;
123
        }
124 4
    }
125
126 4
    private function recursiveDirSearch(string $folder, string $pattern): array
127
    {
128 4
        if (!file_exists($folder)) {
129
            return [];
130
        }
131
132 4
        $files = new RegexIterator(
133 4
            new RecursiveIteratorIterator(
134 4
                new RecursiveDirectoryIterator($folder)
135
            ),
136 4
            $pattern, RegexIterator::GET_MATCH
137
        );
138
139 4
        $list = [];
140
141 4
        foreach ($files as $file) {
142 4
            $list = array_merge($list, $file);
143
        }
144
145 4
        return $list;
146
    }
147
}
148