Test Failed
Push — master ( b97539...3507af )
by Jean-Christophe
09:15
created

AssetsManager::gString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Ubiquity\assets;
4
5
use Ubiquity\themes\ThemesManager;
6
use Ubiquity\utils\base\UArray;
7
8
/**
9
 * Assets manager for css and js inclusions in templates.
10
 * Ubiquity\assets$AssetsManager
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.1
15
 * @since Ubiquity 2.1.0
16
 *
17
 */
18
class AssetsManager {
19
	const ASSETS_FOLDER = '/public/assets/';
20
	private static $siteURL;
21
22
	private static function gString($template, $variable, $attributes = []) {
23
		$implode = UArray::implodeAsso ( $attributes, ' ' );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $implode is correct as Ubiquity\utils\base\UArr...eAsso($attributes, ' ') targeting Ubiquity\utils\base\UArray::implodeAsso() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
24
		return sprintf ( $template, $variable, $implode );
25
	}
26
27
	private static function script($src, $attributes = []) {
28
		return self::gString ( '<script type="text/javascript" src="%s" %s></script>', $src, $attributes );
29
	}
30
31
	private static function stylesheet($link, $attributes = []) {
32
		return self::gString ( '<link href="%s" type="text/css" rel="stylesheet" %s>', $link, $attributes );
33
	}
34
35
	/**
36
	 * Starts the assets manager.
37
	 * Essential to define the siteURL part
38
	 *
39
	 * @param array $config
40
	 */
41
	public static function start(&$config) {
42
		$siteURL = $config ['siteUrl'] ?? '';
43
		self::$siteURL = rtrim ( $siteURL, '/' );
44
	}
45
46
	/**
47
	 * Returns the absolute or relative url to the resource.
48
	 *
49
	 * @param string $resource
50
	 * @param boolean $absolute
51
	 * @return string
52
	 */
53
	public static function getUrl($resource, $absolute = false) {
54
		if (strpos ( $resource, '//' ) !== false) {
55
			return $resource;
56
		}
57
		if ($absolute) {
58
			return self::$siteURL . self::ASSETS_FOLDER . $resource;
59
		}
60
		return ltrim ( self::ASSETS_FOLDER, '/' ) . $resource;
61
	}
62
63
	/**
64
	 * Returns the absolute or relative url for a resource in the **activeTheme**.
65
	 *
66
	 * @param string $resource
67
	 * @param boolean $absolute
68
	 * @return string
69
	 */
70
	public static function getActiveThemeUrl($resource, $absolute = false) {
71
		$activeTheme = ThemesManager::getActiveTheme ();
72
		return self::getThemeUrl ( $activeTheme, $resource, $absolute );
73
	}
74
75
	/**
76
	 * Returns the absolute or relative url for a resource in a theme.
77
	 *
78
	 * @param string $theme
79
	 * @param string $resource
80
	 * @param boolean $absolute
81
	 * @return string
82
	 */
83
	public static function getThemeUrl($theme, $resource, $absolute = false) {
84
		if ($absolute) {
85
			return self::$siteURL . self::ASSETS_FOLDER . $theme . '/' . $resource;
86
		}
87
		return ltrim ( self::ASSETS_FOLDER, '/' ) . $theme . '/' . $resource;
88
	}
89
90
	/**
91
	 * Returns the script inclusion for a javascript resource.
92
	 *
93
	 * @param string $resource The javascript resource to include
94
	 * @param array $attributes The other html attributes of the script element
95
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
96
	 * @return string
97
	 */
98
	public static function js($resource, $attributes = [], $absolute = false) {
99
		return self::script ( self::getUrl ( $resource, $absolute ), $attributes );
100
	}
101
102
	/**
103
	 * Returns the css inclusion for a stylesheet resource.
104
	 *
105
	 * @param string $resource The css resource to include
106
	 * @param array $attributes The other html attributes of the script element
107
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
108
	 * @return string
109
	 */
110
	public static function css($resource, $attributes = [], $absolute = false) {
111
		return self::stylesheet ( self::getUrl ( $resource, $absolute ), $attributes );
112
	}
113
114
	/**
115
	 * Returns the script inclusion for a javascript resource in **activeTheme**.
116
	 *
117
	 * @param string $resource The javascript resource to include
118
	 * @param array $attributes The other html attributes of the script element
119
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
120
	 * @return string
121
	 */
122
	public static function js_($resource, $attributes = [], $absolute = false) {
123
		return self::script ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes );
124
	}
125
126
	/**
127
	 * Returns the css inclusion for a stylesheet resource in **activeTheme**.
128
	 *
129
	 * @param string $resource The css resource to include
130
	 * @param array $attributes The other html attributes of the script element
131
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
132
	 * @return string
133
	 */
134
	public static function css_($resource, $attributes = [], $absolute = false) {
135
		return self::stylesheet ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes );
136
	}
137
}
138