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

CdnFacade::asset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
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
        // if asset always append the public/ dir to the path (since the user should not add public/ to asset)
82
        return $this->generateUrl($path, 'public/');
83
    }
84
	
85
	/**
86
     * this function will be called from the 'views' using the
87
     * 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into
88
     * it's CDN url.
89
     *
90
     * @param $path
91
     *
92
     * @return mixed
93
     *
94
     * @throws Exceptions\EmptyPathException, \InvalidArgumentException
95
     */
96
	public function elixir($path)
97
    {
98
        static $manifest = null;
99
        if (is_null($manifest)) {
100
            $manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
101
        }
102
        if (isset($manifest[$path])) {
103
            return $this->generateUrl('build/' . $manifest[$path], 'public/');
104
        }
105
        throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
106
    }
107
	
108
	/**
109
     * this function will be called from the 'views' using the
110
     * 'Cdn' facade {{Cdn::mix('')}} to convert the Laravel 5.4 webpack mix
111
     * generated file path into it's CDN url.
112
     *
113
     * @param $path
114
     *
115
     * @return mixed
116
     *
117
     * @throws Exceptions\EmptyPathException, \InvalidArgumentException
118
     */
119
	public function mix($path)
120
    {
121
        static $manifests = [];
122
123
        if (! starts_with($path, '/')) {
124
            $path = "/{$path}";
125
        }
126
127
        $manifestPath = public_path('/mix-manifest.json');
128
129
        if (! isset($manifests[$manifestPath])) {
130
            if (! file_exists($manifestPath)) {
131
                throw new Exception('The Mix manifest does not exist.');
132
            }
133
134
            $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
135
        }
136
137
        $manifest = $manifests[$manifestPath];
138
139
        if (! isset($manifest[$path])) {
140
            throw new \InvalidArgumentException(
141
                "Unable to locate Mix file: {$path}. Please check your ".
142
                'webpack.mix.js output paths and try again.'
143
            );
144
        }
145
        return $this->generateUrl($manifest[$path], 'public/');
146
    }
147
148
    /**
149
     * this function will be called from the 'views' using the
150
     * 'Cdn' facade {{Cdn::path('')}} to convert the path into
151
     * it's CDN url.
152
     *
153
     * @param $path
154
     *
155
     * @return mixed
156
     *
157
     * @throws Exceptions\EmptyPathException
158
     */
159
    public function path($path)
160
    {
161
        return $this->generateUrl($path);
162
    }
163
164
    /**
165
     * check if package is surpassed or not then
166
     * prepare the path before generating the url.
167
     *
168
     * @param        $path
169
     * @param string $prepend
170
     *
171
     * @return mixed
172
     */
173
    private function generateUrl($path, $prepend = '')
174
    {
175
        // if the package is surpassed, then return the same $path
176
        // to load the asset from the localhost
177
        if (isset($this->configurations['bypass']) && $this->configurations['bypass']) {
178
            return new HtmlString(asset($path));
179
        }
180
181
        if (!isset($path)) {
182
            throw new EmptyPathException('Path does not exist.');
183
        }
184
185
        // Add version number
186
        //$path = str_replace(
187
        //    "build",
188
        //    $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...
189
        //    $path
190
        //);
191
192
        // remove slashes from begging and ending of the path
193
        // and append directories if needed
194
        $clean_path = $prepend.$this->helper->cleanPath($path);
195
196
        // call the provider specific url generator
197
        return $this->provider->urlGenerator($clean_path);
198
    }
199
200
    /**
201
     * Read the configuration file and pass it to the provider factory
202
     * to return an object of the default provider specified in the
203
     * config file.
204
     */
205
    private function init()
206
    {
207
        // return the configurations from the config file
208
        $this->configurations = $this->helper->getConfigurations();
209
210
        // return an instance of the corresponding Provider concrete according to the configuration
211
        $this->provider = $this->provider_factory->create($this->configurations);
212
    }
213
}
214