CdnFacade::elixir()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
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
	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], '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::path('')}} to convert the path into
110
     * it's CDN url.
111
     *
112
     * @param $path
113
     *
114
     * @return mixed
115
     *
116
     * @throws Exceptions\EmptyPathException
117
     */
118
    public function path($path)
119
    {
120
        return $this->generateUrl($path);
121
    }
122
123
    /**
124
     * check if package is surpassed or not then
125
     * prepare the path before generating the url.
126
     *
127
     * @param        $path
128
     * @param string $prepend
129
     *
130
     * @return mixed
131
     */
132
    private function generateUrl($path, $prepend = '')
133
    {
134
        // if the package is surpassed, then return the same $path
135
        // to load the asset from the localhost
136
        if (isset($this->configurations['bypass']) && $this->configurations['bypass']) {
137
            return Request::root().'/'.$path;
138
        }
139
140
        if (!isset($path)) {
141
            throw new EmptyPathException('Path does not exist.');
142
        }
143
144
        // Add version number
145
        //$path = str_replace(
146
        //    "build",
147
        //    $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...
148
        //    $path
149
        //);
150
151
        // remove slashes from begging and ending of the path
152
        // and append directories if needed
153
        $clean_path = $prepend.$this->helper->cleanPath($path);
154
155
        // call the provider specific url generator
156
        return $this->provider->urlGenerator($clean_path);
157
    }
158
159
    /**
160
     * Read the configuration file and pass it to the provider factory
161
     * to return an object of the default provider specified in the
162
     * config file.
163
     */
164
    private function init()
165
    {
166
        // return the configurations from the config file
167
        $this->configurations = $this->helper->getConfigurations();
168
169
        // return an instance of the corresponding Provider concrete according to the configuration
170
        $this->provider = $this->provider_factory->create($this->configurations);
171
    }
172
}
173