Total Complexity | 57 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like EditCliente 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 EditCliente, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class EditCliente extends ComercialContactController |
||
40 | { |
||
41 | /** |
||
42 | * Returns the customer's risk on pending delivery notes. |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getDeliveryNotesRisk(): string |
||
47 | { |
||
48 | $codcliente = $this->getViewModelValue('EditCliente', 'codcliente'); |
||
49 | $total = empty($codcliente) ? 0 : CustomerRiskTools::getDeliveryNotesRisk($codcliente); |
||
50 | return Tools::money($total); |
||
51 | } |
||
52 | |||
53 | public function getImageUrl(): string |
||
54 | { |
||
55 | $mvn = $this->getMainViewName(); |
||
56 | return $this->views[$mvn]->model->gravatar(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns the customer's risk on unpaid invoices. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getInvoicesRisk(): string |
||
65 | { |
||
66 | $codcliente = $this->getViewModelValue('EditCliente', 'codcliente'); |
||
67 | $total = empty($codcliente) ? 0 : CustomerRiskTools::getInvoicesRisk($codcliente); |
||
68 | return Tools::money($total); |
||
69 | } |
||
70 | |||
71 | public function getModelClassName(): string |
||
72 | { |
||
73 | return 'Cliente'; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Returns the customer's risk on pending orders. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getOrdersRisk(): string |
||
82 | { |
||
83 | $codcliente = $this->getViewModelValue('EditCliente', 'codcliente'); |
||
84 | $total = empty($codcliente) ? 0 : CustomerRiskTools::getOrdersRisk($codcliente); |
||
85 | return Tools::money($total); |
||
86 | } |
||
87 | |||
88 | public function getPageData(): array |
||
95 | } |
||
96 | |||
97 | protected function createDocumentView(string $viewName, string $model, string $label) |
||
98 | { |
||
99 | $this->createCustomerListView($viewName, $model, $label); |
||
100 | |||
101 | // botones |
||
102 | $this->setSettings($viewName, 'btnPrint', true); |
||
103 | $this->addButtonGroupDocument($viewName); |
||
104 | $this->addButtonApproveDocument($viewName); |
||
105 | } |
||
106 | |||
107 | protected function createInvoiceView(string $viewName) |
||
108 | { |
||
109 | $this->createCustomerListView($viewName, 'FacturaCliente', 'invoices'); |
||
110 | |||
111 | // botones |
||
112 | $this->setSettings($viewName, 'btnPrint', true); |
||
113 | $this->addButtonLockInvoice($viewName); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Create views |
||
118 | */ |
||
119 | protected function createViews() |
||
120 | { |
||
121 | parent::createViews(); |
||
122 | $this->createContactsView(); |
||
123 | $this->addEditListView('EditCuentaBancoCliente', 'CuentaBancoCliente', 'customer-banking-accounts', 'fas fa-piggy-bank'); |
||
124 | |||
125 | if ($this->user->can('EditSubcuenta')) { |
||
126 | $this->createSubaccountsView(); |
||
127 | } |
||
128 | |||
129 | $this->createEmailsView(); |
||
130 | $this->createViewDocFiles(); |
||
131 | |||
132 | if ($this->user->can('EditFacturaCliente')) { |
||
133 | $this->createInvoiceView('ListFacturaCliente'); |
||
134 | $this->createLineView('ListLineaFacturaCliente', 'LineaFacturaCliente'); |
||
135 | } |
||
136 | if ($this->user->can('EditAlbaranCliente')) { |
||
137 | $this->createDocumentView('ListAlbaranCliente', 'AlbaranCliente', 'delivery-notes'); |
||
138 | } |
||
139 | if ($this->user->can('EditPedidoCliente')) { |
||
140 | $this->createDocumentView('ListPedidoCliente', 'PedidoCliente', 'orders'); |
||
141 | } |
||
142 | if ($this->user->can('EditPresupuestoCliente')) { |
||
143 | $this->createDocumentView('ListPresupuestoCliente', 'PresupuestoCliente', 'estimations'); |
||
144 | } |
||
145 | if ($this->user->can('EditReciboCliente')) { |
||
146 | $this->createReceiptView('ListReciboCliente', 'ReciboCliente'); |
||
147 | } |
||
148 | |||
149 | $this->createViewSettings(); |
||
150 | } |
||
151 | |||
152 | protected function createViewSettings(string $viewName = 'EditSettingsModel'): void |
||
153 | { |
||
154 | $this->addEditListView($viewName, 'SettingsModel', 'settings', 'fas fa-wrench'); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | protected function editAction() |
||
171 | } |
||
172 | |||
173 | protected function execPreviousAction($action) |
||
174 | { |
||
175 | if ($this->active === 'EditSettingsModel' && in_array($action, ['edit', 'insert'])){ |
||
176 | |||
177 | /** @var Cliente $cliente */ |
||
178 | $cliente = $this->getModel(); |
||
179 | $cliente->loadFromCode($this->request->get('code')); |
||
180 | |||
181 | /** @var SettingsModel $settingsModel */ |
||
182 | $settingsModel = $this->views[$this->active]->model; |
||
183 | $settingsModel->loadFromCode('', [ |
||
184 | new DataBaseWhere('classnamemodel', get_class($cliente)), |
||
185 | new DataBaseWhere('idmodel', $cliente->primaryColumnValue()), |
||
186 | new DataBaseWhere('idempresa', $this->request->request->get('idempresa')), |
||
187 | ]); |
||
188 | |||
189 | $settingsModel->classnamemodel = get_class($cliente); |
||
190 | $settingsModel->idmodel = $cliente->primaryColumnValue(); |
||
191 | $settingsModel->idempresa = $this->request->request->get('idempresa'); |
||
192 | $settingsModel->settings = json_encode([ |
||
193 | 'codalmacen' => empty($this->request->request->get('codalmacen')) ? null : $this->request->request->get('codalmacen'), |
||
194 | 'codserie' => empty($this->request->request->get('codserie')) ? null : $this->request->request->get('codserie'), |
||
195 | 'coddivisa' => empty($this->request->request->get('coddivisa')) ? null : $this->request->request->get('coddivisa'), |
||
196 | 'codpago' => empty($this->request->request->get('codpago')) ? null : $this->request->request->get('codpago'), |
||
197 | ]); |
||
198 | } |
||
199 | |||
200 | return parent::execPreviousAction($action); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | protected function insertAction() |
||
207 | { |
||
208 | if (false === parent::insertAction()) { |
||
209 | return false; |
||
210 | } |
||
211 | |||
212 | // redirect to returnUrl if return is defined |
||
213 | $returnUrl = $this->request->query->get('return'); |
||
214 | if (!empty($returnUrl)) { |
||
215 | $model = $this->views[$this->active]->model; |
||
216 | $this->redirect($returnUrl . '?' . $model->primaryColumn() . '=' . $model->primaryColumnValue()); |
||
217 | } |
||
218 | |||
219 | return true; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Load view data procedure |
||
224 | * |
||
225 | * @param string $viewName |
||
226 | * @param BaseView $view |
||
227 | */ |
||
228 | protected function loadData($viewName, $view) |
||
229 | { |
||
230 | $mainViewName = $this->getMainViewName(); |
||
231 | $codcliente = $this->getViewModelValue($mainViewName, 'codcliente'); |
||
232 | $where = [new DataBaseWhere('codcliente', $codcliente)]; |
||
233 | |||
234 | switch ($viewName) { |
||
235 | case 'EditCuentaBancoCliente': |
||
236 | $view->loadData('', $where, ['codcuenta' => 'DESC']); |
||
237 | break; |
||
238 | |||
239 | case 'EditDireccionContacto': |
||
240 | $view->loadData('', $where, ['idcontacto' => 'DESC']); |
||
241 | break; |
||
242 | |||
243 | case 'ListFacturaCliente': |
||
244 | $view->loadData('', $where); |
||
245 | $this->addButtonGenerateAccountingInvoices($viewName, $codcliente); |
||
246 | break; |
||
247 | |||
248 | case 'ListAlbaranCliente': |
||
249 | case 'ListPedidoCliente': |
||
250 | case 'ListPresupuestoCliente': |
||
251 | case 'ListReciboCliente': |
||
252 | $view->loadData('', $where); |
||
253 | break; |
||
254 | |||
255 | case 'ListLineaFacturaCliente': |
||
256 | $inSQL = 'SELECT idfactura FROM facturascli WHERE codcliente = ' . $this->dataBase->var2str($codcliente); |
||
257 | $where = [new DataBaseWhere('idfactura', $inSQL, 'IN')]; |
||
258 | $view->loadData('', $where); |
||
259 | break; |
||
260 | |||
261 | case 'EditSettingsModel': |
||
262 | $where = [ |
||
263 | new DataBaseWhere('classnamemodel', get_class($this->getModel())), |
||
264 | new DataBaseWhere('idmodel', $codcliente), |
||
265 | ]; |
||
266 | $view->loadData('', $where); |
||
267 | |||
268 | // agregamos al modelo las propiedades que hay dentro de settings |
||
269 | // para conservar la clase con todas las propiedades y que detecte si el modelo existe o no. |
||
270 | foreach($view->cursor as $model){ |
||
271 | foreach ($model->settings as $key => $value) { |
||
272 | $model->{$key} = $value; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | break; |
||
277 | |||
278 | case $mainViewName: |
||
279 | parent::loadData($viewName, $view); |
||
280 | $this->loadLanguageValues($viewName); |
||
281 | break; |
||
282 | |||
283 | default: |
||
284 | parent::loadData($viewName, $view); |
||
285 | break; |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Load the available language values from translator. |
||
291 | */ |
||
292 | protected function loadLanguageValues(string $viewName) |
||
293 | { |
||
294 | $columnLangCode = $this->views[$viewName]->columnForName('language'); |
||
295 | if ($columnLangCode && $columnLangCode->widget->getType() === 'select') { |
||
296 | $langs = []; |
||
297 | foreach (Tools::lang()->getAvailableLanguages() as $key => $value) { |
||
298 | $langs[] = ['value' => $key, 'title' => $value]; |
||
299 | } |
||
300 | |||
301 | $columnLangCode->widget->setValuesFromArray($langs, false, true); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | protected function setCustomWidgetValues(string $viewName) |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths