Completed
Pull Request — master (#116)
by Christopher
01:20
created

CdnFacade::mix()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.439
cc 5
eloc 15
nc 10
nop 1
1
<?php
2
3
namespace Vinelab\Cdn;
4
5
use Illuminate\Support\Facades\Request;
6
use Illuminate\Support\HtmlString;
7
use Vinelab\Cdn\Contracts\CdnFacadeInterface;
8
use Vinelab\Cdn\Contracts\CdnHelperInterface;
9
use Vinelab\Cdn\Contracts\ProviderFactoryInterface;
10
use Vinelab\Cdn\Exceptions\EmptyPathException;
11
use Vinelab\Cdn\Validators\CdnFacadeValidator;
12
13
/**
14
 * Class CdnFacade.
15
 *
16
 * @category
17
 *
18
 * @author  Mahmoud Zalt <[email protected]>
19
 */
20
class CdnFacade implements CdnFacadeInterface
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $configurations;
26
27
    /**
28
     * @var \Vinelab\Cdn\Contracts\ProviderFactoryInterface
29
     */
30
    protected $provider_factory;
31
32
    /**
33
     * instance of the default provider object.
34
     *
35
     * @var \Vinelab\Cdn\Providers\Contracts\ProviderInterface
36
     */
37
    protected $provider;
38
39
    /**
40
     * @var \Vinelab\Cdn\Contracts\CdnHelperInterface
41
     */
42
    protected $helper;
43
44
    /**
45
     * @var \Vinelab\Cdn\Validators\CdnFacadeValidator
46
     */
47
    protected $cdn_facade_validator;
48
49
    /**
50
     * Calls the provider initializer.
51
     *
52
     * @param \Vinelab\Cdn\Contracts\ProviderFactoryInterface $provider_factory
53
     * @param \Vinelab\Cdn\Contracts\CdnHelperInterface       $helper
54
     * @param \Vinelab\Cdn\Validators\CdnFacadeValidator      $cdn_facade_validator
55
     */
56
    public function __construct(
57
        ProviderFactoryInterface $provider_factory,
58
        CdnHelperInterface $helper,
59
        CdnFacadeValidator $cdn_facade_validator
60
    ) {
61
        $this->provider_factory = $provider_factory;
62
        $this->helper = $helper;
63
        $this->cdn_facade_validator = $cdn_facade_validator;
64
65
        $this->init();
66
    }
67
68
    /**
69
     * this function will be called from the 'views' using the
70
     * 'Cdn' facade {{Cdn::asset('')}} to convert the path into
71
     * it's CDN url.
72
     *
73
     * @param $path
74
     *
75
     * @return mixed
76
     *
77
     * @throws Exceptions\EmptyPathException
78
     */
79
    public function asset($path)
80
    {
81
        return $this->generateUrl($path);
82
    }
83
84
	/**
85
     * this function will be called from the 'views' using the
86
     * 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into
87
     * it's CDN url.
88
     *
89
     * @param $path
90
     *
91
     * @return mixed
92
     *
93
     * @throws Exceptions\EmptyPathException, \InvalidArgumentException
94
     */
95
	public function elixir($path)
96
    {
97
        static $manifest = null;
98
        if (is_null($manifest)) {
99
            $manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
100
        }
101
        if (isset($manifest[$path])) {
102
            return $this->generateUrl('build/' . $manifest[$path]);
103
        }
104
        throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
105
    }
106
107
	/**
108
     * this function will be called from the 'views' using the
109
     * 'Cdn' facade {{Cdn::mix('')}} to convert the Laravel 5.4 webpack mix
110
     * generated file path into it's CDN url.
111
     *
112
     * @param $path
113
     *
114
     * @return mixed
115
     *
116
     * @throws Exceptions\EmptyPathException, \InvalidArgumentException
117
     */
118
	public function mix($path)
119
    {
120
        static $manifests = [];
121
122
        if (! starts_with($path, '/')) {
123
            $path = "/{$path}";
124
        }
125
126
        $manifestPath = public_path('/mix-manifest.json');
127
128
        if (! isset($manifests[$manifestPath])) {
129
            if (! file_exists($manifestPath)) {
130
                throw new Exception('The Mix manifest does not exist.');
131
            }
132
133
            $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
134
        }
135
136
        $manifest = $manifests[$manifestPath];
137
138
        if (! isset($manifest[$path])) {
139
            throw new \InvalidArgumentException(
140
                "Unable to locate Mix file: {$path}. Please check your ".
141
                'webpack.mix.js output paths and try again.'
142
            );
143
        }
144
        return $this->generateUrl($manifest[$path]);
145
    }
146
147
    /**
148
     * this function will be called from the 'views' using the
149
     * 'Cdn' facade {{Cdn::path('')}} to convert the path into
150
     * it's CDN url.
151
     *
152
     * @param $path
153
     *
154
     * @return mixed
155
     *
156
     * @throws Exceptions\EmptyPathException
157
     */
158
    public function path($path)
159
    {
160
        return $this->generateUrl($path);
161
    }
162
163
    /**
164
     * check if package is surpassed or not then
165
     * prepare the path before generating the url.
166
     *
167
     * @param        $path
168
     *
169
     * @return mixed
170
     */
171
    private function generateUrl($path)
172
    {
173
        // if the package is surpassed, then return the same $path
174
        // to load the asset from the localhost
175
        if (isset($this->configurations['bypass']) && $this->configurations['bypass']) {
176
            return new HtmlString(asset($path));
177
        }
178
179
        if (!isset($path)) {
180
            throw new EmptyPathException('Path does not exist.');
181
        }
182
183
        // Add version number
184
        //$path = str_replace(
185
        //    "build",
186
        //    $this->configurations['providers']['aws']['s3']['version'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
187
        //    $path
188
        //);
189
190
        // remove slashes from begging and ending of the path
191
        // and append directories if needed
192
        $clean_path = $this->helper->cleanPath($path);
193
194
        // call the provider specific url generator
195
        return $this->provider->urlGenerator($clean_path);
196
    }
197
198
    /**
199
     * Read the configuration file and pass it to the provider factory
200
     * to return an object of the default provider specified in the
201
     * config file.
202
     */
203
    private function init()
204
    {
205
        // return the configurations from the config file
206
        $this->configurations = $this->helper->getConfigurations();
207
208
        // return an instance of the corresponding Provider concrete according to the configuration
209
        $this->provider = $this->provider_factory->create($this->configurations);
210
    }
211
}
212