Test Failed
Pull Request — master (#154)
by Antonio Carlos
04:56 queued 02:28
created

ResourceLoader::getResources()   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
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Health\Support;
4
5
use DomainException;
6
use PragmaRX\Yaml\Package\Yaml;
7
use PragmaRX\Yaml\Package\Support\Resolver;
8
9
class ResourceLoader
10
{
11
    /**
12
     * Yaml service.
13
     *
14
     * @var Yaml
15
     */
16
    protected $yaml;
17
18
    /**
19
     * @var \Illuminate\Support\Collection
20
     */
21
    private $resources;
22
23
    /**
24
     * ResourceLoader constructor.
25
     *
26
     * @param Yaml $yaml
27
     */
28
    public function __construct(Yaml $yaml)
29
    {
30
        $this->yaml = $yaml;
31
    }
32
33
    /**
34
     * Get enabled resources.
35
     *
36
     * @param $resources
37
     * @return \Illuminate\Support\Collection
38
     * @throws \Exception
39
     */
40
    private function getEnabledResources($resources)
41
    {
42
        if (
43
            is_array($filters = config($configKey = 'health.resources.enabled'))
44
        ) {
45
            return collect($resources)->filter(function ($resource, $name) use (
46
                $filters
47
            ) {
48
                foreach ($filters as $filter) {
49
                    if (preg_match("|^$filter$|", $name)) {
50
                        return true;
51
                    }
52
                }
53
54
                return false;
55
            });
56
        }
57
58
        throw new DomainException("Invalid value for config('$configKey'')");
59
    }
60
61
    /**
62
     * Resources getter.
63
     *
64
     * @return \Illuminate\Support\Collection
65
     * @throws \Exception
66
     */
67
    public function getResources()
68
    {
69
        $this->load();
70
71
        return $this->resources;
72
    }
73
74
    /**
75
     * Load all resources.
76
     *
77
     * @return \Illuminate\Support\Collection
78
     * @throws \Exception
79
     */
80
    public function load()
81
    {
82
        if (! empty($this->resources)) {
83
            return $this->resources;
84
        }
85
86
        return $this->resources = $this->sanitizeResources(
87
            $this->getEnabledResources($this->loadResourceFiles())
88
        );
89
    }
90
91
    /**
92
     * Load resources in files.
93
     *
94
     * @return \Illuminate\Support\Collection
95
     */
96
    private function loadResourceFiles()
97
    {
98
        return $this->replaceExecutableCode(
99
            $this->loadResourcesFiles()
100
        )->mapWithKeys(function ($value, $key) {
101
            return [$this->removeExtension($key) => $value];
102
        });
103
    }
104
105
    /**
106
     * Replace executable code.
107
     *
108
     * @return \Illuminate\Support\Collection
109
     */
110
    private function replaceExecutableCode($files)
111
    {
112
        return $files->map(function ($item) {
113
            return collect($item)
114
                ->map(function ($value) {
115
                    return (new Resolver())->recursivelyFindAndReplaceExecutableCode(
116
                        $value
117
                    );
118
                })
119
                ->all();
120
        });
121
    }
122
123
    /**
124
     * Load Resources.
125
     *
126
     * @return \Illuminate\Support\Collection
127
     * @throws \Exception
128
     */
129
    public function loadResources()
130
    {
131
        return $this->load();
132
    }
133
134
    /**
135
     * Load resource files.
136
     *
137
     * @return \Illuminate\Support\Collection
138
     */
139
    private function loadResourcesFiles()
140
    {
141
        $files = $this->yaml->loadFromDirectory(
142
            config('health.resources.path')
143
        );
144
145
        $files =
146
            $files->count() == 0
147
                ? $this->yaml->loadFromDirectory(package_resources_dir())
148
                : $files;
149
150
        return $files;
151
    }
152
153
    /**
154
     * Remove extension from file name.
155
     *
156
     * @param $key
157
     * @return string
158
     */
159
    private function removeExtension($key)
160
    {
161
        return preg_replace('/\.[^.]+$/', '', $key);
162
    }
163
164
    /**
165
     * Sanitize resource key.
166
     *
167
     * @param $key
168
     * @return string
169
     */
170
    private function sanitizeKey($key)
171
    {
172
        return $key;
173
    }
174
175
    /**
176
     * Sanitize resources.
177
     *
178
     * @param $resources
179
     * @return \Illuminate\Support\Collection
180
     */
181
    private function sanitizeResources($resources)
182
    {
183
        return $resources->map(function ($resource) {
184
            return collect($resource)->mapWithKeys(function ($value, $key) {
185
                return [$this->sanitizeKey($key) => $value];
186
            });
187
        });
188
    }
189
}
190