Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

CustomerEditController::index()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 108
Code Lines 73

Duplication

Lines 8
Ratio 7.41 %

Code Coverage

Tests 63
CRAP Score 8.0136

Importance

Changes 0
Metric Value
cc 8
eloc 73
nc 17
nop 3
dl 8
loc 108
ccs 63
cts 67
cp 0.9403
crap 8.0136
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
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
45
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
46
47
/**
48
 * @Component
49
 * @Route(service=CustomerEditController::class)
50
 */
51
class CustomerEditController extends AbstractController
52
{
53
    /**
54
     * @Inject("eccube.event.dispatcher")
55
     * @var EventDispatcher
56
     */
57
    protected $eventDispatcher;
58
59
    /**
60
     * @Inject("form.factory")
61
     * @var FormFactory
62
     */
63
    protected $formFactory;
64
65
    /**
66
     * @Inject("config")
67
     * @var array
68
     */
69
    protected $appConfig;
70
71
    /**
72
     * @Inject("orm.em")
73
     * @var EntityManager
74
     */
75
    protected $entityManager;
76
77
    /**
78
     * @Inject(CustomerRepository::class)
79
     * @var CustomerRepository
80
     */
81
    protected $customerRepository;
82
83
    /**
84
     * @Inject("security.encoder_factory")
85
     * @var EncoderFactoryInterface
86
     */
87
    protected $encoderFactory;
88
89
    /**
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...
90
     * @Route("/{_admin}/customer/new", name="admin_customer_new")
91
     * @Route("/{_admin}/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit")
92
     * @Template("Customer/edit.twig")
93
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
94 6
    public function index(Application $app, Request $request, $id = null)
95
    {
96 6
        $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
97
        // 編集
98 6
        if ($id) {
99 4
            $Customer = $this->customerRepository
100 4
                ->find($id);
101
102 4
            if (is_null($Customer)) {
103
                throw new NotFoundHttpException();
104
            }
105
            // 編集用にデフォルトパスワードをセット
106 4
            $previous_password = $Customer->getPassword();
107 4
            $Customer->setPassword($this->appConfig['default_password']);
108
            // 新規登録
109
        } else {
110 2
            $Customer = $this->customerRepository->newCustomer();
111 2
            $CustomerAddress = new CustomerAddress();
112 2
            $Customer->setBuyTimes(0);
113 2
            $Customer->setBuyTotal(0);
114
        }
115
116
        // 会員登録フォーム
117 6
        $builder = $this->formFactory
118 6
            ->createBuilder(CustomerType::class, $Customer);
119
120 6
        $event = new EventArgs(
121
            array(
122 6
                'builder' => $builder,
123 6
                'Customer' => $Customer,
124
            ),
125 6
            $request
126
        );
127 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
128
129 6
        $form = $builder->getForm();
130
131 6
        if ('POST' === $request->getMethod()) {
132 2
            $form->handleRequest($request);
133 2
            if ($form->isValid()) {
134 2
                log_info('会員登録開始', array($Customer->getId()));
135
136 2
                $encoder = $this->encoderFactory->getEncoder($Customer);
137
138 2
                if ($Customer->getId() === null) {
139 1
                    $Customer->setSalt($encoder->createSalt());
140 1
                    $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
141
142 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...
143 1
                        ->setName02($Customer->getName02())
144 1
                        ->setKana01($Customer->getKana01())
145 1
                        ->setKana02($Customer->getKana02())
146 1
                        ->setCompanyName($Customer->getCompanyName())
147 1
                        ->setZip01($Customer->getZip01())
148 1
                        ->setZip02($Customer->getZip02())
149 1
                        ->setZipcode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
150 1
                        ->setPref($Customer->getPref())
151 1
                        ->setAddr01($Customer->getAddr01())
152 1
                        ->setAddr02($Customer->getAddr02())
153 1
                        ->setTel01($Customer->getTel01())
154 1
                        ->setTel02($Customer->getTel02())
155 1
                        ->setTel03($Customer->getTel03())
156 1
                        ->setFax01($Customer->getFax01())
157 1
                        ->setFax02($Customer->getFax02())
158 1
                        ->setFax03($Customer->getFax03())
159 1
                        ->setCustomer($Customer);
160
161 1
                    $this->entityManager->persist($CustomerAddress);
162
                }
163
164 2 View Code Duplication
                if ($Customer->getPassword() === $this->appConfig['default_password']) {
165
                    $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...
166
                } else {
167 2
                    if ($Customer->getSalt() === null) {
168
                        $Customer->setSalt($encoder->createSalt());
169
                    }
170 2
                    $Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt()));
171
                }
172
173 2
                $this->entityManager->persist($Customer);
174 2
                $this->entityManager->flush();
175
176 2
                log_info('会員登録完了', array($Customer->getId()));
177
178 2
                $event = new EventArgs(
179
                    array(
180 2
                        'form' => $form,
181 2
                        'Customer' => $Customer,
182
                    ),
183 2
                    $request
184
                );
185 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
186
187 2
                $app->addSuccess('admin.customer.save.complete', 'admin');
188
189 2
                return $app->redirect($app->url('admin_customer_edit', array(
190 2
                    'id' => $Customer->getId(),
191
                )));
192
            } else {
193
                $app->addError('admin.customer.save.failed', 'admin');
194
            }
195
        }
196
197
        return [
198 4
            'form' => $form->createView(),
199 4
            'Customer' => $Customer,
200
        ];
201
    }
202
}
203