Completed
Push — master ( ccfd66...b806bf )
by Nikola
04:27
created

Code::addLazyLoadedScriptFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 17
    private function __construct()
38
    {
39 17
    }
40
41 17
    public static function create(string $shortName) : Code
42
    {
43 17
        $code = new self();
44
45 17
        $code->shortName = $shortName;
46
47 17
        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 12
    public function addScriptFile(string $name, array $options = []) : Code
63
    {
64 12
        if (!isset($this->scriptFiles[$name])) {
65 12
            $data = array_merge([
66 12
                'name' => $name,
67
                'id' => null,
68
                'lazy_load' => false,
69
            ], $options);
70
71 12
            $this->scriptFiles[$name] = $data;
72
        }
73
74 12
        return $this;
75
    }
76
77 3
    public function hasScriptFile(string $fileName) : bool
78
    {
79 3
        return isset($this->scriptFiles[$fileName]);
80
    }
81
82 1
    public function __toString() : string
83
    {
84 1
        return $this->toHtml();
85
    }
86
87 9
    public function toHtml() : string
88
    {
89 9
        $this->htmlFragments = [];
90
91 9
        $this->buildConfigVariablesHtml();
92 9
        $this->buildJsFilesHtml();
93
94 9
        return PHP_EOL . implode(PHP_EOL . PHP_EOL, $this->htmlFragments) . PHP_EOL;
95
    }
96
97 9
    private function buildConfigVariablesHtml()
98
    {
99 9
        if (empty($this->config)) {
100 6
            return;
101
        }
102
103 3
        $script = '<script>' . PHP_EOL;
104 3
        $script .= self::INDENT . 'var disqus_config = function () {' . PHP_EOL;
105
106 3
        foreach ($this->config as $key => $value) {
107 3
            if (is_string($value)) {
108 3
                $value = addslashes($value);
109 3
                $value = "'$value'";
110
            }
111 3
            $script .= self::INDENT . self::INDENT . "this.$key = $value;" . PHP_EOL;
112
        }
113
114 3
        $script .= self::INDENT . '};' . PHP_EOL ;
115 3
        $script .= '</script>';
116
117 3
        $this->htmlFragments[] = $script;
118 3
    }
119
120 9
    private function buildJsFilesHtml()
121
    {
122 9
        foreach ($this->scriptFiles as $fileData) {
123 8
            if ($fileData['lazy_load']) {
124 3
                $this->htmlFragments[] = $this->renderLazyLoadedJsFile($fileData);
125 3
                continue;
126
            }
127
128 5
            $this->htmlFragments[] = $this->renderJsFile($fileData);
129
        }
130 9
    }
131
132 5
    private function renderJsFile(array $fileData) : string
133
    {
134 5
        return sprintf(
135 5
            '<script id="%2$s" src="%1$s" async></script>',
136 5
            $this->getJsFileUrl($fileData['name']),
137 5
            $fileData['id']
138
        );
139
    }
140
141 3
    private function renderLazyLoadedJsFile(array $fileData) : string
142
    {
143 3
        $script = '<script>' . PHP_EOL;
144 3
        $script .= self::INDENT . '(function() {' . PHP_EOL;
145 3
        $script .= self::INDENT . self::INDENT . 'var d = document, s = d.createElement("script");' . PHP_EOL;
146 3
        $script .= self::INDENT . self::INDENT . 's.src = "' . $this->getJsFileUrl($fileData['name']) . '";' . PHP_EOL;
147 3
        $script .= self::INDENT . self::INDENT . 's.setAttribute("data-timestamp", +new Date());' . PHP_EOL;
148 3
        $script .= self::INDENT . self::INDENT . '(d.head || d.body).appendChild(s);' . PHP_EOL;
149 3
        $script .= self::INDENT . '})();' . PHP_EOL;
150 3
        $script .= '</script>';
151
152 3
        return $script;
153
    }
154
155 8
    private function getJsFileUrl(string $fileName)
156
    {
157 8
        return '//' . $this->shortName . '.disqus.com/' . $fileName;
158
    }
159
}