Completed
Push — dev ( 7529c4...0154aa )
by Zach
03:54
created

CacheBuster::validateJsonData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 3
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Larafolio\Http;
4
5
class CacheBuster
6
{
7
    /**
8
     * Resolve path for versioned resource file.
9
     *
10
     * @param string $path     Unversioned file path.
11
     * @param string $jsonData Json object.
12
     * @param string $root     Location of files in /public.
13
     *
14
     * @return string
15
     */
16
    public function resolvePath($path, $jsonData, $root)
17
    {
18
        $file = trim(str_replace($root, '', $path), '/');
19
20
        $jsonData = $this->validateJsonData($path, $jsonData, $root);
21
22
        $data = json_decode($jsonData, true);
23
24
        if (array_key_exists($file, $data)) {
25
            return '/'.$root.'/'.$data[$file];
26
        }
27
28
        return elixir($path);
29
    }
30
31
    /**
32
     * Get json data from rev manifest and check for null.
33
     * 
34
     * @param string $path     Unversioned file path.
35
     * @param string $jsonData Json object.
36
     * @param string $root     Location of files in /public.
37
     *
38
     * @return string
39
     */
40
    protected function validateJsonData($path, $jsonData, $root)
41
    {
42
        if ($jsonData === null && file_exists(public_path($root.'/rev-manifest.json'))) {
43
            $jsonData = file_get_contents(public_path($root.'/rev-manifest.json'));
44
        }
45
46
        if ($jsonData === null) {
47
            abort(500, "Resource {$path} could not be resolved.");
48
        }
49
50
        return $jsonData;
51
    }
52
}
53