Completed
Pull Request — master (#114)
by Jakub
13:20
created

helper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
/**
14
 * Helper
15
 */
16
class helper
17
{
18
	/** @var \phpbb\user */
19
	protected $user;
20
21
	/** @var \phpbb\user_loader */
22
	protected $user_loader;
23
24
	/** @var \phpbb\language\language */
25
	protected $language;
26
27
	/** @var \phpbb\template\template */
28
	protected $template;
29
30
	/** @var \phpbb\log\log */
31
	protected $log;
32
33
	/** @var \phpbb\ads\location\manager */
34
	protected $location_manager;
35
36
	/** @var string root_path */
37
	protected $root_path;
38
39
	/** @var string php_ext */
40
	protected $php_ext;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param \phpbb\user                 $user             User object
46
	 * @param \phpbb\user_loader          $user_loader      User loader object
47
	 * @param \phpbb\language\language    $language         Language object
48
	 * @param \phpbb\template\template    $template         Template object
49
	 * @param \phpbb\log\log              $log              The phpBB log system
50
	 * @param \phpbb\ads\location\manager $location_manager Template location manager object
51
	 * @param string                      $root_path        phpBB root path
52
	 * @param string                      $php_ext          PHP extension
53
	 */
54 16
	public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\location\manager $location_manager, $root_path, $php_ext)
55
	{
56 16
		$this->user = $user;
57 16
		$this->user_loader = $user_loader;
58 16
		$this->language = $language;
59 16
		$this->template = $template;
60 16
		$this->log = $log;
61 16
		$this->location_manager = $location_manager;
62 16
		$this->root_path = $root_path;
63 16
		$this->php_ext = $php_ext;
64 16
	}
65
66
	/**
67
	 * Assign ad data for ACP form template.
68
	 *
69
	 * @param	array	$data	Ad data
70
	 * @param	array	$errors	Validation errors
71
	 */
72 5
	public function assign_data($data, $errors)
73
	{
74 5
		$this->assign_locations($data['ad_locations']);
75
76 5
		$errors = array_map(array($this->language, 'lang'), $errors);
77 5
		$this->template->assign_vars(array(
78 5
			'S_ERROR'   => (bool) count($errors),
79 5
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
80
81 5
			'AD_NAME'         	=> $data['ad_name'],
82 5
			'AD_NOTE'         	=> $data['ad_note'],
83 5
			'AD_CODE'         	=> $data['ad_code'],
84 5
			'AD_ENABLED'      	=> $data['ad_enabled'],
85 5
			'AD_END_DATE'     	=> $data['ad_end_date'],
86 5
			'AD_PRIORITY'     	=> $data['ad_priority'],
87 5
			'AD_CONTENT_ONLY'	=> $data['ad_content_only'],
88 5
			'AD_VIEWS_LIMIT'  	=> $data['ad_views_limit'],
89 5
			'AD_CLICKS_LIMIT' 	=> $data['ad_clicks_limit'],
90 5
			'AD_OWNER'        	=> $this->get_username($data['ad_owner']),
91 5
		));
92 5
	}
93
94
	/**
95
	 * Assign template locations data to the template.
96
	 *
97
	 * @param	mixed	$ad_locations	The form data or nothing.
98
	 * @return	void
99
	 */
100 7
	public function assign_locations($ad_locations = false)
101
	{
102 7
		foreach ($this->location_manager->get_all_locations() as $location_category_id => $location_category)
103
		{
104 2
			$this->template->assign_block_vars('ad_locations', array(
105 2
				'CATEGORY_NAME' => $this->language->lang($location_category_id),
106 2
			));
107
108 2
			foreach ($location_category as $location_id => $location_data)
109
			{
110 2
				$this->template->assign_block_vars('ad_locations', array(
111 2
					'LOCATION_ID'   => $location_id,
112 2
					'LOCATION_DESC' => $location_data['desc'],
113 2
					'LOCATION_NAME' => $location_data['name'],
114 2
					'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
115 2
				));
116 2
			}
117 7
		}
118 7
	}
119
120
	/**
121
	 * Log action
122
	 *
123
	 * @param	string	$action		Performed action in uppercase
124
	 * @param	string	$ad_name	Advertisement name
125
	 * @return	void
126
	 */
127 1
	public function log($action, $ad_name)
128
	{
129 1
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
130 1
	}
131
132
	/**
133
	 * Get "Find username" URL to easily look for ad owner.
134
	 *
135
	 * @return	string	Find username URL
136
	 */
137 1
	public function get_find_username_link()
138
	{
139 1
		return append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true');
140
	}
141
142
	/**
143
	 * Generate URL to enable visual demo of ad locations.
144
	 *
145
	 * @return	string	Enable visual demo URL
146
	 */
147
	public function get_enable_visual_demo_link()
148
	{
149
		return append_sid("{$this->root_path}index.{$this->php_ext}", 'enable_visual_demo=true');
150
	}
151
152
	/**
153
	 * Is an ad expired?
154
	 *
155
	 * @param	array	$row	Advertisement data
156
	 * @return	bool	True if expired, false otherwise
157
	 */
158 7
	public function is_expired($row)
159
	{
160 7
		if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < time())
161 7
		{
162 1
			return true;
163
		}
164
165 6
		if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit'])
166 6
		{
167 1
			return true;
168
		}
169
170 5
		if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit'])
171 5
		{
172 1
			return true;
173
		}
174
175 4
		return false;
176
	}
177
178
	/**
179
	 * Prepare ad owner for display. Method takes user_id
180
	 * of the ad owner and returns username.
181
	 *
182
	 * @param	int		$user_id	User ID
183
	 * @return	string	Username belonging to $user_id.
184
	 */
185 5
	protected function get_username($user_id)
186
	{
187 5
		if (!$user_id)
188 5
		{
189 2
			return '';
190
		}
191
192 3
		$this->user_loader->load_users(array($user_id));
193 3
		return $this->user_loader->get_username($user_id, 'username');
194
	}
195
}
196