Completed
Push — master ( e4d2eb...4df581 )
by Nikola
02:47
created

Code::buildJsFilesHtml()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3
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 string
34
     */
35
    private $htmlFragments;
36
37 15
    private function __construct()
38
    {
39 15
    }
40
41 15
    public static function create(string $shortName) : Code
42
    {
43 15
        $code = new self();
44
45 15
        $code->shortName = $shortName;
46
47 15
        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 View Code Duplication
    public function addScriptFile(string $fileName) : Code
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...
63
    {
64 6
        if (!isset($this->scriptFiles[$fileName])) {
65 6
            $this->scriptFiles[$fileName] = [
66 6
                'fileName' => $fileName,
67
                'lazyLoad' => false,
68
            ];
69
        }
70
71 6
        return $this;
72
    }
73
74 4 View Code Duplication
    public function addLazyLoadedScriptFile(string $fileName) : Code
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...
75
    {
76 4
        if (!isset($this->scriptFiles[$fileName])) {
77 4
            $this->scriptFiles[$fileName] = [
78 4
                'fileName' => $fileName,
79
                'lazyLoad' => true,
80
            ];
81
        }
82
83 4
        return $this;
84
    }
85
86 3
    public function hasScriptFile(string $fileName) : bool
87
    {
88 3
        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 = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type string of property $htmlFragments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
}