Customeradmin::handleCustomerAdmin()   F
last analyzed

Complexity

Conditions 19
Paths 280

Size

Total Lines 146

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 146
rs 2.1466
c 0
b 0
f 0
cc 19
nc 280
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller\Admin\Customer;
22
23
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Class Customeradmin
28
 * @package HaaseIT\HCSF\Controller\Admin\Customer
29
 */
30
class Customeradmin extends Base
31
{
32
    /**
33
     * @var \Doctrine\DBAL\Connection
34
     */
35
    private $dbal;
36
37
    /**
38
     * @var \HaaseIT\HCSF\HardcodedText
39
     */
40
    private $hardcodedtextcats;
41
42
    /**
43
     * Customeradmin constructor.
44
     * @param ServiceManager $serviceManager
45
     */
46
    public function __construct(ServiceManager $serviceManager)
47
    {
48
        parent::__construct($serviceManager);
49
        $this->dbal = $serviceManager->get('dbal');
50
        $this->hardcodedtextcats = $serviceManager->get('hardcodedtextcats');
51
    }
52
53
    /**
54
     *
55
     */
56
    public function preparePage()
57
    {
58
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager, [], 'admin/base.twig');
59
        $this->P->cb_pagetype = 'content';
60
        $this->P->cb_subnav = 'admin';
61
62
        $CUA = [
63
            ['title' => $this->hardcodedtextcats->get('customeradmin_list_no'), 'key' => 'cust_no', 'width' => '16%', 'linked' => false,'stylehead' => 'text-align: left;',],
64
            ['title' => $this->hardcodedtextcats->get('customeradmin_list_company'), 'key' => 'cust_corp', 'width' => '16%', 'linked' => false,'stylehead' => 'text-align: left;',],
65
            ['title' => $this->hardcodedtextcats->get('customeradmin_list_name'), 'key' => 'cust_name', 'width' => '16%', 'linked' => false,'stylehead' => 'text-align: left;',],
66
            ['title' => $this->hardcodedtextcats->get('customeradmin_list_town'), 'key' => 'cust_town', 'width' => '16%', 'linked' => false,'stylehead' => 'text-align: left;',],
67
            ['title' => $this->hardcodedtextcats->get('customeradmin_list_active'), 'key' => 'cust_active', 'width' => '16%', 'linked' => false,'stylehead' => 'text-align: left;',],
68
            [
69
                'title' => $this->hardcodedtextcats->get('customeradmin_list_edit'),
70
                'key' => 'cust_id',
71
                'width' => '16%',
72
                'linked' => true,
73
                'ltarget' => '/_admin/customeradmin.html',
74
                'lkeyname' => 'id',
75
                'lgetvars' => ['action' => 'edit',],
76
            ],
77
        ];
78
        $aPData = $this->handleCustomerAdmin($CUA, $this->serviceManager->get('twig'));
79
        $this->P->cb_customcontenttemplate = 'customer/customeradmin';
80
        $this->P->oPayload->cl_html = $aPData['customeradmin']['text'];
81
        $this->P->cb_customdata = $aPData;
82
    }
83
84
    /**
85
     * @param $CUA
86
     * @param $twig
87
     * @return mixed
88
     */
89
    private function handleCustomerAdmin($CUA, $twig)
