TemplateResources   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 27
c 1
b 0
f 0
dl 0
loc 126
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 5 1
A getCssFiles() 0 3 1
A getJsFiles() 0 3 1
A addCssFile() 0 5 2
A __construct() 0 7 3
A addCssFiles() 0 4 2
A addJsFile() 0 5 2
A addJsFiles() 0 4 2
A compileResources() 0 15 3
1
<?php
2
namespace Mezon\HtmlTemplate;
3
4
use Mezon\Conf\Conf;
5
6
/**
7
 * Class TemplateResources
8
 *
9
 * @package Mezon
10
 * @subpackage TemplateResources
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/08/17)
13
 * @copyright Copyright (c) 2019, aeon.org
14
 */
15
16
/**
17
 * Class collects resources for page.
18
 *
19
 * Any including components can add to the page their own resources without having access to the application or template.
20
 */
21
class TemplateResources
22
{
23
24
    /**
25
     * Custom CSS files to be included
26
     */
27
    private $cssFiles = false;
28
29
    /**
30
     * Custom JS files to be included
31
     */
32
    private $jsFiles = false;
33
34
    /**
35
     * Constructor.
36
     */
37
    function __construct()
38
    {
39
        if ($this->cssFiles === false) {
40
            $this->cssFiles = [];
41
        }
42
        if ($this->jsFiles === false) {
43
            $this->jsFiles = [];
44
        }
45
    }
46
47
    /**
48
     * Additing single CSS file
49
     *
50
     * @param string $cssFile
51
     *            CSS file
52
     */
53
    function addCssFile(string $cssFile)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
    {
55
        // additing only unique paths
56
        if (array_search($cssFile, $this->cssFiles) === false) {
0 ignored issues
show
Bug introduced by
$this->cssFiles of type boolean is incompatible with the type array expected by parameter $haystack of array_search(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        if (array_search($cssFile, /** @scrutinizer ignore-type */ $this->cssFiles) === false) {
Loading history...
57
            $this->cssFiles[] = Conf::expandString($cssFile);
58
        }
59
    }
60
61
    /**
62
     * Additing multyple CSS files
63
     *
64
     * @param array $cssFiles
65
     *            CSS files
66
     */
67
    function addCssFiles(array $cssFiles)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
    {
69
        foreach ($cssFiles as $cssFile) {
70
            $this->addCssFile($cssFile);
71
        }
72
    }
73
74
    /**
75
     * Method returning added CSS files
76
     */
77
    function getCssFiles()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
    {
79
        return $this->cssFiles;
80
    }
81
82
    /**
83
     * Additing single CSS file
84
     *
85
     * @param string $jsFile
86
     *            JS file
87
     */
88
    function addJsFile($jsFile)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
    {
90
        // additing only unique paths
91
        if (array_search($jsFile, $this->jsFiles) === false) {
0 ignored issues
show
Bug introduced by
$this->jsFiles of type boolean is incompatible with the type array expected by parameter $haystack of array_search(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        if (array_search($jsFile, /** @scrutinizer ignore-type */ $this->jsFiles) === false) {
Loading history...
92
            $this->jsFiles[] = Conf::expandString($jsFile);
93
        }
94
    }
95
96
    /**
97
     * Additing multyple CSS files
98
     *
99
     * @param array $jsFiles
100
     *            JS files
101
     */
102
    function addJsFiles(array $jsFiles)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
    {
104
        foreach ($jsFiles as $jsFile) {
105
            $this->addJsFile($jsFile);
106
        }
107
    }
108
109
    /**
110
     * Method returning added JS files.
111
     */
112
    function getJsFiles()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
113
    {
114
        return $this->jsFiles;
115
    }
116
117
    /**
118
     * Method clears loaded resources.
119
     */
120
    function clear()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
    {
122
        $this->cssFiles = [];
123
124
        $this->jsFiles = [];
125
    }
126
127
    /**
128
     * Method returns compiled page resources
129
     *
130
     * @return string Compiled resources includers
131
     */
132
    public function compileResources(): string
133
    {
134
        $content = '';
135
136
        foreach ($this->cssFiles as $cssFile) {
0 ignored issues
show
Bug introduced by
The expression $this->cssFiles of type boolean is not traversable.
Loading history...
137
            $content .= '
138
        <link href="' . $cssFile . '" rel="stylesheet" type="text/css">';
139
        }
140
141
        foreach ($this->jsFiles as $jsFile) {
0 ignored issues
show
Bug introduced by
The expression $this->jsFiles of type boolean is not traversable.
Loading history...
142
            $content .= '
143
        <script src="' . $jsFile . '"></script>';
144
        }
145
146
        return $content;
147
    }
148
}
149