Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php namespace VojtaSvoboda\Reservations\Controllers; |
||
12 | class Reservations extends Controller |
||
13 | { |
||
14 | public $implement = [ |
||
15 | 'Backend\Behaviors\ListController', |
||
16 | 'Backend\Behaviors\FormController', |
||
17 | 'Backend\Behaviors\ImportExportController', |
||
18 | ]; |
||
19 | |||
20 | public $listConfig = 'config_list.yaml'; |
||
21 | public $formConfig = 'config_form.yaml'; |
||
22 | public $importExportConfig = 'config_import_export.yaml'; |
||
23 | |||
24 | private $stateColors; |
||
25 | private $stateNames; |
||
26 | |||
27 | public $requiredPermissions = [ |
||
28 | 'vojtasvoboda.reservations.reservations', |
||
29 | ]; |
||
30 | |||
31 | public function __construct() |
||
32 | { |
||
33 | parent::__construct(); |
||
34 | |||
35 | $action = $this->action == 'export' ? 'export' : 'reservations'; |
||
36 | BackendMenu::setContext('VojtaSvoboda.Reservations', 'reservations', $action); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Extend controller listing. |
||
41 | */ |
||
42 | public function index() |
||
43 | { |
||
44 | $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/bulk-actions.js'); |
||
45 | $this->asExtension('ListController')->index(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Override displaying reservation listing. |
||
50 | * |
||
51 | * @param Reservation $record |
||
52 | * @param string $columnName |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function listOverrideColumnValue($record, $columnName) |
||
57 | { |
||
58 | $statusStyle = "display:inline-block;width:13px;height:14px;position:relative;top:2px;margin-right:4px;color:#fff;font-size:11px;padding-left:3px;"; |
||
59 | |||
60 | if ($columnName == 'status_id') { |
||
61 | $color = $this->getStateColor($record->status_id); |
||
62 | $name = $this->getStateName($record->status_id); |
||
63 | |||
64 | return '<div><span style="' . $statusStyle . 'background-color:' . $color . '"></span> ' . $name . '</div>'; |
||
65 | } |
||
66 | |||
67 | if ($columnName == 'returning') { |
||
68 | return $this->isReturning($record->email) ? '<i class=" icon-star"></i>' : ''; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Bulk change state and bulk delete. |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function index_onBulkAction() |
||
78 | { |
||
79 | if (($bulkAction = post('action')) && ($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { |
||
80 | if ($bulkAction == 'delete') { |
||
81 | $this->getFacade()->bulkDelete($checkedIds); |
||
82 | } else { |
||
83 | $this->getFacade()->bulkStateChange($checkedIds, $bulkAction); |
||
84 | } |
||
85 | |||
86 | $msg = Lang::get('vojtasvoboda.reservations::lang.reservations.change_status_success'); |
||
87 | Flash::success($msg); |
||
88 | } |
||
89 | |||
90 | return $this->listRefresh(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Return if User is returning. |
||
95 | * |
||
96 | * @param string $email Users email. |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | private function isReturning($email) |
||
104 | |||
105 | /** |
||
106 | * Get facade providing all plugin logic. |
||
107 | * |
||
108 | * @return ReservationsFacade |
||
109 | */ |
||
110 | private function getFacade() |
||
114 | |||
115 | /** |
||
116 | * Get color by state ident. |
||
117 | * |
||
118 | * @param string $ident |
||
119 | * |
||
120 | * @return string|null |
||
121 | */ |
||
122 | View Code Duplication | private function getStateColor($ident) |
|
130 | |||
131 | /** |
||
132 | * Get name by state ident. |
||
133 | * |
||
134 | * @param string $ident |
||
135 | * |
||
136 | * @return string|null |
||
137 | */ |
||
138 | View Code Duplication | private function getStateName($ident) |
|
146 | } |
||
147 |