Completed
Pull Request — master (#114)
by Jakub
11:39
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_VIEWS_LIMIT'  => $data['ad_views_limit'],
88 5
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
89 5
			'AD_OWNER'        => $this->get_username($data['ad_owner']),
90 5
		));
91 5
	}
92
93
	/**
94
	 * Assign template locations data to the template.
95
	 *
96
	 * @param	mixed	$ad_locations	The form data or nothing.
97
	 * @return	void
98
	 */
99 7
	public function assign_locations($ad_locations = false)
100
	{
101 7 View Code Duplication
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
102
		{
103 2
			$this->template->assign_block_vars('ad_locations', array(
104 2
				'LOCATION_ID'   => $location_id,
105 2
				'LOCATION_DESC' => $location_data['desc'],
106 2
				'LOCATION_NAME' => $location_data['name'],
107 2
				'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
108 2
			));
109 7
		}
110 7
	}
111
112
	/**
113
	 * Log action
114
	 *
115
	 * @param	string	$action		Performed action in uppercase
116
	 * @param	string	$ad_name	Advertisement name
117
	 * @return	void
118
	 */
119 1
	public function log($action, $ad_name)
120
	{
121 1
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
122 1
	}
123
124
	/**
125
	 * Get "Find username" URL to easily look for ad owner.
126
	 *
127
	 * @return	string	Find username URL
128
	 */
129 1
	public function get_find_username_link()
130
	{
131 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');
132
	}
133
134
	/**
135
	 * Generate URL to enable visual demo of ad locations.
136
	 *
137
	 * @return	string	Enable visual demo URL
138
	 */
139
	public function get_enable_visual_demo_link()
140
	{
141
		return append_sid("{$this->root_path}index.{$this->php_ext}", 'enable_visual_demo=true');
142
	}
143
144
	/**
145
	 * Is an ad expired?
146
	 *
147
	 * @param	array	$row	Advertisement data
148
	 * @return	bool	True if expired, false otherwise
149
	 */
150 7
	public function is_expired($row)
151
	{
152 7
		if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < time())
153 7
		{
154 1
			return true;
155
		}
156
157 6
		if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit'])
158 6
		{
159 1
			return true;
160
		}
161
162 5
		if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit'])
163 5
		{
164 1
			return true;
165
		}
166
167 4
		return false;
168
	}
169
170
	/**
171
	 * Prepare ad owner for display. Method takes user_id
172
	 * of the ad owner and returns username.
173
	 *
174
	 * @param	int		$user_id	User ID
175
	 * @return	string	Username belonging to $user_id.
176
	 */
177 5
	protected function get_username($user_id)
178
	{
179 5
		if (!$user_id)
180 5
		{
181 2
			return '';
182
		}
183
184 3
		$this->user_loader->load_users(array($user_id));
185 3
		return $this->user_loader->get_username($user_id, 'username');
186
	}
187
}
188