Completed
Push — master ( 4df581...67ab38 )
by Nikola
03:34
created

Code::doAddScriptFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Disqus Helper package.
4
 *
5
 * Copyright (c) Nikola Posa <[email protected]>
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
namespace DisqusHelper;
12
13
final class Code
14
{
15
    const INDENT = '    ';
16
17
    /**
18
     * @var string
19
     */
20
    private $shortName;
21
22
    /**
23
     * @var array
24
     */
25
    private $config = [];
26
27
    /**
28
     * @var array
29
     */
30
    private $scriptFiles = [];
31
32
    /**
33
     * @var array
34
     */
35
    private $htmlFragments = [];
36
37 16
    private function __construct()
38
    {
39 16
    }
40
41 16
    public static function create(string $shortName) : Code
42
    {
43 16
        $code = new self();
44
45 16
        $code->shortName = $shortName;
46
47 16
        return $code;
48
    }
49
50 4
    public function mergeConfig(array $config) : Code
51
    {
52 4
        $this->config = array_merge($this->config, $config);
53
54 4
        return $this;
55
    }
56
57 1
    public function getConfig() : array
58
    {
59 1
        return $this->config;
60
    }
61
62 6
    public function addScriptFile(string $fileName) : Code
63
    {
64 6
        $this->doAddScriptFile($fileName, false);
65
66 6
        return $this;
67
    }
68
69 6
    public function addLazyLoadedScriptFile(string $fileName) : Code
70
    {
71 6
        $this->doAddScriptFile($fileName, true);
72
73 6
        return $this;
74
    }
75
76 11
    private function doAddScriptFile(string $fileName, bool $lazyLoad)
77
    {
78 11
        if (!isset($this->scriptFiles[$fileName])) {
79 11
            $this->scriptFiles[$fileName] = [
80 11
                'fileName' => $fileName,
81 11
                'lazyLoad' => $lazyLoad,
82
            ];
83
        }
84 11
    }
85
86 4
    public function hasScriptFile(string $fileName) : bool
87
    {
88 4
        return isset($this->scriptFiles[$fileName]);
89
    }
90
91 1
    public function __toString() : string
92
    {
93 1
        return $this->toHtml();
94
    }
95
96 7
    public function toHtml() : string
97
    {
98 7
        $this->htmlFragments = [];
99
100 7
        $this->buildConfigVariablesHtml();
101 7
        $this->buildJsFilesHtml();
102
103 7
        return implode(PHP_EOL . PHP_EOL, $this->htmlFragments);
104
    }
105
106 7
    private function buildConfigVariablesHtml()
107
    {
108 7
        if (empty($this->config)) {
109 4
            return;
110
        }
111
112 3
        $script = '<script>' . PHP_EOL;
113
114 3
        $script .= self::INDENT . 'var disqus_config = function () {' . PHP_EOL;
115
116 3
        foreach ($this->config as $key => $value) {
117 3
            if (is_string($value)) {
118 3
                $value = addslashes($value);
119 3
                $value = "'$value'";
120
            }
121 3
            $script .= self::INDENT . self::INDENT . "this.$key = $value;" . PHP_EOL;
122
        }
123
124 3
        $script .= self::INDENT . '};' . PHP_EOL ;
125
126 3
        $script .= '</script>';
127
128 3
        $this->htmlFragments[] = $script;
129 3
    }
130
131 7
    private function buildJsFilesHtml()
132
    {
133 7
        foreach ($this->scriptFiles as $fileInfo) {
134 6
            $fileName = $fileInfo['fileName'];
135
136 6
            if ($fileInfo['lazyLoad']) {
137 2
                $this->htmlFragments[] = $this->renderLazyLoadedJsFile($fileName);
138 2
                continue;
139
            }
140
141 4
            $this->htmlFragments[] = $this->renderJsFile($fileName);
142
        }
143 7
    }
144
145 4
    private function renderJsFile(string $fileName) : string
146
    {
147 4
        return sprintf('<script src="%s" async></script>', $this->getJsFileUrl($fileName));
148
    }
149
150 2
    private function renderLazyLoadedJsFile(string $fileName) : string
151
    {
152 2
        $script = '<script>' . PHP_EOL;
153 2
        $script .= self::INDENT . '(function() {' . PHP_EOL;
154 2
        $script .= self::INDENT . self::INDENT . 'var d = document, s = d.createElement("script");' . PHP_EOL;
155 2
        $script .= self::INDENT . self::INDENT . 's.src = "' . $this->getJsFileUrl($fileName) . '";' . PHP_EOL;
156 2
        $script .= self::INDENT . self::INDENT . 's.setAttribute("data-timestamp", +new Date());' . PHP_EOL;
157 2
        $script .= self::INDENT . self::INDENT . '(d.head || d.body).appendChild(s);' . PHP_EOL;
158 2
        $script .= self::INDENT . '})();' . PHP_EOL;
159 2
        $script .= '</script>';
160
161 2
        return $script;
162
    }
163
164 6
    private function getJsFileUrl(string $fileName)
165
    {
166 6
        return '//' . $this->shortName . '.disqus.com/' . $fileName;
167
    }
168
}