EditContacto   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 290
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 148
dl 0
loc 290
rs 7.92
c 0
b 0
f 0
wmc 51

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelClassName() 0 3 1
A addConversionButtons() 0 19 5
A getPageData() 0 7 1
A getImageUrl() 0 4 1
A editAction() 0 8 3
A createCustomerAction() 0 17 3
A createViews() 0 5 1
A execAfterAction() 0 13 3
A execPreviousAction() 0 20 6
A createSupplierAction() 0 17 3
A createEmailsView() 0 11 1
A getRolePermissions() 0 19 5
A checkViesAction() 0 9 2
A loadLanguageValues() 0 10 4
A updateRelations() 0 18 5
B loadData() 0 45 7

How to fix   Complexity   

Complex Class

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
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2018-2023 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScripts\Core\Controller;
21
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
24
use FacturaScripts\Core\Lib\ExtendedController\DocFilesTrait;
25
use FacturaScripts\Core\Lib\ExtendedController\EditController;
26
use FacturaScripts\Core\Tools;
27
use FacturaScripts\Dinamic\Model\Contacto;
28
use FacturaScripts\Dinamic\Model\RoleAccess;
29
30
/**
31
 * Controller to edit a single item from the Contacto model
32
 *
33
 * @author Carlos García Gómez <[email protected]>
34
 */