90
    {
91
        $sType = 'all';
92
        $type = filter_input(INPUT_GET, 'type');
93
        if ($type !== null) {
94
            if ($type === 'active') {
95
                $sType = 'active';
96
            } elseif ($type === 'inactive') {
97
                $sType = 'inactive';
98
            }
99
        }
100
        $return = '';
101
        if (filter_input(INPUT_GET, 'action') === null) {
102
            $querybuilder = $this->dbal->createQueryBuilder();
103
            $querybuilder
104
                ->select(DB_ADDRESSFIELDS)
105
                ->from('customer')
106
                ->orderBy('cust_no', 'ASC')
107
            ;
108
109
            if ($sType === 'active') {
110
                $querybuilder
111
                    ->where('cust_active = ?')
112
                    ->setParameter(0, 'y')
113
                ;
114
            } elseif ($sType === 'inactive') {
115
                $querybuilder
116
                    ->where('cust_active = ?')
117
                    ->setParameter(0, 'n')
118
                ;
119
            }
120
            $stmt = $querybuilder->execute();
121
            if ($stmt->rowCount() !== 0) {
122
                $aData = $stmt->fetchAll();
123
                $return .= \HaaseIT\Toolbox\Tools::makeListtable($CUA, $aData, $twig);
124
            } else {
125
                $aInfo['nodatafound'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aInfo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aInfo = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
126
            }
127
        } elseif (filter_input(INPUT_GET, 'action') === 'edit') {
128
            $iId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
129
            $aErr = [];
130
            if (filter_input(INPUT_POST, 'doEdit') === 'yes') {
131
                $sCustno = trim(filter_input(INPUT_POST, 'custno', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW));
132
                if (strlen($sCustno) < $this->config->getCustomer('minimum_length_custno')) {
133
                    $aErr['custnoinvalid'] = true;
134
                } else {
135
                    $querybuilder = $this->dbal->createQueryBuilder();
136
                    $querybuilder
137
                        ->select(DB_ADDRESSFIELDS)
138
                        ->from('customer')
139
                        ->where('cust_id != ?')
140
                        ->andWhere('cust_no = ?')
141
                        ->setParameter(0, $iId)
142
                        ->setParameter(1, $sCustno)
143
                    ;
144
                    $stmt = $querybuilder->execute();
145
146
                    if ($stmt->rowCount() === 1) {
147
                        $aErr['custnoalreadytaken'] = true;
148
                    }
149
150
                    $querybuilder = $this->dbal->createQueryBuilder();
151
                    $querybuilder
152
                        ->select(DB_ADDRESSFIELDS)
153
                        ->from('customer')
154
                        ->where('cust_id != ?')
155
                        ->andWhere('cust_email = ?')
156
                        ->setParameter(0, $iId)
157
                        ->setParameter(1, filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL))
158
                    ;
159
                    $stmt = $querybuilder->execute();
160
                    if ($stmt->rowCount() === 1) {
161
                        $aErr['emailalreadytaken'] = true;
162
                    }
163
                    $aErr = $this->helperCustomer->validateCustomerForm($this->config->getLang(), $aErr, true);
164
                    if (count($aErr) === 0) {
165
                        $querybuilder = $this->dbal->createQueryBuilder();
166
                        $querybuilder
167
                            ->update('customer')
168
                            ->set('cust_no', ':cust_no')
169
                            ->set('cust_email', ':cust_email')
170
                            ->set('cust_corp', ':cust_corp')
171
                            ->set('cust_name', ':cust_name')
172
                            ->set('cust_street', ':cust_street')
173
                            ->set('cust_zip', ':cust_zip')
174
                            ->set('cust_town', ':cust_town')
175
                            ->set('cust_phone', ':cust_phone')
176
                            ->set('cust_cellphone', ':cust_cellphone')
177
                            ->set('cust_fax', ':cust_fax')
178
                            ->set('cust_country', ':cust_country')
179
                            ->set('cust_group', ':cust_group')
180
                            ->set('cust_emailverified', ':cust_emailverified')
181
                            ->set('cust_active', ':cust_active')
182
                            ->where('cust_id = :cust_id')
183
                            ->setParameter(':cust_no', $sCustno)
184
                            ->setParameter(':cust_email', trim(filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL)))
185
                            ->setParameter(':cust_corp', trim(filter_input(INPUT_POST, 'corpname', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
186
                            ->setParameter(':cust_name', trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
187
                            ->setParameter(':cust_street', trim(filter_input(INPUT_POST, 'street', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
188
                            ->setParameter(':cust_zip', trim(filter_input(INPUT_POST, 'zip', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
189
                            ->setParameter(':cust_town', trim(filter_input(INPUT_POST, 'town', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
190
                            ->setParameter(':cust_phone', trim(filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
191
                            ->setParameter(':cust_cellphone', trim(filter_input(INPUT_POST, 'cellphone', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
192
                            ->setParameter(':cust_fax', trim(filter_input(INPUT_POST, 'fax', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
193
                            ->setParameter(':cust_country', trim(filter_input(INPUT_POST, 'country', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
194
                            ->setParameter(':cust_group', trim(filter_input(INPUT_POST, 'custgroup', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW)))
195
                            ->setParameter(':cust_emailverified', (filter_input(INPUT_POST, 'emailverified') === 'y') ? 'y' : 'n')
196
                            ->setParameter(':cust_active', (filter_input(INPUT_POST, 'active') === 'y') ? 'y' : 'n')
197
                            ->setParameter(':cust_id', $iId)
198
                        ;
199
200
                        if (!empty(filter_input(INPUT_POST, 'pwd'))) {
201
                            $querybuilder
202
                                ->set('cust_password', ':cust_password')
203
                                ->setParameter(':cust_password', password_hash(filter_input(INPUT_POST, 'pwd'), PASSWORD_DEFAULT))
204
                            ;
205
                            $aInfo['passwordchanged'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aInfo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aInfo = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
206
                        }
207
208
                        $querybuilder->execute();
209
                        $aInfo['changeswritten'] = true;
0 ignored issues
show
Bug introduced by
The variable $aInfo does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
210
                    }
211
                }
212
            }
213
            $querybuilder = $this->dbal->createQueryBuilder();
214
            $querybuilder
215
                ->select(DB_ADDRESSFIELDS)
216
                ->from('customer')
217
                ->where('cust_id = ?')
218
                ->setParameter(0, $iId)
219
            ;
220
            $stmt = $querybuilder->execute();
221
            if ($stmt->rowCount() === 1) {
222
                $aUser = $stmt->fetch();
223
                $aPData['customerform'] = $this->helperCustomer->buildCustomerForm($this->config->getLang(), 'admin', $aErr, $aUser);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aPData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aPData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
224
            } else {
225
                $aInfo['nosuchuserfound'] = true;
226
            }
227
        }
228
        $aPData['customeradmin']['text'] = $return;
0 ignored issues
show
Bug introduced by
The variable $aPData does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
229
        $aPData['customeradmin']['type'] = $sType;
230
        if (isset($aInfo)) {
231
            $aPData['customeradmin']['info'] = $aInfo;
232
        }
233
        return $aPData;
234
    }
235
236
}
237