Completed
Push — master ( f82444...6e866c )
by Daniel
02:55
created

setJavascriptFileCDN()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 21
Code Lines 16

Duplication

Lines 5
Ratio 23.81 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 5
loc 21
rs 7.551
cc 7
eloc 16
nc 12
nop 1
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * DOM component functions
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait DomComponentsByDanielGPwithCDN
37
{
38
39
    private function knownCloudFlareJavascript($jsFileName)
40
    {
41
        $justFile = pathinfo($jsFileName)['basename'];
42
        switch ($justFile) {
43
            case 'jquery.placeholder.min.js':
44
                return [
45
                    'justFile' => $justFile,
46
                    'version'  => 'jquery-placeholder/2.0.8/',
47
                    'eVerify'  => 'jQuery.placeholder',
48
                ];
49
            // intentionally left blank
50
            case 'jquery.easing.1.3.min.js':
51
                return [
52
                    'justFile' => str_replace('.1.3', '', $justFile),
53
                    'version'  => 'jquery-easing/1.3/',
54
                    'eVerify'  => 'jQuery.easing["jswing"]',
55
                ];
56
            // intentionally left blank
57
        }
58
    }
59
60
    /**
61
     * Manages all known CSS that can be handled through CDNs
62
     *
63
     * @param string $cssFileName
64
     * @return array
65
     */
66
    protected function setCssFileCDN($cssFileName)
67
    {
68
        $onlyFileName = pathinfo($cssFileName)['basename'];
69
        if (strpos($onlyFileName, 'font-awesome-') !== false) {
70
            $patternFound = $this->setCssFileCDNforFontAwesome($cssFileName);
71
        } else {
72
            $patternFound = null;
73
        }
74 View Code Duplication
        if (is_null($patternFound)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            $patternFound = [
76
                false,
77
                filter_var($cssFileName, FILTER_SANITIZE_STRING),
78
            ];
79
        }
80
        return $patternFound;
81
    }
82
83
    /**
84
     * Returns css link to a given file
85
     * Returns an array with CDN call of a known Font-websome css
86
     *
87
     * @param string $cssFile
0 ignored issues
show
Documentation introduced by
There is no parameter named $cssFile. Did you maybe mean $cssFileName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
88
     * @return string
89
     */
90
    private function setCssFileCDNforFontAwesome($cssFileName)
91
    {
92
        $patternFound = [
93
            true,
94
            implode('', [
95
                '//cdnjs.cloudflare.com/ajax/libs/font-awesome/',
96
                str_replace(['font-awesome-', '.min.css'], '', pathinfo($cssFileName)['basename']),
97
                '/css/font-awesome.min.css',
98
            ])
99
        ];
100
        return $patternFound;
101
    }
102
103
    /**
104
     * Manages all known Javascript that can be handled through CDNs
105
     *
106
     * @param string $jsFileName
107
     * @return array
108
     */
109
    protected function setJavascriptFileCDN($jsFileName)
110
    {
111
        $onlyFileName = pathinfo($jsFileName)['basename'];
112
        // if within local network makes no sense to use CDNs
113
        $patternFound = null;
114
        if (strpos($onlyFileName, 'jquery-') !== false) {
115
            $patternFound = $this->setJavascriptFileCDNjQuery($jsFileName);
116
        } elseif (strpos($onlyFileName, 'jquery.placeholder.min.js') !== false) {
117
            $patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName);
118
        } elseif (strpos($onlyFileName, 'jquery.easing.1.3.min.js') !== false) {
119
            $patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName);
120
        } elseif (strpos($onlyFileName, 'highcharts-') !== false) {
121
            $patternFound = $this->setJavascriptFileCDNforHighCharts($jsFileName);
122
        } elseif (strpos($onlyFileName, 'exporting-') !== false) {
123
            $patternFound = $this->setJavascriptFileCDNforHighChartsExporting($jsFileName);
124
        }
125 View Code Duplication
        if (is_null($patternFound)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126
            $patternFound = [false, filter_var($jsFileName, FILTER_SANITIZE_STRING), ''];
127
        }
128
        return $patternFound;
129
    }
130
131
    /**
132
     * Returns an array with CDN call of a known Javascript library
133
     * and fall-back line that points to local cache of it
134
     * specific for HighCharts
135
     *
136
     * @param string $jsFileName
137
     * @return array
138
     */
