Completed
Pull Request — master (#30)
by
unknown
03:51
created

CdnFacade::mix()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 1
1
<?php
2
3
namespace Rehmatworks\laravelcdn;
4
5
use Illuminate\Support\Facades\Request;
6
use Rehmatworks\laravelcdn\Contracts\CdnFacadeInterface;
7
use Rehmatworks\laravelcdn\Contracts\CdnHelperInterface;
8
use Rehmatworks\laravelcdn\Contracts\ProviderFactoryInterface;
9
use Rehmatworks\laravelcdn\Exceptions\EmptyPathException;
10
use Rehmatworks\laravelcdn\Validators\CdnFacadeValidator;
11
12
/**
13
 * Class CdnFacade.
14
 *
15
 * @category
16
 *
17
 * @author  Mahmoud Zalt <[email protected]>
18
 */
19
class CdnFacade implements CdnFacadeInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $configurations;
25
26
    /**
27
     * @var \Rehmatworks\laravelcdn\Contracts\ProviderFactoryInterface
28
     */
29
    protected $provider_factory;
30
31
    /**
32
     * instance of the default provider object.
33
     *
34
     * @var \Rehmatworks\laravelcdn\Providers\Contracts\ProviderInterface
35
     */
36
    protected $provider;
37
38
    /**
39
     * @var \Rehmatworks\laravelcdn\Contracts\CdnHelperInterface
40
     */
41
    protected $helper;
42
43
    /**
44
     * @var \Rehmatworks\laravelcdn\Validators\CdnFacadeValidator
45
     */
46
    protected $cdn_facade_validator;
47
48
    /**
49
     * Calls the provider initializer.
50
     *
51
     * @param \Rehmatworks\laravelcdn\Contracts\ProviderFactoryInterface $provider_factory
52
     * @param \Rehmatworks\laravelcdn\Contracts\CdnHelperInterface       $helper
53
     * @param \Rehmatworks\laravelcdn\Validators\CdnFacadeValidator      $cdn_facade_validator
54
     */
55
    public function __construct(
56
        ProviderFactoryInterface $provider_factory,
57
        CdnHelperInterface $helper,
58
        CdnFacadeValidator $cdn_facade_validator
59
    ) {
60
        $this->provider_factory = $provider_factory;
61
        $this->helper = $helper;
62
        $this->cdn_facade_validator = $cdn_facade_validator;
63
64
        $this->init();
65
    }
66
67
    /**
68
     * Read the configuration file and pass it to the provider factory
69
     * to return an object of the default provider specified in the
70
     * config file.
71
     */
72
    private function init()
73
    {
74
        // return the configurations from the config file
75
        $this->configurations = $this->helper->getConfigurations();
76
77
        // return an instance of the corresponding Provider concrete according to the configuration
78
        $this->provider = $this->provider_factory->create($this->configurations);
79
    }
80
81
    /**
82
     * this function will be called from the 'views' using the
83
     * 'Cdn' facade {{Cdn::asset('')}} to convert the path into
84
     * it's CDN url.
85
     *
86
     * @param $path
87
     *
88
     * @throws Exceptions\EmptyPathException
89
     *
90
     * @return mixed
91
     */
92
    public function asset($path)
93
    {
94
        // if asset always append the public/ dir to the path (since the user should not add public/ to asset)
95
        return $this->generateUrl($path, 'public/');
96
    }
97
98
    /**
99
     * check if package is surpassed or not then
100
     * prepare the path before generating the url.
101
     *
102
     * @param        $path
103
     * @param string $prepend
104
     *
105
     * @return mixed
106
     */
107
    private function generateUrl($path, $prepend = '')
108
    {
109
        // if the package is surpassed, then return the same $path
110
        // to load the asset from the localhost
111
        if (isset($this->configurations['bypass']) && $this->configurations['bypass']) {
112
            return Request::root().'/'.$path;
113
        }
114
115
        if (!isset($path)) {
116
            throw new EmptyPathException('Path does not exist.');
117
        }
118
119
        // Add version number
120
        //$path = str_replace(
121
        //    "build",
122
        //    $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...
123
        //    $path
124
        //);
125
126
        // remove slashes from begging and ending of the path
127
        // and append directories if needed
128
        $clean_path = $prepend.$this->helper->cleanPath($path);
129
130
        // call the provider specific url generator
131
        return $this->provider->urlGenerator($clean_path);
132
    }
133
134
    /**
135
     * this function will be called from the 'views' using the
136
     * 'Cdn' facade {{Cdn::mix('')}} to convert the Laravel 5.4 webpack mix
137
     * generated file path into it's CDN url.
138
     *
139
     * @param $path
140
     *
141
     * @return mixed
142
     *
143
     * @throws Exceptions\EmptyPathException, \InvalidArgumentException
144
     */
145
    public function mix($path)
146
    {
147
        static $manifest = null;
148
        if (is_null($manifest)) {
149
            $manifest = json_decode(file_get_contents(public_path('mix-manifest.json')), true);
150
        }
151
        if (isset($manifest['/' . $path])) {
152
            return $this->generateUrl($manifest['/' . $path], 'public/');
153
        }
154
        if (isset($manifest[$path])) {
155
            return $this->generateUrl($manifest[$path], 'public/');
156
        }
157
        throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
158
    }
159
160
    /**
161
     * this function will be called from the 'views' using the
162
     * 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into
163
     * it's CDN url.
164
     *
165
     * @param $path
166
     *
167
     * @throws Exceptions\EmptyPathException, \InvalidArgumentException
168
     *
169
     * @return mixed
170
     */
171
    public function elixir($path)
172
    {
173
        static $manifest = null;
174
        if (is_null($manifest)) {
175
            $manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
176
        }
177
        if (isset($manifest[$path])) {
178
            return $this->generateUrl('build/' . $manifest[$path], 'public/');
179
        }
180
        throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
181
    }
182
183
    /**
184
     * this function will be called from the 'views' using the
185
     * 'Cdn' facade {{Cdn::path('')}} to convert the path into
186
     * it's CDN url.
187
     *
188
     * @param $path
189
     *
190
     * @throws Exceptions\EmptyPathException
191
     *
192
     * @return mixed
193
     */
194
    public function path($path)
195
    {
196
        return $this->generateUrl($path);
197
    }
198
}
199