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

CdnFacade::mix()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 3
eloc 7
nc 4
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 View Code Duplication
	public function elixir($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
	public function mix($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        static $manifest = null;
122
        if (is_null($manifest)) {
123
            $manifest = json_decode(file_get_contents(public_path('mix-manifest.json')), true);
124
        }
125
        if (isset($manifest[$path])) {
126
            return $this->generateUrl($manifest[$path], 'public/');
127
        }
128
        throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
129
    }
130
131
    /**
132
     * this function will be called from the 'views' using the
133
     * 'Cdn' facade {{Cdn::path('')}} to convert the path into
134
     * it's CDN url.
135
     *
136
     * @param $path
137
     *
138
     * @return mixed
139
     *
140
     * @throws Exceptions\EmptyPathException
141
     */
142
    public function path($path)
143
    {
144
        return $this->generateUrl($path);
145
    }
146
147
    /**
148
     * check if package is surpassed or not then
149
     * prepare the path before generating the url.
150
     *
151
     * @param        $path
152
     * @param string $prepend
153
     *
154
     * @return mixed
155
     */
156
    private function generateUrl($path, $prepend = '')
157
    {
158
        // if the package is surpassed, then return the same $path
159
        // to load the asset from the localhost
160
        if (isset($this->configurations['bypass']) && $this->configurations['bypass']) {
161
            return new HtmlString(asset($path));
162
        }
163
164
        if (!isset($path)) {
165
            throw new EmptyPathException('Path does not exist.');
166
        }
167
168
        // Add version number
169
        //$path = str_replace(
170
        //    "build",
171
        //    $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...
172
        //    $path
173
        //);
174
175
        // remove slashes from begging and ending of the path
176
        // and append directories if needed
177
        $clean_path = $prepend.$this->helper->cleanPath($path);
178
179
        // call the provider specific url generator
180
        return $this->provider->urlGenerator($clean_path);
181
    }
182
183
    /**
184
     * Read the configuration file and pass it to the provider factory
185
     * to return an object of the default provider specified in the
186
     * config file.
187
     */
188
    private function init()
189
    {
190
        // return the configurations from the config file
191
        $this->configurations = $this->helper->getConfigurations();
192
193
        // return an instance of the corresponding Provider concrete according to the configuration
194
        $this->provider = $this->provider_factory->create($this->configurations);
195
    }
196
}
197