139 View Code Duplication
    private function setJavascriptFileCDNforHighCharts($jsFileName)
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...
140
    {
141
        $patternFound   = null;
142
        $jQueryPosition = strpos($jsFileName, 'highcharts');
143
        if ($jQueryPosition !== false) {
144
            $patternFound = [
145
                true,
146
                implode('', [
147
                    '//cdnjs.cloudflare.com/ajax/libs/highcharts/',
148
                    str_replace(['highcharts-', '.js'], '', pathinfo($jsFileName)['basename']),
149
                    '/highcharts.js',
150
                ]),
151
                implode('', [
152
                    '<script>!window.Highcharts && document.write(\'<script src="',
153
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
154
                    '">\x3C/script>\')</script>'
155
                ])
156
            ];
157
        }
158
        return $patternFound;
159
    }
160
161
    /**
162
     * Returns an array with CDN call of a known Javascript library
163
     * and fall-back line that points to local cache of it
164
     * specific for HighCharts Exporting feature
165
     *
166
     * @param string $jsFileName
167
     * @return array
168
     */
169 View Code Duplication
    private function setJavascriptFileCDNforHighChartsExporting($jsFileName)
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...
170
    {
171
        $patternFound   = null;
172
        $jQueryPosition = strpos($jsFileName, 'exporting');
173
        if ($jQueryPosition !== false) {
174
            $patternFound = [
175
                true,
176
                implode('', [
177
                    '//cdnjs.cloudflare.com/ajax/libs/highcharts/',
178
                    str_replace(['exporting-', '.js'], '', pathinfo($jsFileName)['basename']),
179
                    '/modules/exporting.js',
180
                ]),
181
                implode('', [
182
                    '<script>!window.Highcharts.post && document.write(\'<script src="',
183
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
184
                    '">\x3C/script>\')</script>'
185
                ])
186
            ];
187
        }
188
        return $patternFound;
189
    }
190
191
    /**
192
     * Returns an array with CDN call of a known Javascript library
193
     * and fall-back line that points to local cache of it
194
     * specific for jQuery
195
     *
196
     * @param string $jsFileName
197
     * @return array
198
     */
199
    private function setJavascriptFileCDNjQuery($jsFileName)
200
    {
201
        $patternFound       = null;
202
        $jQueryPosition     = strpos($jsFileName, 'jquery-');
203
        $jQueryMajorVersion = substr($jsFileName, 7, 1);
204
        if (($jQueryPosition !== false) && is_numeric($jQueryMajorVersion) && (substr($jsFileName, -7) == '.min.js')) {
205
            $patternFound = [
206
                true,
207
                implode('', [
208
                    '//cdnjs.cloudflare.com/ajax/libs/jquery/',
209
                    str_replace(['jquery-', '.min.js'], '', pathinfo($jsFileName)['basename']),
210
                    '/jquery.min.js',
211
                ]),
212
                implode('', [
213
                    '<script>window.jQuery || document.write(\'<script src="',
214
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
215
                    '">\x3C/script>\')</script>'
216
                ])
217
            ];
218
        }
219
        return $patternFound;
220
    }
221
222
    /**
223
     * Returns an array with CDN call of a known Javascript library
224
     * and fall-back line that points to local cache of it
225
     * specific for jQuery Libraries
226
     *
227
     * @param string $jsFileName
228
     * @return array
229
     */
230
    private function setJavascriptFileCDNjQueryLibs($jsFileName)
231
    {
232
        $patternFound = null;
233
        $eArray       = $this->knownCloudFlareJavascript(filter_var($jsFileName, FILTER_SANITIZE_STRING));
234
        if (!is_null($eArray['version'])) {
235
            $patternFound = [
236
                true,
237
                implode('', [
238
                    '//cdnjs.cloudflare.com/ajax/libs/',
239
                    $eArray['version'],
240
                    $eArray['justFile'],
241
                ]),
242
                implode('', [
243
                    '<script>' . $eArray['eVerify'] . ' || document.write(\'<script src="',
244
                    filter_var($jsFileName, FILTER_SANITIZE_STRING),
245
                    '">\x3C/script>\')</script>'
246
                ])
247
            ];
248
        }
249
        return $patternFound;
250
    }
251
}
252