Total Complexity | 45 |
Total Lines | 497 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DataGridActionBase 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 DataGridActionBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | abstract class DataGridActionBase implements DataGridActionInterface |
||
37 | { |
||
38 | /** |
||
39 | * El objeto reflexivo que determina si se muestra la acción |
||
40 | * |
||
41 | * @var callable |
||
42 | */ |
||
43 | protected $runtimeFilter; |
||
44 | /** |
||
45 | * El nombre de la acción |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $name = ''; |
||
50 | /** |
||
51 | * El título de la acción |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $title = ''; |
||
56 | /** |
||
57 | * El id de la acción |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $id = 0; |
||
62 | /** |
||
63 | * La función javascript del evento OnClick |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $onClickFunction = ''; |
||
68 | /** |
||
69 | * Los argumentos de la función OnClick |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $onClickArgs; |
||
74 | /** |
||
75 | * El icono de la acción |
||
76 | * |
||
77 | * @var IconInterface |
||
78 | */ |
||
79 | protected $icon; |
||
80 | /** |
||
81 | * Si se debe de omitir para los elementos del listado |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | protected $isSkip = false; |
||
86 | /** |
||
87 | * La columna de origen de datos que condiciona esta acción |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $filterRowSource; |
||
92 | /** |
||
93 | * Si es una acción de ayuda |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | protected $isHelper; |
||
98 | /** |
||
99 | * El tipo de acción |
||
100 | * |
||
101 | * @var int |
||
102 | */ |
||
103 | protected $type = 0; |
||
104 | /** |
||
105 | * Atributos de datos adicionales |
||
106 | * |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $data; |
||
110 | /** |
||
111 | * Atributos adicionales |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $attributes; |
||
116 | /** |
||
117 | * @var array |
||
118 | */ |
||
119 | protected $classes; |
||
120 | /** |
||
121 | * @var bool |
||
122 | */ |
||
123 | protected $isSelection = false; |
||
124 | |||
125 | /** |
||
126 | * DataGridActionBase constructor. |
||
127 | * |
||
128 | * @param int $id EL id de la acción |
||
129 | */ |
||
130 | public function __construct($id = null) |
||
131 | { |
||
132 | $this->id = $id; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Devolver el método reflexivo que determina si se muestra la acción |
||
137 | * |
||
138 | * @return callable |
||
139 | */ |
||
140 | public function getRuntimeFilter() |
||
141 | { |
||
142 | return $this->runtimeFilter; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Establecer el método reflexivo que determina si se muestra la acción |
||
147 | * |
||
148 | * @param string $class |
||
149 | * @param string $method |
||
150 | * |
||
151 | * @throws \RuntimeException |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function setRuntimeFilter($class, $method) |
||
155 | { |
||
156 | if (method_exists($class, $method)) { |
||
157 | $this->runtimeFilter = function ($filter) use ($method) { |
||
158 | // new \ReflectionMethod($class, $method); |
||
159 | return $filter->{$method}(); |
||
160 | }; |
||
161 | } else { |
||
162 | throw new \RuntimeException('Method does not exist'); |
||
163 | } |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getName() |
||
172 | { |
||
173 | return $this->name; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param $name string |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function setName($name) |
||
182 | { |
||
183 | $this->name = $name; |
||
184 | |||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getId() |
||
192 | { |
||
193 | return $this->id; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param int $id |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function setId($id) |
||
202 | { |
||
203 | $this->id = $id; |
||
204 | |||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getTitle() |
||
212 | { |
||
213 | return $this->title; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param $title string |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setTitle($title) |
||
222 | { |
||
223 | $this->title = $title; |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param $function string |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function setOnClickFunction($function) |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param $args string |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setOnClickArgs($args) |
||
246 | { |
||
247 | if ($this->onClickArgs === null) { |
||
248 | $this->onClickArgs = []; |
||
249 | } |
||
250 | |||
251 | $this->onClickArgs[] = $args; |
||
252 | |||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | public function getOnClick() |
||
260 | { |
||
261 | if ($this->onClickArgs !== null) { |
||
262 | |||
263 | $args = array_map(function ($value) { |
||
264 | return (!is_numeric($value) && $value !== 'this') ? '\'' . $value . '\'' : $value; |
||
265 | }, $this->onClickArgs); |
||
266 | |||
267 | return count($args) > 0 ? $this->onClickFunction . '(' . implode(',', $args) . ')' : $this->onClickFunction; |
||
268 | } |
||
269 | |||
270 | return $this->onClickFunction; |
||
271 | |||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @return IconInterface |
||
276 | */ |
||
277 | public function getIcon() |
||
278 | { |
||
279 | return $this->icon; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param $icon IconInterface |
||
284 | * |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function setIcon($icon) |
||
288 | { |
||
289 | $this->icon = $icon; |
||
290 | |||
291 | return $this; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param $skip bool |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function setSkip($skip) |
||
300 | { |
||
301 | $this->isSkip = $skip; |
||
302 | |||
303 | return $this; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function isSkip() |
||
310 | { |
||
311 | return $this->isSkip; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function isHelper() |
||
318 | { |
||
319 | return $this->isHelper; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @param bool $helper |
||
324 | * |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function setIsHelper($helper) |
||
328 | { |
||
329 | $this->isHelper = $helper; |
||
330 | |||
331 | return $this; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | */ |
||
337 | public function getFilterRowSource() |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Filtro para mostrar la acción |
||
344 | * |
||
345 | * @param $rowSource string |
||
346 | * @param mixed $value Valor a filtrar |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function setFilterRowSource($rowSource, $value = 1) |
||
351 | { |
||
352 | if ($this->filterRowSource === null) { |
||
353 | $this->filterRowSource = []; |
||
354 | } |
||
355 | |||
356 | $this->filterRowSource[] = ['field' => $rowSource, 'value' => $value]; |
||
357 | |||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return int El tipo de acción |
||
363 | */ |
||
364 | public function getType() |
||
365 | { |
||
366 | return $this->type; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @param int $type El tipo de acción definido en DataGridActionType |
||
371 | * |
||
372 | * @return $this |
||
373 | */ |
||
374 | public function setType($type) |
||
375 | { |
||
376 | $this->type = $type; |
||
377 | |||
378 | return $this; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @return array |
||
383 | */ |
||
384 | public function getData() |
||
385 | { |
||
386 | return (array)$this->data; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param array $data Los datos de los atributos |
||
391 | * |
||
392 | * @return $this |
||
393 | */ |
||
394 | public function setData(array $data) |
||
395 | { |
||
396 | $this->data = $data; |
||
397 | |||
398 | return $this; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Añadir nuevo atributo de datos |
||
403 | * |
||
404 | * @param string $name El nombe del atributo |
||
405 | * @param mixed $data Los datos del atributo |
||
406 | * |
||
407 | * @return $this |
||
408 | */ |
||
409 | public function addData($name, $data) |
||
410 | { |
||
411 | if ($this->data === null) { |
||
412 | $this->data = []; |
||
413 | } |
||
414 | |||
415 | $this->data[$name] = $data; |
||
416 | |||
417 | return $this; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @return array |
||
422 | */ |
||
423 | public function getAttributes() |
||
424 | { |
||
425 | return (array)$this->attributes; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Establecer atributos |
||
430 | * |
||
431 | * @param array $attributes Los datos de los atributos |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function setAttributes(array $attributes) |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Añadir nuevo atributo |
||
444 | * |
||
445 | * @param string $name El nombe del atributo |
||
446 | * @param mixed $value |
||
447 | * |
||
448 | * @return $this |
||
449 | */ |
||
450 | public function addAttribute($name, $value) |
||
451 | { |
||
452 | if ($this->attributes === null) { |
||
453 | $this->attributes = []; |
||
454 | } |
||
455 | |||
456 | $this->attributes[$name] = $value; |
||
457 | |||
458 | return $this; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Returns classes as a string |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | public function getClassesAsString() |
||
467 | { |
||
468 | if ($this->classes === null) { |
||
469 | return ''; |
||
470 | } |
||
471 | |||
472 | return implode(' ', $this->classes); |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Returns classes |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | public function getClasses() |
||
481 | { |
||
482 | return (array)$this->classes; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Set classes |
||
487 | * |
||
488 | * @param array $classes |
||
489 | */ |
||
490 | public function setClasses(array $classes) |
||
491 | { |
||
492 | $this->classes = $classes; |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Adds a new class |
||
497 | * |
||
498 | * @param mixed $value |
||
499 | * |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function addClass($value) |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Returns if the action is used for selecting multiple items |
||
515 | * |
||
516 | * @return bool |
||
517 | */ |
||
518 | public function isSelection(): bool |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * @param bool $isSelection |
||
525 | * |
||
526 | * @return DataGridActionBase |
||
527 | */ |
||
528 | public function setIsSelection(bool $isSelection) |
||
529 | { |
||
530 | $this->isSelection = $isSelection; |
||
531 | |||
532 | return $this; |
||
533 | } |
||
534 | } |
||
535 |