Completed
Pull Request — master (#3)
by Nikola
32:48 queued 50s
created

Code::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 array
19
     */
20
    private $config = [];
21
22
    /**
23
     * @var array
24
     */
25
    private $scriptFiles = [];
26
27
    /**
28
     * @var string
29
     */
30
    private $html;
31
32
    private function __construct()
33
    {
34
    }
35
36
    public static function create() : Code
37
    {
38
        return new self();
39
    }
40
41
    public function setConfigVariable(string $key, $value) : Code
42
    {
43
        $this->config[$key] = $value;
44
45
        return $this;
46
    }
47
48
    public function getConfigVariable(string $key)
49
    {
50
        if (!isset($this->config[$key])) {
51
            return null;
52
        }
53
54
        return $this->config[$key];
55
    }
56
57
    public function mergeConfig(array $config) : Code
58
    {
59
        $this->config = array_merge($this->config, $config);
60
61
        return $this;
62
    }
63
64
    public function addScriptFile(string $fileName) : Code
65
    {
66
        if (!isset($this->scriptFiles[$fileName])) {
67
            $this->scriptFiles[$fileName] = $fileName;
68
        }
69
70
        return $this;
71
    }
72
73
    public function hasScriptFile(string $fileName) : bool
74
    {
75
        return isset($this->scriptFiles[$fileName]);
76
    }
77
78
    public function __toString() : string
79
    {
80
        return $this->toHtml();
81
    }
82
83
    public function toHtml() : string
84
    {
85
        $this->html = '';
86
87
        $this->html = '<script type="text/javascript">';
88
        $this->html .= PHP_EOL;
89
90
        $this->renderConfigVariables();
91
92
        $this->html .= PHP_EOL . PHP_EOL;
93
94
        $this->renderJsFiles();
95
96
        $this->html .= PHP_EOL . PHP_EOL;
97
        $this->html .= '</script>';
98
99
        return $this->html;
100
    }
101
102
    private function renderConfigVariables()
103
    {
104
        $configVars = [];
105
106
        foreach ($this->config as $key => $value) {
107
            if (is_string($value)) {
108
                $value = addslashes((string) $value);
109
                $value = "'$value'";
110
            }
111
            $configVars[] = self::INDENT . "var disqus_$key = $value;";
112
        }
113
114
        $this->html .= implode(PHP_EOL, $configVars);
115
    }
116
117
    private function renderJsFiles()
118
    {
119
        foreach ($this->scriptFiles as $fileName) {
120
            $this->html .= self::INDENT . '(function() {' . PHP_EOL;
121
            $this->html .= self::INDENT . self::INDENT . 'var s = document.createElement("script");' . PHP_EOL;
122
            $this->html .= self::INDENT . self::INDENT . 's.type = "text/javascript";' . PHP_EOL;
123
            $this->html .= self::INDENT . self::INDENT . 's.async = true;' . PHP_EOL;
124
            $this->html .= self::INDENT . self::INDENT . 's.src = "//" + disqus_shortname + ".disqus.com/' . $fileName . '";' . PHP_EOL;
125
            $this->html .= self::INDENT . self::INDENT . '(document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0]).appendChild(s);' . PHP_EOL;
126
            $this->html .= self::INDENT . '})();' . PHP_EOL . PHP_EOL;
127
        }
128
129
        $this->html = rtrim($this->html, PHP_EOL);
130
    }
131
}