Passed
Branch develop (786e4f)
by Daniel
07:32
created

util   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 97.83%

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 45
cts 46
cp 0.9783
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get_default_avatar() 0 3 1
A reset() 0 5 1
A set_assets() 0 11 1
B add_assets() 0 9 5
A __construct() 0 7 1
A get_theme_path() 0 4 1
A get_web_path() 0 7 3
A get_form_key() 0 5 1
A sort_assets() 0 8 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class util
13
{
14
	/** @var \phpbb\path_helper */
0 ignored issues
show
Bug introduced by
The type phpbb\path_helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
	protected $path_helper;
16
17
	/** @var \phpbb\template\template */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
	protected $template;
19
20
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
	protected $user;
22
23
	/** string */
24
	protected $web_path;
25
26
	/** array */
27
	protected $assets;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\path_helper					$path_helper		Path helper object
33
	 * @param \phpbb\template\template				$template			Template object
34
	 * @param \phpbb\user							$user				User object
35
	 */
36 101
	public function __construct(\phpbb\path_helper $path_helper, \phpbb\template\template $template, \phpbb\user $user)
37
	{
38 101
		$this->path_helper = $path_helper;
39 101
		$this->template = $template;
40 101
		$this->user = $user;
41
42 101
		$this->reset();
43 101
	}
44
45
	/**
46
	 * include css/javascript
47
	 * @param array $assets array of form array('js' => array('test.js', 'test2.js'), 'css' => array())
48
	 * @return void
49
	 */
50 2
	public function add_assets(array $assets)
51
	{
52 2
		foreach ($assets as $type => $scripts)
53
		{
54 1
			foreach ($scripts as $order => $script)
55
			{
56 1
				if (!isset($this->assets[$type][$script]) || $this->assets[$type][$script] < $order)
57 1
				{
58 1
					$this->assets[$type][$script] = $order;
59 1
				}
60 1
			}
61 2
		}
62 2
	}
63
64
	/**
65
	 * Pass assets to template
66
	 */
67 2
	public function set_assets()
68
	{
69 2
		uasort($this->assets['js'], array($this, 'sort_assets'));
70 2
		uasort($this->assets['css'], array($this, 'sort_assets'));
71
72 2
		$this->template->assign_var('assets', array(
73 2
			'css'	=> array_keys($this->assets['css']),
74 2
			'js'	=> array_keys($this->assets['js']),
75 2
		));
76
77 2
		$this->reset();
78 2
	}
79
80
	/**
81
	 * Add a secret token to the form (requires the S_FORM_TOKEN template variable)
82
	 * @param string  $form_name The name of the form; has to match the name used in check_form_key, otherwise no restrictions apply
83
	 */
84 1
	public function get_form_key($form_name)
85
	{
86 1
		add_form_key($form_name);
0 ignored issues
show
Bug introduced by
The function add_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
		/** @scrutinizer ignore-call */ 
87
  add_form_key($form_name);
Loading history...
87
88 1
		return $this->template->retrieve_var('S_FORM_TOKEN');
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94 15
	public function get_default_avatar()
95
	{
96 15
		return '<img src="' . $this->get_theme_path() . '/images/no_avatar.gif" alt="" />';
97
	}
98
99
	/**
100
	 * Returns a corrected theme path depending on whether or not we are accessing a controller
101
	 * @return string
102
	 */
103 15
	public function get_theme_path()
104
	{
105 15
		$web_path = $this->get_web_path();
106 15
		return "{$web_path}styles/" . rawurlencode($this->user->style['style_path']) . '/theme';
107
	}
108
109
	/**
110
	 * Returns a corrected root path depending on whether or not we are accessing a controller
111
	 * @return string
112
	 */
113 15
	public function get_web_path()
114
	{
115
		// Determine board url - we may need it later
116 15
		$board_url = generate_board_url() . '/';
0 ignored issues
show
Bug introduced by
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

116
		$board_url = /** @scrutinizer ignore-call */ generate_board_url() . '/';
Loading history...
117
118 15
		$corrected_path = $this->path_helper->get_web_root_path();
119 15
		return (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $corrected_path;
1 ignored issue
show
Bug introduced by
The constant blitze\sitemaker\services\PHPBB_USE_BOARD_URL_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
120
	}
121
122
	/**
123
	 * @param int $a
124
	 * @param int $b
125
	 * @return int
126
	 */
127 1
	protected function sort_assets($a, $b)
128
	{
129 1
		if ($a == $b)
130 1
		{
131
			return 0;
132
		}
133
134 1
		return ($a < $b) ? -1 : 1;
135
	}
136
137
	/**
138
	 * @return void
139
	 */
140 101
	protected function reset()
141
	{
142 101
		$this->assets = array(
143 101
			'js'	=> array(),
144 101
			'css'   => array(),
145
		);
146 101
	}
147
}
148