Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

fields::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 9
crap 2

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
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services;
11
12
class fields extends topic
13
{
14
	/** @var \blitze\content\services\comments\comments_interface */
15
	protected $comments;
16
17
	/** @var \blitze\content\services\form\fields_factory */
18
	protected $fields_factory;
19
20
	/** @var array */
21
	protected $form_fields;
22
23
	/** @var string */
24
	protected $content_type;
25
26
	/** @var array */
27
	protected $content_fields;
28
29
	/** @var array */
30
	protected $tags = array();
31
32
	/** @var string */
33
	protected $tpl_name = '';
34
35
	/** @var string */
36
	protected $display_mode = '';
37
38
	/** @var string */
39
	protected $view_mode = '';
40
41
	/** @var array */
42
	protected $label = array('label-hidden', 'label-inline', 'label-newline');
43
44
	/**
45
	 * Construct
46
	 *
47
	 * @param \phpbb\config\db										$config					Config object
48
	 * @param \phpbb\controller\helper								$controller_helper		Controller Helper object
49
	 * @param \phpbb\event\dispatcher_interface						$phpbb_dispatcher		Event dispatcher object
50
	 * @param \phpbb\language\language								$language				Language object
51
	 * @param \phpbb\template\template								$template				Template object
52
	 * @param \phpbb\user											$user					User object
53
	 * @param \blitze\content\services\form\fields_factory			$fields_factory			Form fields factory
54
	 * @param \blitze\content\services\comments\comments_interface	$comments				Comments object
55
	 * @param \blitze\content\services\helper						$helper					Content helper object
56
	 */
57
	public function __construct(\phpbb\config\db $config, \phpbb\controller\helper $controller_helper, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \blitze\content\services\comments\comments_interface $comments, \blitze\content\services\form\fields_factory $fields_factory, \blitze\content\services\helper $helper)
58
	{
59
		parent::__construct($config, $controller_helper, $phpbb_dispatcher, $language, $template, $user, $helper);
60
61
		$this->comments = $comments;
62
		$this->fields_factory = $fields_factory;
63
	}
64
65
	/**
66
	 * Set type data needed to display topics
67
	 *
68
	 * @param \blitze\content\model\entity\type $entity
69
	 * @param array $topic_ids
70
	 * @param array $view_mode_fields
71
	 * @param string $custom_tpl
72
	 * @param string $view_mode
73
	 * @return void
74
	 */
75
	public function prepare_to_show(\blitze\content\model\entity\type $entity, array $topic_ids, array $view_mode_fields, $custom_tpl, $view_mode)
76
	{
77
		$this->reset();
78
		$db_fields = array_fill_keys($topic_ids, array());
79
80
		/**
81
		 * Event to set the values for fields that are stored in the database, as opposed to post text
82
		 *
83
		 * @event blitze.content.fields.set_values
84
		 * @var string								view_mode			The current view mode (summary|detail|block)
85
		 * @var	array								view_mode_fields	Array containing fields for current view_mode
86
		 * @var \blitze\content\model\entity\type	entity				Content type entity
87
		 * @var array								db_fields			This array allows extensions that provide fields to list field values for current topic ids.
88
		 *																Extensions should merge and not overwrite/replace these entries, unless it is necessary to do so
89
		 *																Ex. array([topic_id] => array([field_name] => [field_value]))
90
		 */
91
		$vars = array('view_mode', 'view_mode_fields', 'entity', 'db_fields');
92
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.fields.set_values', compact($vars)));
93
94
		$this->display_mode = $view_mode;
95
		$this->content_type = $entity->get_content_name();
96
		$this->tpl_name	= ($custom_tpl) ? $this->content_type . '_' . $view_mode : '';
97
		$this->view_mode = (in_array($view_mode, array('summary', 'detail'))) ? $view_mode : 'summary';
98
		$this->form_fields = array_intersect_key($this->fields_factory->get_all(), array_flip($view_mode_fields));
99
		$this->db_fields = $db_fields;
0 ignored issues
show
Bug introduced by
The property db_fields does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
		$this->set_content_fields($view_mode_fields, $entity->get_content_fields());
101
	}
102
103
	/**
104
	 * @param string $type
105
	 * @param array $topic_data
106
	 * @param array $post_data
107
	 * @param array $users_cache
108
	 * @param array $attachments
109
	 * @param array $update_count
110
	 * @param array $topic_tracking_info
111
	 * @param array $topic_data_overwrite
112
	 * @param string $mode
113
	 * @return array
114
	 */
115
	public function show($type, array $topic_data, array $post_data, array $users_cache, array &$attachments, array &$update_count, array $topic_tracking_info, array $topic_data_overwrite = array(), $mode = '')
116
	{
117
		$callable = 'get_' . $this->view_mode . '_template_data';
118
		$tpl_data = array_merge(array(
119
				'TOPIC_COMMENTS'	=> $this->comments->count($topic_data),
120
				'S_USER_LOGGED_IN'	=> $this->user->data['is_registered'],
121
			),
122
			$this->{$callable}($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count, $mode),
123
			$topic_data_overwrite
124
		);
125
126
		return $this->build_content($tpl_data);
127
	}
128
129
	/**
130
	 * @param array $tpl_data
131
	 * @return array
132
	 */
133
	public function build_content(array $tpl_data)
134
	{
135
		$fields_data = $this->get_fields_data_for_display($tpl_data);
136
137
		if ($this->tpl_name)
138
		{
139
			$this->template->assign_vars(array_change_key_case(array_merge($tpl_data, $fields_data), CASE_UPPER));
140
			$this->template->set_filenames(array('content' => $this->tpl_name));
141
			$tpl_data['CUSTOM_DISPLAY'] = $this->template->assign_display('content');
142
		}
143
		else
144
		{
145
			$tpl_data['SEQ_DISPLAY'] = join("\n", $fields_data);
146
		}
147
148
		return $tpl_data;
149
	}
150
151
	/**
152
	 * @param array $view_mode_fields
153
	 * @param array $fields_data
154
	 * @return void
155
	 */
156
	protected function set_content_fields(array $view_mode_fields, array $fields_data)
157
	{
158
		foreach ($view_mode_fields as $name => $field_type)
159
		{
160
			if (isset($this->form_fields[$field_type]))
161
			{
162
				$this->tags[$name] = $name;
163
				$this->content_fields[$name] = $fields_data[$name];
164
			}
165
		}
166
	}
167
168
	/**
169
	 * @param array $tpl_data
170
	 * @return array
171
	 */
172
	protected function get_fields_data_for_display(array &$tpl_data)
173
	{
174
		$field_values = array_merge($this->db_fields[$tpl_data['TOPIC_ID']], $this->get_fields_data_from_post($tpl_data['MESSAGE']));
175
		unset($tpl_data['MESSAGE']);
176
177
		$display_data = array();
178
		foreach ($this->content_fields as $field_name => $field_data)
179
		{
180
			$field_type = $field_data['field_type'];
181
			$field_data['field_props'] = array_replace_recursive($this->form_fields[$field_type]->get_default_props(), $field_data['field_props']);
182
			$field_data['field_value'] = &$field_values[$field_name];
183
184
			$field_contents	= $this->form_fields[$field_type]->display_field($field_data, $this->display_mode, $tpl_data, $this->content_type);
185
186
			// this essentially hides other fields if the field returns an array
187
			if (is_array($field_contents))
188
			{
189
				$display_data = $field_contents;
190
				break;
191
			}
192
193
			if (!empty($field_contents))
194
			{
195
				$display_data[$field_name] = '<div class="field-label ' . $this->label[$field_data['field_' . $this->view_mode . '_ldisp']] . '">' . $field_data['field_label'] . $this->language->lang('COLON') . ' </div>' . $field_contents;
196
			}
197
		}
198
199
		return $display_data;
200
	}
201
202
	/**
203
	 * @param string $post_text
204
	 * @return array
205
	 */
206
	protected function get_fields_data_from_post($post_text)
207
	{
208
		$fields_data = array();
209
		$find_tags = join('|', $this->tags);
210
		if (preg_match_all("#<div data-field=\"($find_tags)\">(.*?)</div><br><!-- end field -->#s", $post_text, $matches))
211
		{
212
			$fields_data = array_combine($matches[1], $matches[2]);
213
		}
214
215
		return array_intersect_key($fields_data, $this->tags);
216
	}
217
218
	/**
219
	 * @return void
220
	 */
221
	protected function reset()
222
	{
223
		$this->tags = array();
224
		$this->content_fields = array();
225
		$this->form_fields = array();
226
		$this->tpl_name = '';
227
		$this->view_mode = '';
228
	}
229
}
230