Total Complexity | 46 |
Total Lines | 412 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class builder |
||
15 | { |
||
16 | /** @var \phpbb\auth\auth */ |
||
17 | protected $auth; |
||
18 | |||
19 | /** @var \phpbb\event\dispatcher_interface */ |
||
20 | protected $phpbb_dispatcher; |
||
21 | |||
22 | /** @var \phpbb\language\language */ |
||
23 | protected $language; |
||
24 | |||
25 | /** @var \phpbb\request\request_interface */ |
||
26 | protected $request; |
||
27 | |||
28 | /** @var \phpbb\template\context */ |
||
29 | protected $template_context; |
||
30 | |||
31 | /** @var \phpbb\user */ |
||
32 | protected $user; |
||
33 | |||
34 | /* @var \blitze\content\services\fields */ |
||
35 | protected $fields; |
||
36 | |||
37 | /* @var \blitze\content\services\types */ |
||
38 | protected $types; |
||
39 | |||
40 | /** @var \blitze\content\services\form\form */ |
||
41 | protected $form; |
||
42 | |||
43 | /** @var string */ |
||
44 | protected $phpbb_root_path; |
||
45 | |||
46 | /** @var string */ |
||
47 | protected $php_ext; |
||
48 | |||
49 | /** @var string */ |
||
50 | protected $mode = ''; |
||
51 | |||
52 | /** @var int */ |
||
53 | protected $topic_time = 0; |
||
54 | |||
55 | /** @var bool */ |
||
56 | protected $req_approval = false; |
||
57 | |||
58 | /** @var bool */ |
||
59 | protected $req_mod_input = false; |
||
60 | |||
61 | /** @var bool */ |
||
62 | protected $user_is_mod = false; |
||
63 | |||
64 | /** @var array */ |
||
65 | protected $content_fields = array(); |
||
66 | |||
67 | /** |
||
68 | * Constructor |
||
69 | * |
||
70 | * @param \phpbb\auth\auth $auth Auth object |
||
71 | * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object |
||
72 | * @param \phpbb\language\language $language Language object |
||
73 | * @param \phpbb\request\request_interface $request Request object |
||
74 | * @param \phpbb\template\context $template_context Template context object |
||
75 | * @param \phpbb\user $user User object |
||
76 | * @param \blitze\content\services\fields $fields Content fields object |
||
77 | * @param \blitze\content\services\types $types Content types object |
||
78 | * @param \blitze\content\services\form\form $form Form object |
||
79 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
80 | * @param string $php_ext php file extension |
||
81 | */ |
||
82 | public function __construct(\phpbb\auth\auth $auth, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\request\request_interface $request, \phpbb\template\context $template_context, \phpbb\user $user, \blitze\content\services\fields $fields, \blitze\content\services\types $types, \blitze\content\services\form\form $form, $phpbb_root_path, $php_ext) |
||
83 | { |
||
84 | $this->auth = $auth; |
||
85 | $this->phpbb_dispatcher = $phpbb_dispatcher; |
||
86 | $this->language = $language; |
||
87 | $this->request = $request; |
||
88 | $this->template_context = $template_context; |
||
89 | $this->user = $user; |
||
90 | $this->fields = $fields; |
||
91 | $this->types = $types; |
||
92 | $this->form = $form; |
||
93 | $this->phpbb_root_path = $phpbb_root_path; |
||
94 | $this->php_ext = $php_ext; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param int $forum_id |
||
99 | * @param int $topic_id |
||
100 | * @param string $mode |
||
101 | * @param bool $save_draft |
||
102 | * @return array |
||
103 | */ |
||
104 | public function init($forum_id, $topic_id, $mode, $save_draft) |
||
105 | { |
||
106 | $content_langname = ''; |
||
107 | if ($type = $this->types->get_forum_type($forum_id)) |
||
108 | { |
||
109 | $this->language->add_lang('posting', 'blitze/content'); |
||
110 | |||
111 | /** @var \blitze\content\model\entity\type $entity */ |
||
112 | $entity = $this->types->get_type($type, true); |
||
113 | $content_langname = $entity->get_content_langname(); |
||
114 | $fields_data = $entity->get_content_fields(); |
||
115 | |||
116 | /** |
||
117 | * Event to set the values for fields that are stored in the database, for purposes of displaying the form field |
||
118 | * |
||
119 | * @event blitze.content.builder.set_field_values |
||
120 | * @var int topic_id Current topic id |
||
121 | * @var array fields_data Array containing fields data, minus 'field_value' prop, which is what we are setting here |
||
122 | * @var \blitze\content\model\entity\type entity Content type entity |
||
123 | */ |
||
124 | $vars = array('topic_id', 'fields_data', 'entity'); |
||
125 | extract($this->phpbb_dispatcher->trigger_event('blitze.content.builder.set_field_values', compact($vars))); |
||
126 | |||
127 | $this->content_fields = $fields_data; |
||
128 | $this->user_is_mod = $this->auth->acl_get('m_', $entity->get_forum_id()); |
||
129 | $this->req_approval = $entity->get_req_approval(); |
||
130 | $this->mode = $this->request->variable('cp', ($this->user_is_mod && $mode !== 'post') ? 'mcp' : 'ucp'); |
||
131 | |||
132 | if ($save_draft && !$this->request->is_set('message')) |
||
133 | { |
||
134 | $this->request->overwrite('message', $this->generate_message()); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return array($type, $content_langname); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function generate_message() |
||
145 | { |
||
146 | $fields_data = $this->form->get_submitted_data($this->content_fields, $this->req_mod_input, $this->mode); |
||
147 | |||
148 | $message = ''; |
||
149 | foreach ($fields_data as $field => $value) |
||
150 | { |
||
151 | $value = is_array($value) ? join("\n", $value) : $value; |
||
152 | $message .= '[smcf=' . $field . ']' . $value . '[/smcf]'; |
||
153 | } |
||
154 | |||
155 | return $message; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | public function get_errors() |
||
162 | { |
||
163 | return $this->form->get_errors(); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | public function get_cp_url() |
||
170 | { |
||
171 | return append_sid("{$this->phpbb_root_path}{$this->mode}.$this->php_ext", "i=-blitze-content-{$this->mode}-content_module&mode=content"); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string $content_type |
||
176 | * @param array $topic_data |
||
177 | * @return string |
||
178 | */ |
||
179 | public function get_post_url($content_type, array $topic_data) |
||
180 | { |
||
181 | return $this->fields->get_topic_url($content_type, $topic_data); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param string $content_type |
||
186 | * @param array $post_data |
||
187 | * @param string $view |
||
188 | * @return string |
||
189 | */ |
||
190 | public function get_content_view($content_type, array $post_data, $view) |
||
191 | { |
||
192 | $text = ''; |
||
193 | if ($entity = $this->types->get_type($content_type)) |
||
194 | { |
||
195 | $fields_accessor = 'get_' . $view . '_fields'; |
||
196 | $template_accessor = 'get_' . $view . '_tpl'; |
||
197 | |||
198 | // we do this to ensure topic_id keys exists when previewing a new topic |
||
199 | $post_data += array('topic_id' => 0); |
||
200 | |||
201 | $this->fields->prepare_to_show($entity, array($post_data['topic_id']), $entity->$fields_accessor(), $entity->$template_accessor(), $view); |
||
202 | $this->fields->set_view_mode('preview'); |
||
203 | |||
204 | $content = $this->fields->build_content(array_change_key_case($post_data, CASE_UPPER)); |
||
205 | |||
206 | $text = isset($content['CUSTOM_DISPLAY']) ? $content['CUSTOM_DISPLAY'] : join('', $content['FIELDS']['all']); |
||
207 | } |
||
208 | return $text; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param string $content_type |
||
213 | * @param array $post_data |
||
214 | * @return string |
||
215 | */ |
||
216 | public function generate_preview($content_type, array $post_data) |
||
217 | { |
||
218 | $post_data['MESSAGE'] = $this->template_context->get_root_ref()['PREVIEW_MESSAGE']; |
||
219 | |||
220 | return $this->get_content_view($content_type, $post_data, 'detail'); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param array $data |
||
225 | * @return void |
||
226 | */ |
||
227 | public function modify_posting_data(array &$data) |
||
228 | { |
||
229 | $this->topic_time = time(); |
||
230 | $data['post_time'] = $this->topic_time; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param array $sql_data |
||
235 | * @return void |
||
236 | */ |
||
237 | public function modify_sql_data(array &$sql_data) |
||
238 | { |
||
239 | $slugify = new Slugify(); |
||
240 | $sql_data['topic_slug'] = $slugify->slugify($sql_data['topic_title']); |
||
241 | $sql_data['req_mod_input'] = $this->req_mod_input; |
||
242 | |||
243 | $this->topic_time = $this->request->variable('topic_time', $this->topic_time); |
||
244 | |||
245 | if ($this->mode === 'mcp') |
||
246 | { |
||
247 | $publish_on = $this->request->variable('publish_on', ''); |
||
248 | $posted_on = $this->user->format_date($this->topic_time, 'm/d/Y H:i'); |
||
249 | |||
250 | if ($publish_on !== $posted_on) |
||
251 | { |
||
252 | $this->topic_time = strtotime($publish_on); |
||
253 | $sql_data['topic_time'] = $this->topic_time; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param array $topic_data |
||
260 | * @return void |
||
261 | */ |
||
262 | public function save_db_fields(array $topic_data) |
||
263 | { |
||
264 | $topic_data['topic_time'] = (!empty($topic_data['topic_time'])) ? $topic_data['topic_time'] : $this->topic_time; |
||
265 | $this->form->save_db_fields($topic_data, $this->content_fields); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param int $topic_id |
||
270 | * @param array $post_data |
||
271 | * @param array $page_data |
||
272 | * @return string |
||
273 | */ |
||
274 | public function generate_form($topic_id, array &$post_data, array $page_data = array()) |
||
275 | { |
||
276 | $this->set_field_values($post_data['post_text']); |
||
277 | |||
278 | $this->form->create('postform', 'posting') |
||
279 | ->add('cp', 'hidden', array('field_value' => $this->mode)); |
||
280 | |||
281 | foreach ($this->content_fields as $field => $field_data) |
||
282 | { |
||
283 | if ($field_data['field_type'] === 'textarea') |
||
284 | { |
||
285 | $field_data += $page_data; |
||
286 | } |
||
287 | $this->add_field($field, $field_data, $topic_id); |
||
288 | } |
||
289 | $this->add_moderator_fields($post_data); |
||
290 | |||
291 | return $this->form->get_form(false); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param string $field |
||
296 | * @param array $field_data |
||
297 | * @param int $topic_id |
||
298 | * @return void |
||
299 | */ |
||
300 | protected function add_field($field, array $field_data, $topic_id) |
||
301 | { |
||
302 | if (!$field_data['field_mod_only'] || $this->mode === 'mcp') |
||
303 | { |
||
304 | $this->form->add($field, $field_data['field_type'], $field_data, $topic_id); |
||
305 | } |
||
306 | else if (!empty($field_data['field_value'])) |
||
307 | { |
||
308 | $this->form->add($field, 'hidden', $field_data, $topic_id); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param array $post_data |
||
314 | * @return void |
||
315 | */ |
||
316 | protected function add_moderator_fields(array $post_data) |
||
317 | { |
||
318 | if ($this->mode === 'mcp') |
||
319 | { |
||
320 | $this->form->add('topic_time', 'hidden', array('field_value' => $post_data['topic_time'])) |
||
321 | ->add('publish_on', 'datetime', array( |
||
322 | 'field_label' => $this->language->lang('CONTENT_PUBLISH_DATE'), |
||
323 | 'field_value' => $this->user->format_date($post_data['topic_time'], 'm/d/Y H:i'), |
||
324 | 'field_props' => array( |
||
325 | 'min_date' => 0, |
||
326 | ), |
||
327 | )) |
||
328 | ->add('force_status', 'radio', array( |
||
329 | 'field_label' => 'CONTENT_FORCE_STATUS', |
||
330 | 'field_value' => '-1', |
||
331 | 'field_props' => array( |
||
332 | 'vertical' => true, |
||
333 | 'options' => $this->get_moderator_options($post_data['topic_visibility']), |
||
334 | ) |
||
335 | ) |
||
336 | ); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param string $mode |
||
342 | * @param array $data |
||
343 | * @return void |
||
344 | */ |
||
345 | public function force_visibility($mode, array &$data) |
||
346 | { |
||
347 | if ($this->mode === 'mcp') |
||
348 | { |
||
349 | if ('-1' !== $visibility = $this->request->variable('force_status', '')) |
||
350 | { |
||
351 | $data['force_visibility'] = $visibility; |
||
352 | } |
||
353 | } |
||
354 | else if ($this->force_state()) |
||
355 | { |
||
356 | $data['force_visibility'] = ($mode == 'edit_first_post') ? ITEM_REAPPROVE : ITEM_UNAPPROVED; |
||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @return bool |
||
362 | */ |
||
363 | protected function force_state() |
||
364 | { |
||
365 | return ($this->req_approval || $this->req_mod_input); |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param string $post_text |
||
370 | * @return void |
||
371 | */ |
||
372 | protected function set_field_values($post_text) |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Get fields data from post |
||
387 | * |
||
388 | * @param string $post_text |
||
389 | * @param array $fields |
||
390 | * @return array |
||
391 | */ |
||
392 | protected function get_fields_data_from_post($post_text, array $fields) |
||
393 | { |
||
394 | $fields_data = array(); |
||
395 | $find_fields = join('|', $fields); |
||
396 | |||
397 | if (preg_match_all("/\[smcf=($find_fields)\](.*?)\[\/smcf]/s", $post_text, $matches)) |
||
398 | { |
||
399 | $fields_data = array_combine($matches[1], $matches[2]); |
||
400 | } |
||
401 | |||
402 | return $fields_data; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param int $visibility |
||
407 | * @return array |
||
408 | */ |
||
409 | protected function get_moderator_options($visibility) |
||
426 | } |
||
427 | } |
||
428 |