Total Complexity | 40 |
Total Lines | 355 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 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 |
||
15 | class builder |
||
16 | { |
||
17 | /** @var \phpbb\auth\auth */ |
||
18 | protected $auth; |
||
19 | |||
20 | /** @var \phpbb\event\dispatcher_interface */ |
||
21 | protected $phpbb_dispatcher; |
||
22 | |||
23 | /** @var \phpbb\language\language */ |
||
24 | protected $language; |
||
25 | |||
26 | /** @var \phpbb\request\request_interface */ |
||
27 | protected $request; |
||
28 | |||
29 | /** @var \phpbb\template\context */ |
||
30 | protected $template_context; |
||
31 | |||
32 | /** @var \phpbb\user */ |
||
33 | protected $user; |
||
34 | |||
35 | /* @var \blitze\content\services\fields */ |
||
36 | protected $fields; |
||
37 | |||
38 | /* @var \blitze\content\services\types */ |
||
39 | protected $types; |
||
40 | |||
41 | /** @var \blitze\content\services\form\form */ |
||
42 | protected $form; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $phpbb_root_path; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $php_ext; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $mode = ''; |
||
52 | |||
53 | /** @var int */ |
||
54 | protected $topic_time = 0; |
||
55 | |||
56 | /** @var bool */ |
||
57 | protected $req_approval = false; |
||
58 | |||
59 | /** @var bool */ |
||
60 | protected $req_mod_input = false; |
||
61 | |||
62 | /** @var bool */ |
||
63 | protected $user_is_mod = false; |
||
64 | |||
65 | /** @var array */ |
||
66 | protected $content_fields = array(); |
||
67 | |||
68 | /** |
||
69 | * Constructor |
||
70 | * |
||
71 | * @param \phpbb\auth\auth $auth Auth object |
||
72 | * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object |
||
73 | * @param \phpbb\language\language $language Language object |
||
74 | * @param \phpbb\request\request_interface $request Request object |
||
75 | * @param \phpbb\template\context $template_context Template context object |
||
76 | * @param \phpbb\user $user User object |
||
77 | * @param \blitze\content\services\fields $fields Content fields object |
||
78 | * @param \blitze\content\services\types $types Content types object |
||
79 | * @param \blitze\content\services\form\form $form Form object |
||
80 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
81 | * @param string $php_ext php file extension |
||
82 | */ |
||
83 | 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) |
||
84 | { |
||
85 | $this->auth = $auth; |
||
86 | $this->phpbb_dispatcher = $phpbb_dispatcher; |
||
87 | $this->language = $language; |
||
88 | $this->request = $request; |
||
89 | $this->template_context = $template_context; |
||
90 | $this->user = $user; |
||
91 | $this->fields = $fields; |
||
92 | $this->types = $types; |
||
93 | $this->form = $form; |
||
94 | $this->phpbb_root_path = $phpbb_root_path; |
||
95 | $this->php_ext = $php_ext; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param int $forum_id |
||
100 | * @param int $topic_id |
||
101 | * @param string $mode |
||
102 | * @param bool $save_draft |
||
103 | * @return array |
||
104 | */ |
||
105 | public function init($forum_id, $topic_id, $mode, $save_draft) |
||
106 | { |
||
107 | $content_langname = ''; |
||
108 | if ($type = $this->types->get_forum_type($forum_id)) |
||
109 | { |
||
110 | $this->language->add_lang('posting', 'blitze/content'); |
||
111 | |||
112 | /** @var \blitze\content\model\entity\type $entity */ |
||
113 | $entity = $this->types->get_type($type, true); |
||
114 | $content_langname = $entity->get_content_langname(); |
||
115 | $fields_data = $entity->get_content_fields(); |
||
116 | |||
117 | /** |
||
118 | * Event to set the values for fields that are stored in the database, for purposes of displaying the form field |
||
119 | * |
||
120 | * @event blitze.content.builder.set_field_values |
||
121 | * @var int topic_id Current topic id |
||
122 | * @var array fields_data Array containing fields data, minus 'field_value' prop, which is what we are setting here |
||
123 | * @var \blitze\content\model\entity\type entity Content type entity |
||
124 | */ |
||
125 | $vars = array('topic_id', 'fields_data', 'entity'); |
||
126 | extract($this->phpbb_dispatcher->trigger_event('blitze.content.builder.set_field_values', compact($vars))); |
||
127 | |||
128 | $this->content_fields = $fields_data; |
||
129 | $this->user_is_mod = $this->auth->acl_get('m_', $entity->get_forum_id()); |
||
130 | $this->req_approval = $entity->get_req_approval(); |
||
131 | $this->mode = $this->request->variable('cp', ($this->user_is_mod && $mode !== 'post') ? 'mcp' : 'ucp'); |
||
132 | |||
133 | $this->form->create('postform', 'posting') |
||
134 | ->add('cp', 'hidden', array('field_value' => $this->mode)) |
||
135 | ->add('redirect', 'hidden', array('field_value' => $this->get_redirect_url())); |
||
136 | |||
137 | if ($save_draft && !$this->request->is_set('message')) |
||
138 | { |
||
139 | $this->request->overwrite('message', $this->generate_message()); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return array($type, $content_langname); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | public function generate_message() |
||
150 | { |
||
151 | $fields_data = $this->form->get_submitted_data($this->content_fields, $this->req_mod_input, $this->mode); |
||
152 | |||
153 | $message = ''; |
||
154 | foreach ($fields_data as $field => $value) |
||
155 | { |
||
156 | $value = is_array($value) ? join("\n", $value) : $value; |
||
157 | $message .= '[smcf=' . $field . ']' . $value . '[/smcf]'; |
||
158 | } |
||
159 | |||
160 | return $message; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return array |
||
165 | */ |
||
166 | public function get_errors() |
||
167 | { |
||
168 | return $this->form->get_errors(); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | public function get_redirect_url() |
||
175 | { |
||
176 | return $this->request->variable('redirect', ''); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $content_type |
||
181 | * @param array $post_data |
||
182 | * @param string $view |
||
183 | * @return string |
||
184 | */ |
||
185 | public function get_content_view($content_type, array $post_data, $view) |
||
186 | { |
||
187 | $text = ''; |
||
188 | if ($entity = $this->types->get_type($content_type)) |
||
189 | { |
||
190 | $fields_accessor = 'get_' . $view . '_fields'; |
||
191 | $template_accessor = 'get_' . $view . '_tpl'; |
||
192 | |||
193 | // we do this to ensure topic_id keys exists when previewing a new topic |
||
194 | $post_data += array('topic_id' => 0); |
||
195 | |||
196 | $this->fields->prepare_to_show($entity, array($post_data['topic_id']), $entity->$fields_accessor(), $entity->$template_accessor(), $view) |
||
197 | ->set_display_mode('preview'); |
||
198 | |||
199 | $content = $this->fields->build_content(array_change_key_case($post_data, CASE_UPPER)); |
||
200 | |||
201 | $text = isset($content['CUSTOM_DISPLAY']) ? $content['CUSTOM_DISPLAY'] : join('', $content['FIELDS']['all']); |
||
202 | } |
||
203 | return $text; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param string $content_type |
||
208 | * @param array $post_data |
||
209 | * @return string |
||
210 | */ |
||
211 | public function generate_preview($content_type, array $post_data) |
||
212 | { |
||
213 | $post_data['MESSAGE'] = $this->template_context->get_root_ref()['PREVIEW_MESSAGE']; |
||
214 | |||
215 | return $this->get_content_view($content_type, $post_data, 'detail'); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param array $sql_data |
||
220 | * @return void |
||
221 | */ |
||
222 | public function modify_sql_data(array &$sql_data) |
||
223 | { |
||
224 | $slugify = new Slugify(); |
||
225 | $sql_data['topic_slug'] = $slugify->slugify($sql_data['topic_title']); |
||
226 | $sql_data['req_mod_input'] = $this->req_mod_input; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param array $topic_data |
||
231 | * @return void |
||
232 | */ |
||
233 | public function save_db_fields(array $topic_data) |
||
234 | { |
||
235 | $this->form->save_db_fields($topic_data, $this->content_fields); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param int $topic_id |
||
240 | * @param array $post_data |
||
241 | * @param array $page_data |
||
242 | * @return void |
||
243 | */ |
||
244 | public function generate_form($topic_id, array $post_data, array $page_data = array()) |
||
245 | { |
||
246 | $this->set_field_values($post_data['post_text']); |
||
247 | |||
248 | foreach ($this->content_fields as $field => $field_data) |
||
249 | { |
||
250 | if ($field_data['field_type'] === 'textarea') |
||
251 | { |
||
252 | $field_data += $page_data; |
||
253 | } |
||
254 | $this->add_field($field, $field_data, $topic_id); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @return void |
||
260 | */ |
||
261 | public function build_form() |
||
262 | { |
||
263 | $this->form->build_form(false); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param string $field |
||
268 | * @param array $field_data |
||
269 | * @param int $topic_id |
||
270 | * @return void |
||
271 | */ |
||
272 | protected function add_field($field, array $field_data, $topic_id) |
||
273 | { |
||
274 | if (!$field_data['field_mod_only'] || $this->mode === 'mcp') |
||
275 | { |
||
276 | $this->form->add($field, $field_data['field_type'], $field_data, $topic_id); |
||
277 | } |
||
278 | else if (!empty($field_data['field_value'])) |
||
279 | { |
||
280 | $this->form->add($field, 'hidden', $field_data, $topic_id); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param string $mode |
||
286 | * @param array $data |
||
287 | * @return void |
||
288 | */ |
||
289 | public function force_visibility($mode, array &$data) |
||
290 | { |
||
291 | if ($this->mode === 'mcp') |
||
292 | { |
||
293 | if ('-1' !== $visibility = $this->request->variable('force_status', '')) |
||
294 | { |
||
295 | $data['force_visibility'] = $visibility; |
||
296 | } |
||
297 | } |
||
298 | else if ($this->force_state()) |
||
299 | { |
||
300 | $data['force_visibility'] = ($mode == 'edit_first_post') ? ITEM_REAPPROVE : ITEM_UNAPPROVED; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return bool |
||
306 | */ |
||
307 | protected function force_state() |
||
308 | { |
||
309 | return ($this->req_approval || $this->req_mod_input); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param string $post_text |
||
314 | * @return void |
||
315 | */ |
||
316 | protected function set_field_values($post_text) |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Get fields data from post |
||
331 | * |
||
332 | * @param string $post_text |
||
333 | * @param array $fields |
||
334 | * @return array |
||
335 | */ |
||
336 | protected function get_fields_data_from_post($post_text, array $fields) |
||
337 | { |
||
338 | $fields_data = array(); |
||
339 | $find_fields = join('|', $fields); |
||
340 | |||
341 | if (preg_match_all("/\[smcf=($find_fields)\](.*?)\[\/smcf]/s", $post_text, $matches)) |
||
342 | { |
||
343 | $fields_data = (array) array_combine($matches[1], $matches[2]); |
||
344 | } |
||
345 | |||
346 | return $fields_data; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * @param int $visibility |
||
351 | * @return array |
||
352 | */ |
||
353 | protected function get_moderator_options($visibility) |
||
370 | } |
||
371 | } |
||
372 |