Failed Conditions
Pull Request — experimental/3.1 (#2512)
by chihiro
12:14
created

CustomerEditController::index()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 112
Code Lines 75

Duplication

Lines 10
Ratio 8.93 %

Code Coverage

Tests 65
CRAP Score 8.0124

Importance

Changes 0
Metric Value
cc 8
eloc 75
nc 17
nop 3
dl 10
loc 112
ccs 65
cts 69
cp 0.942
crap 8.0124
rs 5.2676
c 0
b 0
f 0

How to fix   Long Method   

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
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Controller\Admin\Customer;
25
26
use Doctrine\ORM\EntityManager;
27
use Eccube\Annotation\Inject;
28
use Eccube\Annotation\Component;
29
use Eccube\Application;
30
use Eccube\Common\Constant;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Entity\CustomerAddress;
33
use Eccube\Event\EccubeEvents;
34
use Eccube\Event\EventArgs;
35
use Eccube\Form\Type\Admin\CustomerType;
36
use Eccube\Repository\CustomerRepository;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
40
use Symfony\Component\EventDispatcher\EventDispatcher;
41
use Symfony\Component\Form\FormFactory;
42
use Symfony\Component\HttpFoundation\Request;
43
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
44
45
/**
46
 * @Component
47
 * @Route(service=CustomerEditController::class)
48
 */
49
class CustomerEditController extends AbstractController
50
{
51
    /**
52
     * @Inject("eccube.event.dispatcher")
53
     * @var EventDispatcher
54
     */
55
    protected $eventDispatcher;
56
57
    /**
58
     * @Inject("form.factory")
59
     * @var FormFactory
60
     */
61
    protected $formFactory;
62
63
    /**
64
     * @Inject("config")
65
     * @var array
66
     */
67
    protected $appConfig;
68
69
    /**
70
     * @Inject("orm.em")
71
     * @var EntityManager
72
     */
73
    protected $entityManager;
74
75
    /**
76
     * @Inject(CustomerRepository::class)
77
     * @var CustomerRepository
78
     */
79
    protected $customerRepository;
80
81
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
82
     * @Route("/{_admin}/customer/new", name="admin_customer_new")
83
     * @Route("/{_admin}/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit")
84
     * @Template("Customer/edit.twig")
85
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
86 6
    public function index(Application $app, Request $request, $id = null)
87
    {
88 6
        $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
89
        // 編集
90 6
        if ($id) {
91 4
            $Customer = $this->customerRepository
92 4
                ->find($id);
93
94 4
            if (is_null($Customer)) {
95
                throw new NotFoundHttpException();
96
            }
97
            // 編集用にデフォルトパスワードをセット
98 4
            $previous_password = $Customer->getPassword();
99 4
            $Customer->setPassword($this->appConfig['default_password']);
100
            // 新規登録
101
        } else {
102 2
            $Customer = $this->customerRepository->newCustomer();
103 2
            $CustomerAddress = new CustomerAddress();
104 2
            $Customer->setBuyTimes(0);
105 2
            $Customer->setBuyTotal(0);
106
        }
107
108
        // 会員登録フォーム
109 6
        $builder = $this->formFactory
110 6
            ->createBuilder(CustomerType::class, $Customer);
111
112 6
        $event = new EventArgs(
113
            array(
114 6
                'builder' => $builder,
115 6
                'Customer' => $Customer,
116
            ),
117 6
            $request
118
        );
119 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
120
121 6
        $form = $builder->getForm();
122
123 6
        if ('POST' === $request->getMethod()) {
124 2
            $form->handleRequest($request);
125 2
            if ($form->isValid()) {
126 2
                log_info('会員登録開始', array($Customer->getId()));
127
128 2
                if ($Customer->getId() === null) {
129 1
                    $Customer->setSalt(
130 1
                        $this->customerRepository->createSalt(5)
131
                    );
132 1
                    $Customer->setSecretKey(
133 1
                        $this->customerRepository->getUniqueSecretKey($app)
134
                    );
135
136 1
                    $CustomerAddress->setName01($Customer->getName01())
0 ignored issues
show
Bug introduced by
The variable $CustomerAddress 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...
137 1
                        ->setName02($Customer->getName02())
138 1
                        ->setKana01($Customer->getKana01())
139 1
                        ->setKana02($Customer->getKana02())
140 1
                        ->setCompanyName($Customer->getCompanyName())
141 1
                        ->setZip01($Customer->getZip01())
142 1
                        ->setZip02($Customer->getZip02())
143 1
                        ->setZipcode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
144 1
                        ->setPref($Customer->getPref())
145 1
                        ->setAddr01($Customer->getAddr01())
146 1
                        ->setAddr02($Customer->getAddr02())
147 1
                        ->setTel01($Customer->getTel01())
148 1
                        ->setTel02($Customer->getTel02())
149 1
                        ->setTel03($Customer->getTel03())
150 1
                        ->setFax01($Customer->getFax01())
151 1
                        ->setFax02($Customer->getFax02())
152 1
                        ->setFax03($Customer->getFax03())
153 1
                        ->setCustomer($Customer);
154
155 1
                    $this->entityManager->persist($CustomerAddress);
156
                }
157
158 2 View Code Duplication
                if ($Customer->getPassword() === $this->appConfig['default_password']) {
159
                    $Customer->setPassword($previous_password);
0 ignored issues
show
Bug introduced by
The variable $previous_password 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...
160
                } else {
161 2
                    if ($Customer->getSalt() === null) {
162
                        $Customer->setSalt($this->customerRepository->createSalt(5));
163
                    }
164 2
                    $Customer->setPassword(
165 2
                        $this->customerRepository->encryptPassword($app, $Customer)
166
                    );
167
                }
168
169 2
                $this->entityManager->persist($Customer);
170 2
                $this->entityManager->flush();
171
172 2
                log_info('会員登録完了', array($Customer->getId()));
173
174 2
                $event = new EventArgs(
175
                    array(
176 2
                        'form' => $form,
177 2
                        'Customer' => $Customer,
178
                    ),
179 2
                    $request
180
                );
181 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
182
183 2
                $app->addSuccess('admin.customer.save.complete', 'admin');
184
185 2
                return $app->redirect($app->url('admin_customer_edit', array(
186 2
                    'id' => $Customer->getId(),
187
                )));
188
            } else {
189
                $app->addError('admin.customer.save.failed', 'admin');
190
            }
191
        }
192
193
        return [
194 4
            'form' => $form->createView(),
195 4
            'Customer' => $Customer,
196
        ];
197
    }
198
}
199