Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

util   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 12
c 5
b 2
f 0
lcom 2
cbo 0
dl 0
loc 96
ccs 43
cts 43
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A add_assets() 0 11 4
A set_assets() 0 13 3
A get_form_key() 0 9 1
A _prep_scripts() 0 16 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\template\template */
15
	protected $template;
16
17
	/** @var \phpbb\template\context */
18
	protected $template_context;
19
20
	/** array */
21
	protected $scripts;
22
23
	/** array */
24
	public $asset_path;
25
26
	/**
27
	 * Constructor
28
	 *
29
	 * @param \phpbb\path_helper					$path_helper		Path helper object
30
	 * @param \phpbb\template\template				$template			Template object
31
	 * @param \phpbb\template\context				$template_context	Template context object
32
	 */
33 5
	public function __construct(\phpbb\path_helper $path_helper, \phpbb\template\template $template, \phpbb\template\context $template_context)
34
	{
35 5
		$this->template = $template;
36 5
		$this->template_context = $template_context;
37 5
		$this->asset_path = $path_helper->get_web_root_path();
38 5
		$this->scripts = array(
39 5
			'js'	=> array(),
40 5
			'css'   => array(),
41
		);
42 5
	}
43
44
	/**
45
	 * include css/javascript
46
	 * receives an array of form: array('js' => array('test.js', 'test2.js'), 'css' => array())
47
	 */
48 2
	public function add_assets($scripts)
49
	{
50 2
		foreach ($scripts as $type => $paths)
51
		{
52 1
			$count = (isset($this->scripts[$type])) ? sizeof($this->scripts[$type]) : 0;
53 1
			foreach ($paths as $key => $script)
54
			{
55 1
				$this->scripts[$type][$count++] = $script;
56 1
			}
57 2
		}
58 2
	}
59
60
	/**
61
	 * Pass assets to template
62
	 */
63 2
	public function set_assets()
64
	{
65 2
		$this->_prep_scripts();
66 2
		foreach ($this->scripts as $type => $scripts)
67
		{
68 1
			foreach ($scripts as $file)
69
			{
70 1
				$this->template->assign_block_vars($type, array('UA_FILE' => trim($file)));
71 1
			}
72 2
		}
73
74 2
		$this->scripts = array();
75 2
	}
76
77
	/**
78
	 * Add a secret token to the form (requires the S_FORM_TOKEN template variable)
79
	 * @param string  $form_name The name of the form; has to match the name used in check_form_key, otherwise no restrictions apply
80
	 */
81 1
	public function get_form_key($form_name)
82
	{
83 1
		add_form_key($form_name);
84
85 1
		$rootref = $this->template_context->get_root_ref();
86 1
		$s_form_token = $rootref['S_FORM_TOKEN'];
87
88 1
		return $s_form_token;
89
	}
90
91 2
	protected function _prep_scripts()
92
	{
93 2
		if (isset($this->scripts['js']))
94 2
		{
95 2
			ksort($this->scripts['js']);
96 2
			$this->scripts['js'] = array_filter(array_unique($this->scripts['js']));
97 2
		}
98
99 2
		if (isset($this->scripts['css']))
100 2
		{
101 2
			ksort($this->scripts['css']);
102 2
			$this->scripts['css'] = array_filter(array_unique($this->scripts['css']));
103 2
		}
104
105 2
		$this->scripts = array_filter($this->scripts);
106 2
	}
107
}
108