Completed
Pull Request — master (#116)
by Christopher
05:30
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 Vinelab\Cdn\Contracts\CdnFacadeInterface;
7
use Vinelab\Cdn\Contracts\CdnHelperInterface;
8
use Vinelab\Cdn\Contracts\ProviderFactoryInterface;
9
use Vinelab\Cdn\Exceptions\EmptyPathException;
10
use Vinelab\Cdn\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 \Vinelab\Cdn\Contracts\ProviderFactoryInterface
28
     */
29
    protected $provider_factory;
30
31
    /**
32
     * instance of the default provider object.
33
     *
34
     * @var \Vinelab\Cdn\Providers\Contracts\ProviderInterface
35
     */
36
    protected $provider;
37
38
    /**
39
     * @var \Vinelab\Cdn\Contracts\CdnHelperInterface
40
     */
41
    protected $helper;
42
43
    /**
44
     * @var \Vinelab\Cdn\Validators\CdnFacadeValidator
45
     */
46
    protected $cdn_facade_validator;
47
48
    /**
49
     * Calls the provider initializer.
50
     *
51
     * @param \Vinelab\Cdn\Contracts\ProviderFactoryInterface $provider_factory
52
     * @param \Vinelab\Cdn\Contracts\CdnHelperInterface       $helper
53
     * @param \Vinelab\Cdn\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
     * this function will be called from the 'views' using the
69
     * 'Cdn' facade {{Cdn::asset('')}} to convert the path into
70
     * it's CDN url.
71
     *
72
     * @param $path
73
     *
74
     * @return mixed
75
     *
76
     * @throws Exceptions\EmptyPathException
77
     */
78
    public function asset($path)
79
    {
80
        // if asset always append the public/ dir to the path (since the user should not add public/ to asset)
81
        return $this->generateUrl($path, 'public/');
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 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...
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], 'public/');
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 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...
119
    {
120
        static $manifest = null;
121
        if (is_null($manifest)) {
122
            $manifest = json_decode(file_get_contents(public_path('mix-manifest.json')), true);
123
        }
124
        if (isset($manifest[$path])) {
125
            return $this->generateUrl($manifest[$path], 'public/');
126
        }
127
        throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
128
    }
129
130
    /**
131
     * this function will be called from the 'views' using the
132
     * 'Cdn' facade {{Cdn::path('')}} to convert the path into
133
     * it's CDN url.
134
     *
135
     * @param $path
136
     *
137
     * @return mixed
138
     *
139
     * @throws Exceptions\EmptyPathException
140
     */
141
    public function path($path)
142
    {
143
        return $this->generateUrl($path);
144
    }
145
146
    /**
147
     * check if package is surpassed or not then
148
     * prepare the path before generating the url.
149
     *
150
     * @param        $path
151
     * @param string $prepend
152
     *
153
     * @return mixed
154
     */
155
    private function generateUrl($path, $prepend = '')
156
    {
157
        // if the package is surpassed, then return the same $path
158
        // to load the asset from the localhost
159
        if (isset($this->configurations['bypass']) && $this->configurations['bypass']) {
160
            return Request::root().'/'.$path;
161
        }
162
163
        if (!isset($path)) {
164
            throw new EmptyPathException('Path does not exist.');
165
        }
166
167
        // Add version number
168
        //$path = str_replace(
169
        //    "build",
170
        //    $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...
171
        //    $path
172
        //);
173
174
        // remove slashes from begging and ending of the path
175
        // and append directories if needed
176
        $clean_path = $prepend.$this->helper->cleanPath($path);
177
178
        // call the provider specific url generator
179
        return $this->provider->urlGenerator($clean_path);
180
    }
181
182
    /**
183
     * Read the configuration file and pass it to the provider factory
184
     * to return an object of the default provider specified in the
185
     * config file.
186
     */
187
    private function init()
188
    {
189
        // return the configurations from the config file
190
        $this->configurations = $this->helper->getConfigurations();
191
192
        // return an instance of the corresponding Provider concrete according to the configuration
193
        $this->provider = $this->provider_factory->create($this->configurations);
194
    }
195
}
196