Completed
Push — master ( 9fc4e6...f0af25 )
by Daniel
02:54
created

setJavascriptFileCDNforHighCharts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

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