CdnFacade::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Publiux\laravelcdn;
4
5
use Illuminate\Support\Facades\Request;
6
use Publiux\laravelcdn\Contracts\CdnFacadeInterface;
7
use Publiux\laravelcdn\Contracts\CdnHelperInterface;
8
use Publiux\laravelcdn\Contracts\ProviderFactoryInterface;
9
use Publiux\laravelcdn\Exceptions\EmptyPathException;
10
use Publiux\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 \Publiux\laravelcdn\Contracts\ProviderFactoryInterface
28
     */
29
    protected $provider_factory;
30
31
    /**
32
     * instance of the default provider object.
33
     *
34
     * @var \Publiux\laravelcdn\Providers\Contracts\ProviderInterface
35
     */
36
    protected $provider;
37
38
    /**
39
     * @var \Publiux\laravelcdn\Contracts\CdnHelperInterface
40
     */
41
    protected $helper;
42
43
    /**
44
     * @var \Publiux\laravelcdn\Validators\CdnFacadeValidator
45
     */
46
    protected $cdn_facade_validator;
47
48
    /**
49
     * Calls the provider initializer.
50
     *
51
     * @param \Publiux\laravelcdn\Contracts\ProviderFactoryInterface $provider_factory
52
     * @param \Publiux\laravelcdn\Contracts\CdnHelperInterface       $helper
53
     * @param \Publiux\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'],
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