Passed
Push — master ( 1438ee...6641ad )
by Quentin
06:42 queued 12s
created

Twill::readJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace A17\Twill\Services\Assets;
4
5
use Illuminate\Support\Facades\Cache;
6
7
class Twill
8
{
9
    function asset($file)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
10
    {
11
        return $this->devAsset($file) ?? $this->twillAsset($file);
12
    }
13
14
    public function twillAsset($file)
15
    {
16
        $manifest = $this->readManifest();
17
18
        if (isset($manifest[$file])) {
19
            return $manifest[$file];
20
        }
21
22
        return '/' . config('twill.public_directory', 'twill') . '/' . $file;
23
    }
24
25
    public function getManifestFilename()
26
    {
27
        $fileName =
28
            public_path(config('twill.public_directory', 'twill')) .
29
            '/' .
30
            config('twill.manifest_file', 'twill-manifest.json');
31
32
        if (file_exists($fileName)) {
33
            return $fileName;
34
        }
35
36
        return base_path(
37
            'vendor/area17/twill/dist/assets/admin/twill-manifest.json'
38
        );
39
    }
40
41
    public function devAsset($file)
42
    {
43
        if (!$this->devMode()) {
44
            return null;
45
        }
46
47
        $devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080');
48
49
        try {
50
            $manifest = $this->readJson(
51
                $devServerUrl .
52
                    '/' .
53
                    config('twill.manifest_file', 'twill-manifest.json')
54
            );
55
        } catch (\Exception $e) {
56
            throw new \Exception(
57
                'Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.'
58
            );
59
        }
60
61
        return $devServerUrl . ($manifest[$file] ?? '/' . $file);
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67
    private function readManifest()
68
    {
69
        try {
70
            return Cache::rememberForever('twill-manifest', function () {
71
                return $this->readJson($this->getManifestFilename());
72
            });
73
        } catch (\Exception $e) {
74
            throw new \Exception(
75
                'Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.'
76
            );
77
        }
78
    }
79
80
    private function readJson($fileName)
81
    {
82
        return json_decode(file_get_contents($fileName), true);
83
    }
84
85
    private function devMode()
86
    {
87
        return app()->environment('local', 'development') &&
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        return app()->/** @scrutinizer ignore-call */ environment('local', 'development') &&
Loading history...
88
            config('twill.dev_mode', false);
89
    }
90
}
91