Total Complexity | 51 |
Total Lines | 290 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like EditContacto 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 EditContacto, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class EditContacto extends EditController |
||
36 | { |
||
37 | use DocFilesTrait; |
||
|
|||
38 | |||
39 | public function getImageUrl(): string |
||
40 | { |
||
41 | $mvn = $this->getMainViewName(); |
||
42 | return $this->views[$mvn]->model->gravatar(); |
||
43 | } |
||
44 | |||
45 | public function getModelClassName(): string |
||
48 | } |
||
49 | |||
50 | public function getPageData(): array |
||
51 | { |
||
52 | $data = parent::getPageData(); |
||
53 | $data['menu'] = 'sales'; |
||
54 | $data['title'] = 'contact'; |
||
55 | $data['icon'] = 'fas fa-address-book'; |
||
56 | return $data; |
||
57 | } |
||
58 | |||
59 | protected function addConversionButtons(string $viewName, BaseView $view) |
||
78 | ]); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | protected function checkViesAction(): bool |
||
83 | { |
||
84 | $model = $this->getModel(); |
||
85 | if (false === $model->loadFromCode($this->request->get('code'))) { |
||
86 | return true; |
||
87 | } |
||
88 | |||
89 | $model->checkVies(); |
||
90 | return true; |
||
91 | } |
||
92 | |||
93 | protected function createCustomerAction() |
||
94 | { |
||
95 | $access = $this->getRolePermissions('EditCliente'); |
||
96 | if (false === $access['allowupdate']) { |
||
97 | Tools::log()->warning('not-allowed-update'); |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | $mvn = $this->getMainViewName(); |
||
102 | $customer = $this->views[$mvn]->model->getCustomer(); |
||
103 | if ($customer->exists()) { |
||
104 | Tools::log()->notice('record-updated-correctly'); |
||
105 | $this->redirect($customer->url() . '&action=save-ok'); |
||
106 | return; |
||
107 | } |
||
108 | |||
109 | Tools::log()->error('record-save-error'); |
||
110 | } |
||
111 | |||
112 | protected function createEmailsView(string $viewName = 'ListEmailSent') |
||
113 | { |
||
114 | $this->addListView($viewName, 'EmailSent', 'emails-sent', 'fas fa-envelope'); |
||
115 | $this->views[$viewName]->addOrderBy(['date'], 'date', 2); |
||
116 | $this->views[$viewName]->addSearchFields(['addressee', 'body', 'subject']); |
||
117 | |||
118 | // desactivamos la columna de destinatario |
||
119 | $this->views[$viewName]->disableColumn('to'); |
||
120 | |||
121 | // desactivamos el botón de nuevo |
||
122 | $this->setSettings($viewName, 'btnNew', false); |
||
123 | } |
||
124 | |||
125 | protected function createSupplierAction() |
||
126 | { |
||
127 | $access = $this->getRolePermissions('EditProveedor'); |
||
128 | if (false === $access['allowupdate']) { |
||
129 | Tools::log()->warning('not-allowed-update'); |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | $mvn = $this->getMainViewName(); |
||
134 | $supplier = $this->views[$mvn]->model->getSupplier(); |
||
135 | if ($supplier->exists()) { |
||
136 | Tools::log()->notice('record-updated-correctly'); |
||
137 | $this->redirect($supplier->url() . '&action=save-ok'); |
||
138 | return; |
||
139 | } |
||
140 | |||
141 | Tools::log()->error('record-save-error'); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Create views. |
||
146 | */ |
||
147 | protected function createViews() |
||
148 | { |
||
149 | parent::createViews(); |
||
150 | $this->createEmailsView(); |
||
151 | $this->createViewDocFiles(); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | protected function editAction() |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Run the controller after actions |
||
169 | * |
||
170 | * @param string $action |
||
171 | */ |
||
172 | protected function execAfterAction($action) |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param string $action |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | protected function execPreviousAction($action): bool |
||
213 | } |
||
214 | |||
215 | protected function getRolePermissions(string $pageName): array |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param string $viewName |
||
238 | * @param BaseView $view |
||
239 | */ |
||
240 | protected function loadData($viewName, $view) |
||
241 | { |
||
242 | $mvn = $this->getMainViewName(); |
||
243 | |||
244 | switch ($viewName) { |
||
245 | case 'docfiles': |
||
246 | $this->loadDataDocFiles($view, $this->getModelClassName(), $this->getModel()->primaryColumnValue()); |
||
247 | break; |
||
248 | |||
249 | case 'ListEmailSent': |
||
250 | $email = $this->getViewModelValue($mvn, 'email'); |
||
251 | if (empty($email)) { |
||
252 | $this->setSettings($viewName, 'active', false); |
||
253 | break; |
||
254 | } |
||
255 | |||
256 | $where = [new DataBaseWhere('addressee', $email)]; |
||
257 | $view->loadData('', $where); |
||
258 | |||
259 | // añadimos un botón para enviar un nuevo email |
||
260 | $this->addButton($viewName, [ |
||
261 | 'action' => 'SendMail?email=' . $email, |
||
262 | 'color' => 'success', |
||
263 | 'icon' => 'fas fa-envelope', |
||
264 | 'label' => 'send', |
||
265 | 'type' => 'link' |
||
266 | ]); |
||
267 | break; |
||
268 | |||
269 | case $mvn: |
||
270 | parent::loadData($viewName, $view); |
||
271 | $this->loadLanguageValues($viewName); |
||
272 | if (false === $view->model->exists()) { |
||
273 | break; |
||
274 | } |
||
275 | if ($this->permissions->allowUpdate) { |
||
276 | $this->addConversionButtons($viewName, $view); |
||
277 | } |
||
278 | $this->addButton($viewName, [ |
||
279 | 'action' => 'check-vies', |
||
280 | 'color' => 'info', |
||
281 | 'icon' => 'fas fa-check-double', |
||
282 | 'label' => 'check-vies' |
||
283 | ]); |
||
284 | break; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Load the available language values from translator. |
||
290 | */ |
||
291 | protected function loadLanguageValues(string $viewName) |
||
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param Contacto $contact |
||
306 | */ |
||
307 | protected function updateRelations($contact) |
||
325 | } |
||
326 | } |
||
328 |