35
class EditContacto extends EditController
36
{
37
    use DocFilesTrait;
0 ignored issues
show
introduced by
The trait FacturaScripts\Core\Lib\...ontroller\DocFilesTrait requires some properties which are not provided by FacturaScripts\Core\Controller\EditContacto: $allowUpdate, $logLevels, $query, $files, $allowDelete
Loading history...
38
39
    public function getImageUrl(): string
40
    {
41
        $mvn = $this->getMainViewName();
42
        return $this->views[$mvn]->model->gravatar();
0 ignored issues
show
Bug introduced by
The method gravatar() does not exist on FacturaScripts\Core\Model\Base\ModelClass. It seems like you code against a sub-type of said class. However, the method does not exist in FacturaScripts\Core\Model\Base\Address or FacturaScripts\Dinamic\Model\Base\ModelClass or FacturaScripts\Core\Model\Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BankAccount or FacturaScripts\Core\Model\Base\Payment or FacturaScripts\Dinamic\Model\Base\Address or FacturaScripts\Core\Model\Base\Receipt or FacturaScripts\Core\Mode...se\BusinessDocumentLine or FacturaScripts\Dinamic\M...Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BusinessDocument or FacturaScripts\Dinamic\Model\Base\Receipt or FacturaScripts\Dinamic\M...se\BusinessDocumentLine or FacturaScripts\Core\Mode...se\PurchaseDocumentLine or FacturaScripts\Core\Model\Base\SalesDocumentLine or FacturaScripts\Dinamic\M...se\PurchaseDocumentLine or FacturaScripts\Dinamic\M...\Base\SalesDocumentLine or FacturaScripts\Dinamic\Model\Base\BusinessDocument or FacturaScripts\Core\Model\Base\TransformerDocument or FacturaScripts\Core\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\M...ase\TransformerDocument or FacturaScripts\Core\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\BankAccount or FacturaScripts\Dinamic\Model\Base\Payment. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        return $this->views[$mvn]->model->/** @scrutinizer ignore-call */ gravatar();
Loading history...
43
    }
44
45
    public function getModelClassName(): string
46
    {
47
        return 'Contacto';
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)
60
    {
61
        $accessClient = $this->getRolePermissions('EditCliente');
62
        if (empty($view->model->codcliente) && $accessClient['allowupdate']) {
63
            $this->addButton($viewName, [
64
                'action' => 'convert-into-customer',
65
                'color' => 'success',
66
                'icon' => 'fas fa-user-check',
67
                'label' => 'convert-into-customer'
68
            ]);
69
        }
70
71
        $accessSupplier = $this->getRolePermissions('EditProveedor');
72
        if (empty($view->model->codproveedor) && $accessSupplier['allowupdate']) {
73
            $this->addButton($viewName, [
74
                'action' => 'convert-into-supplier',
75
                'color' => 'success',
76
                'icon' => 'fas fa-user-cog',
77
                'label' => 'convert-into-supplier'
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();
0 ignored issues
show
Bug introduced by
The method checkVies() does not exist on FacturaScripts\Core\Model\Base\ModelClass. It seems like you code against a sub-type of said class. However, the method does not exist in FacturaScripts\Core\Model\Base\Address or FacturaScripts\Dinamic\Model\Base\ModelClass or FacturaScripts\Core\Model\Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BankAccount or FacturaScripts\Core\Model\Base\Payment or FacturaScripts\Dinamic\Model\Base\Address or FacturaScripts\Core\Model\Base\Receipt or FacturaScripts\Core\Mode...se\BusinessDocumentLine or FacturaScripts\Dinamic\M...Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BusinessDocument or FacturaScripts\Dinamic\Model\Base\Receipt or FacturaScripts\Dinamic\M...se\BusinessDocumentLine or FacturaScripts\Core\Mode...se\PurchaseDocumentLine or FacturaScripts\Core\Model\Base\SalesDocumentLine or FacturaScripts\Dinamic\M...se\PurchaseDocumentLine or FacturaScripts\Dinamic\M...\Base\SalesDocumentLine or FacturaScripts\Dinamic\Model\Base\BusinessDocument or FacturaScripts\Core\Model\Base\TransformerDocument or FacturaScripts\Core\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\M...ase\TransformerDocument or FacturaScripts\Core\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\BankAccount or FacturaScripts\Dinamic\Model\Base\Payment. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        $model->/** @scrutinizer ignore-call */ 
90
                checkVies();
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method getCustomer() does not exist on FacturaScripts\Core\Model\Base\ModelClass. It seems like you code against a sub-type of said class. However, the method does not exist in FacturaScripts\Core\Model\Base\Contact or FacturaScripts\Core\Model\Base\Address or FacturaScripts\Dinamic\Model\Base\ModelClass or FacturaScripts\Core\Model\Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BankAccount or FacturaScripts\Core\Model\Base\Payment or FacturaScripts\Dinamic\Model\Base\Contact or FacturaScripts\Core\Model\Base\ComercialContact or FacturaScripts\Dinamic\Model\Base\ComercialContact or FacturaScripts\Dinamic\Model\Base\Address or FacturaScripts\Core\Model\Base\Receipt or FacturaScripts\Core\Mode...se\BusinessDocumentLine or FacturaScripts\Dinamic\M...Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BusinessDocument or FacturaScripts\Dinamic\Model\Base\Receipt or FacturaScripts\Dinamic\M...se\BusinessDocumentLine or FacturaScripts\Core\Mode...se\PurchaseDocumentLine or FacturaScripts\Core\Model\Base\SalesDocumentLine or FacturaScripts\Dinamic\M...se\PurchaseDocumentLine or FacturaScripts\Dinamic\M...\Base\SalesDocumentLine or FacturaScripts\Dinamic\Model\Base\BusinessDocument or FacturaScripts\Core\Model\Base\TransformerDocument or FacturaScripts\Core\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\M...ase\TransformerDocument or FacturaScripts\Core\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\BankAccount or FacturaScripts\Dinamic\Model\Base\Payment. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        /** @scrutinizer ignore-call */ 
103
        $customer = $this->views[$mvn]->model->getCustomer();
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method addOrderBy() does not exist on FacturaScripts\Core\Lib\...ndedController\BaseView. It seems like you code against a sub-type of FacturaScripts\Core\Lib\...ndedController\BaseView such as FacturaScripts\Core\Lib\...ndedController\ListView. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        $this->views[$viewName]->/** @scrutinizer ignore-call */ 
116
                                 addOrderBy(['date'], 'date', 2);
Loading history...
116
        $this->views[$viewName]->addSearchFields(['addressee', 'body', 'subject']);
0 ignored issues
show
Bug introduced by
The method addSearchFields() does not exist on FacturaScripts\Core\Lib\...ndedController\BaseView. It seems like you code against a sub-type of FacturaScripts\Core\Lib\...ndedController\BaseView such as FacturaScripts\Core\Lib\...ndedController\ListView. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        $this->views[$viewName]->/** @scrutinizer ignore-call */ 
117
                                 addSearchFields(['addressee', 'body', 'subject']);
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method getSupplier() does not exist on FacturaScripts\Core\Model\Base\ModelClass. It seems like you code against a sub-type of said class. However, the method does not exist in FacturaScripts\Core\Model\Base\Contact or FacturaScripts\Core\Model\Base\Address or FacturaScripts\Dinamic\Model\Base\ModelClass or FacturaScripts\Core\Model\Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BankAccount or FacturaScripts\Core\Model\Base\Payment or FacturaScripts\Dinamic\Model\Base\Contact or FacturaScripts\Core\Model\Base\ComercialContact or FacturaScripts\Dinamic\Model\Base\ComercialContact or FacturaScripts\Dinamic\Model\Base\Address or FacturaScripts\Core\Model\Base\Receipt or FacturaScripts\Core\Mode...se\BusinessDocumentLine or FacturaScripts\Dinamic\M...Base\ModelOnChangeClass or FacturaScripts\Core\Model\Base\BusinessDocument or FacturaScripts\Dinamic\Model\Base\Receipt or FacturaScripts\Dinamic\M...se\BusinessDocumentLine or FacturaScripts\Core\Mode...se\PurchaseDocumentLine or FacturaScripts\Core\Model\Base\SalesDocumentLine or FacturaScripts\Dinamic\M...se\PurchaseDocumentLine or FacturaScripts\Dinamic\M...\Base\SalesDocumentLine or FacturaScripts\Dinamic\Model\Base\BusinessDocument or FacturaScripts\Core\Model\Base\TransformerDocument or FacturaScripts\Core\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\M...ase\TransformerDocument or FacturaScripts\Core\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\PurchaseDocument or FacturaScripts\Dinamic\Model\Base\SalesDocument or FacturaScripts\Dinamic\Model\Base\BankAccount or FacturaScripts\Dinamic\Model\Base\Payment. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
        /** @scrutinizer ignore-call */ 
135
        $supplier = $this->views[$mvn]->model->getSupplier();
Loading history...
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()
158
    {
159
        $return = parent::editAction();
160
        if ($return && $this->active === $this->getMainViewName()) {
161
            $this->updateRelations($this->views[$this->active]->model);
162
        }
163
164
        return $return;
165
    }
166
167
    /**
168
     * Run the controller after actions
169
     *
170
     * @param string $action
171
     */
172
    protected function execAfterAction($action)
173
    {
174
        switch ($action) {
175
            case 'convert-into-customer':
176
                $this->createCustomerAction();
177
                break;
178
179
            case 'convert-into-supplier':
180
                $this->createSupplierAction();
181
                break;
182
183
            default:
184
                parent::execAfterAction($action);
185
        }
186
    }
187
188
    /**
189
     * @param string $action
190
     *
191
     * @return bool
192
     */
193
    protected function execPreviousAction($action): bool
194
    {
195
        switch ($action) {
196
            case 'add-file':
197
                return $this->addFileAction();
198
199
            case 'check-vies':
200
                return $this->checkViesAction();
201
202
            case 'delete-file':
203
                return $this->deleteFileAction();
204
205
            case 'edit-file':
206
                return $this->editFileAction();
207
208
            case 'unlink-file':
209
                return $this->unlinkFileAction();
210
        }
211
212
        return parent::execPreviousAction($action);
213
    }
214
215
    protected function getRolePermissions(string $pageName): array
216
    {
217
        $access = [
218
            'allowdelete' => $this->user->admin,
219
            'allowupdate' => $this->user->admin,
220
            'onlyownerdata' => $this->user->admin
221
        ];
222
        foreach (RoleAccess::allFromUser($this->user->nick, $pageName) as $rolesPageUser) {
223
            if ($rolesPageUser->allowdelete) {
224
                $access['allowdelete'] = true;
225
            }
226
            if ($rolesPageUser->allowupdate) {
227
                $access['allowupdate'] = true;
228
            }
229
            if ($rolesPageUser->onlyownerdata) {
230
                $access['onlyownerdata'] = true;
231
            }
232
        }
233
        return $access;
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)
292
    {
293
        $columnLangCode = $this->views[$viewName]->columnForName('language');
294
        if ($columnLangCode && $columnLangCode->widget->getType() === 'select') {
295
            $langs = [];
296
            foreach (Tools::lang()->getAvailableLanguages() as $key => $value) {
297
                $langs[] = ['value' => $key, 'title' => $value];
298
            }
299
300
            $columnLangCode->widget->setValuesFromArray($langs, false, true);
0 ignored issues
show
Bug introduced by
The method setValuesFromArray() does not exist on FacturaScripts\Core\Lib\Widget\BaseWidget. Did you maybe mean setValue()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

300
            $columnLangCode->widget->/** @scrutinizer ignore-call */ 
301
                                     setValuesFromArray($langs, false, true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
301
        }
302
    }
303
304
    /**
305
     * @param Contacto $contact
306
     */
307
    protected function updateRelations($contact)
308
    {
309
        $customer = $contact->getCustomer(false);
310
        if ($customer->idcontactofact == $contact->idcontacto && $customer->exists()) {
311
            $customer->email = $contact->email;
312
            $customer->fax = $contact->fax;
313
            $customer->telefono1 = $contact->telefono1;
314
            $customer->telefono2 = $contact->telefono2;
315
            $customer->save();
316
        }
317
318
        $supplier = $contact->getSupplier(false);
319
        if ($supplier->idcontacto == $contact->idcontacto && $supplier->exists()) {
320
            $supplier->email = $contact->email;
321
            $supplier->fax = $contact->fax;
322
            $supplier->telefono1 = $contact->telefono1;
323
            $supplier->telefono2 = $contact->telefono2;
324
            $supplier->save();
325
        }
326
    }
327
}
328