Passed
Push — master ( 2159a4...9e36bb )
by ANTHONIUS
06:11
created

Mix   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 62
rs 10
c 1
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 50 11
1
<?php
2
3
/*
4
 * This file is part of the EOffice project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace EOffice\Packages\UI;
15
16
use Exception;
17
use Illuminate\Support\HtmlString;
18
use Illuminate\Support\Str;
19
20
class Mix
21
{
22
    /**
23
     * Get the path to a versioned Mix file.
24
     *
25
     * @param string $path
26
     * @param string $manifestDirectory
27
     *
28
     * @throws \Exception
29
     *
30
     * @return \Illuminate\Support\HtmlString|string
31
     */
32
    public function __invoke($path, $manifestDirectory = '')
33
    {
34
        static $manifests = [];
35
36
        if ( ! Str::startsWith($path, '/')) {
37
            $path = "/{$path}";
38
        }
39
40
        if ($manifestDirectory && ! Str::startsWith($manifestDirectory, '/')) {
41
            $manifestDirectory = "/{$manifestDirectory}";
42
        }
43
44
        if (file_exists(public_path($manifestDirectory.'/hot'))) {
45
            $url = rtrim(file_get_contents(public_path($manifestDirectory.'/hot')));
46
47
            if ('local' !== env('APP_ENV')) {
0 ignored issues
show
introduced by
The condition 'local' !== env('APP_ENV') is always true.
Loading history...
48
                if (Str::startsWith($url, ['http://', 'https://'])) {
49
                    return new HtmlString(Str::after($url, ':').$path);
50
                }
51
52
                return new HtmlString("http://localhost:8080{$path}");
53
            }
54
55
            return "http://localhost:8080{$path}";
56
        }
57
58
        $manifestPath = public_path($manifestDirectory.'/mix-manifest.json');
59
60
        if ( ! isset($manifests[$manifestPath])) {
61
            if ( ! file_exists($manifestPath)) {
62
                throw new Exception('The Mix manifest does not exist.');
63
            }
64
65
            $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
66
        }
67
68
        $manifest = $manifests[$manifestPath];
69
70
        if ( ! isset($manifest[$path])) {
71
            $exception = new Exception("Unable to locate Mix file: {$path}.");
72
73
            if ( ! app('config')->get('app.debug')) {
74
                report($exception);
75
76
                return $path;
77
            }
78
            throw $exception;
79
        }
80
81
        return new HtmlString(app('config')->get('app.mix_url').$manifestDirectory.$manifest[$path]);
82
    }
83
}
84