Passed
Pull Request — master (#72)
by Mark
08:01
created

ResourceLoader::replaceExecutableCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Health\Support;
4
5
use DomainException;
6
use Illuminate\Support\Str;
7
use PragmaRX\Yaml\Package\Yaml;
8
use PragmaRX\Yaml\Package\Support\Resolver;
9
10
class ResourceLoader
11
{
12
    /**
13
     * Yaml service.
14
     *
15
     * @var Yaml
16
     */
17
    protected $yaml;
18
19
    /**
20
     * @var \Illuminate\Support\Collection
21
     */
22
    private $resources;
23
24
    /**
25
     * ResourceLoader constructor.
26
     *
27
     * @param Yaml $yaml
28
     */
29 13
    public function __construct(Yaml $yaml)
30
    {
31 13
        $this->yaml = $yaml;
32 13
    }
33
34
    /**
35
     * Can load resources?
36
     *
37
     * @param $what
38
     * @param $type
39
     * @return bool
40
     */
41 13
    private function canLoadResources($what, $type)
42
    {
43 13
        return $this->shouldLoadAnyType($type) ||
44
               $this->isArrayLoader($what, $type) ||
45 13
               $this->isFileLoader($what, $type);
46
    }
47
48
    /**
49
     * Get enabled resources.
50
     *
51
     * @param $resources
52
     * @return \Illuminate\Support\Collection
53
     * @throws \Exception
54
     */
55 13
    private function getEnabledResources($resources)
56
    {
57 13
        if (is_array($keys = config($configKey = 'health.resources_enabled'))) {
58 1
            return collect($resources)->reject(function ($value, $key) use ($keys) {
59 1
                return ! in_array($key, $keys);
60 1
            });
61
        }
62
63 13
        if ($keys == Constants::RESOURCES_ENABLED_ALL) {
64 13
            return collect($resources);
65
        }
66
67 1
        throw new DomainException("Invalid value for config('$configKey'')");
68
    }
69
70
    /**
71
     * Resources getter.
72
     *
73
     * @return \Illuminate\Support\Collection
74
     */
75 13
    public function getResources()
76
    {
77 13
        $this->load();
78
79 13
        return $this->resources;
80
    }
81
82
    /**
83
     * Is it an array loader?
84
     *
85
     * @param $what
86
     * @param $type
87
     * @return bool
88
     */
89
    private function isArrayLoader($what, $type)
90
    {
91
        return $what == Constants::ARRAY_RESOURCE &&
92
                $type == Constants::RESOURCES_TYPE_ARRAY;
93
    }
94
95
    /**
96
     * Is it a file loader?
97
     *
98
     * @param $what
99
     * @param $type
100
     * @return bool
101
     */
102
    private function isFileLoader($what, $type)
103
    {
104
        return $what == Constants::FILES_RESOURCE &&
105
                $type == Constants::RESOURCES_TYPE_FILES;
106
    }
107
108
    /**
109
     * Load all resources.
110
     *
111
     * @return \Illuminate\Support\Collection
112
     */
113 13
    public function load()
114
    {
115 13
        if (! empty($this->resources)) {
116 5
            return $this->resources;
117
        }
118
119 13
        $type = config('health.resources_location.type');
120
121 13
        return $this->resources = $this->sanitizeResources(
122 13
            $this->getEnabledResources(
123 13
                $this->loadResourcesFrom(
124 13
                    Constants::FILES_RESOURCE,
125 13
                    $type,
126 13
                    $this->loadResourcesFrom(
127 13
                        Constants::ARRAY_RESOURCE,
128 13
                        $type, $this->resources
129
                    )
130
                )
131
            )
132
        );
133
    }
134
135
    /**
136
     * Load arrays and files?
137
     *
138
     * @param $what
139
     * @return bool
140
     */
141 13
    private function shouldLoadAnyType($what)
142
    {
143 13
        return $what == Constants::RESOURCES_TYPE_BOTH;
144
    }
145
146
    /**
147
     * Load resources in array.
148
     *
149
     * @return \Illuminate\Support\Collection
150
     */
151
    private function loadArray()
152
    {
153 13
        return collect(config('health.resources'))->mapWithKeys(function ($value, $key) {
154
            return [studly_case($key) => $value];
155 13
        });
156
    }
157
158
    /**
159
     * Load resources in files.
160
     *
161
     * @return \Illuminate\Support\Collection
162
     */
163 13
    private function loadFiles()
164
    {
165 13
        return $this->replaceExecutableCode($this->loadResourcesFiles())
166 13
            ->mapWithKeys(function ($value, $key) {
167 13
                return [$this->removeExtension($key) => $value];
168 13
            });
169
    }
170
171
    /**
172
     * Replace executable code.
173
     *
174
     * @return \Illuminate\Support\Collection
175
     */
176
    private function replaceExecutableCode($files)
177
    {
178
        return $files->map(function ($item) {
179 13
            return collect($item)->map(function ($value) {
180 13
                return (new Resolver)->recursivelyFindAndReplaceExecutableCode($value);
181 13
            })->all();
182 13
        });
183
    }
184
185
    /**
186
     * Load Resources.
187
     *
188
     * @return \Illuminate\Support\Collection
189
     */
190 5
    public function loadResources()
191
    {
192 5
        return $this->sortResources($this->makeResourcesCollection());
193
    }
194
195
    /**
196
     * Load resource files.
197
     *
198
     * @return \Illuminate\Support\Collection
199
     */
200 13
    private function loadResourcesFiles()
201
    {
202 13
        $files = $this->yaml->loadFromDirectory(config('health.resources_location.path'));
203
204 13
        $files = $files->count() == 0
205 13
            ? $this->yaml->loadFromDirectory(package_resources_dir())
206 13
            : $files;
207
208 13
        return $files;
209
    }
210
211
    /**
212
     * Load resources for a particular type.
213
     *
214
     * @param $what
215
     * @param $resources
216
     * @return \Illuminate\Support\Collection
217
     */
218 13
    private function loadResourcesForType($what, $resources)
219
    {
220 13
        return collect(
221 13
            $what == Constants::ARRAY_RESOURCE
222 13
            ? collect($resources)->merge($this->loadArray())
223 13
            : collect($resources)->merge($this->loadFiles())
224
        );
225
    }
226
227
    /**
228
     * Load resources from array.
229
     *
230
     * @param $what
231
     * @param $type
232
     * @param $resources
233
     * @return array
234
     */
235 13
    private function loadResourcesFrom($what, $type, $resources)
236
    {
237 13
        return $this->canLoadResources($what, $type)
238 13
                ? $this->loadResourcesForType($what, $resources)
239 13
                : $resources;
240
    }
241
242
    /**
243
     * Create a resources collection.
244
     *
245
     * @return \Illuminate\Support\Collection
246
     */
247
    protected function makeResourcesCollection()
248
    {
249 5
        return collect($this->load())->map(function ($item, $key) {
250 5
            $item['slug'] = $key;
251
252 5
            $item['name'] = Str::studly($key);
253
254 5
            $item['is_global'] = (isset($item['is_global']) && $item['is_global']);
255
256 5
            return $item;
257 5
        });
258
    }
259
260
    /**
261
     * Remove extension from file name.
262
     *
263
     * @param $key
264
     * @return string
265
     */
266 13
    private function removeExtension($key)
267
    {
268 13
        return preg_replace('/\.[^.]+$/', '', $key);
269
    }
270
271
    /**
272
     * Sanitize resource key.
273
     *
274
     * @param $key
275
     * @return string
276
     */
277 13
    private function sanitizeKey($key)
278
    {
279 13
        if ($key == 'column_size') {
280 13
            return 'columnSize';
281
        }
282
283 13
        return $key;
284
    }
285
286
    /**
287
     * Sanitize resources.
288
     *
289
     * @param $resources
290
     * @return \Illuminate\Support\Collection
291
     */
292
    private function sanitizeResources($resources)
293
    {
294
        return $resources->map(function ($resource) {
295 13
            return collect($resource)->mapWithKeys(function ($value, $key) {
296 13
                return [$this->sanitizeKey($key) => $value];
297 13
            });
298 13
        });
299
    }
300
301
    /**
302
     * Sort resources.
303
     *
304
     * @param $resources
305
     * @return \Illuminate\Support\Collection
306
     */
307 5
    protected function sortResources($resources)
308
    {
309 5
        if ($sortBy = config('health.sort_by')) {
310 4
            $resources = $resources->sortBy(function ($item) use ($sortBy) {
311 4
                return $item['is_global']
312 4
                    ? 0 .$item[$sortBy]
313 4
                    : 1 .$item[$sortBy];
314 4
            });
315
316 4
            return $resources;
317
        }
318
319 1
        return $resources;
320
    }
321
}
322