Completed
Push — support-coverage ( 9b57a3...b5dae6 )
by Kentaro
41:38
created

CustomerEditController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 6.62 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 9
loc 136
ccs 65
cts 70
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C index() 9 110 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Eccube\Controller\AbstractController;
27
use Eccube\Entity\CustomerAddress;
28
use Eccube\Event\EccubeEvents;
29
use Eccube\Event\EventArgs;
30
use Eccube\Form\Type\Admin\CustomerType;
31
use Eccube\Repository\CustomerRepository;
32
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
33
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
37
38
/**
39
 * @Route(service=CustomerEditController::class)
40
 */
41
class CustomerEditController extends AbstractController
42
{
43
    /**
44
     * @var CustomerRepository
45
     */
46
    protected $customerRepository;
47
48
    /**
49
     * @var EncoderFactoryInterface
50
     */
51
    protected $encoderFactory;
52
53 6
    public function __construct(
54
        CustomerRepository $customerRepository,
55
        EncoderFactoryInterface $encoderFactory
56
    ) {
57 6
        $this->customerRepository = $customerRepository;
58 6
        $this->encoderFactory = $encoderFactory;
59
    }
60
61
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
62
     * @Route("/%eccube_admin_route%/customer/new", name="admin_customer_new")
63
     * @Route("/%eccube_admin_route%/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit")
64
     * @Template("@admin/Customer/edit.twig")
65
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
66 6
    public function index(Request $request, $id = null)
67
    {
68
        //$this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
        // 編集
70 6
        if ($id) {
71 4
            $Customer = $this->customerRepository
72 4
                ->find($id);
73
74 4
            if (is_null($Customer)) {
75
                throw new NotFoundHttpException();
76
            }
77
            // 編集用にデフォルトパスワードをセット
78 4
            $previous_password = $Customer->getPassword();
79 4
            $Customer->setPassword($this->eccubeConfig['eccube_default_password']);
80
        // 新規登録
81
        } else {
82 2
            $Customer = $this->customerRepository->newCustomer();
83 2
            $CustomerAddress = new CustomerAddress();
84 2
            $Customer->setBuyTimes(0);
85 2
            $Customer->setBuyTotal(0);
86
        }
87
88
        // 会員登録フォーム
89 6
        $builder = $this->formFactory
90 6
            ->createBuilder(CustomerType::class, $Customer);
91
92 6
        $event = new EventArgs(
93
            [
94 6
                'builder' => $builder,
95 6
                'Customer' => $Customer,
96
            ],
97 6
            $request
98
        );
99 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
100
101 6
        $form = $builder->getForm();
102
103 6
        $form->handleRequest($request);
104
105 6
        if ($form->isSubmitted()) {
106 2
            if ($form->isValid()) {
107 2
                log_info('会員登録開始', [$Customer->getId()]);
108
109 2
                $encoder = $this->encoderFactory->getEncoder($Customer);
110
111 2
                if ($Customer->getId() === null) {
112 1
                    $Customer->setSalt($encoder->createSalt());
113 1
                    $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
114
115 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...
116 1
                        ->setName02($Customer->getName02())
117 1
                        ->setKana01($Customer->getKana01())
118 1
                        ->setKana02($Customer->getKana02())
119 1
                        ->setCompanyName($Customer->getCompanyName())
120 1
                        ->setZip01($Customer->getZip01())
121 1
                        ->setZip02($Customer->getZip02())
122 1
                        ->setZipcode($Customer->getZip01().$Customer->getZip02())
123 1
                        ->setPref($Customer->getPref())
124 1
                        ->setAddr01($Customer->getAddr01())
125 1
                        ->setAddr02($Customer->getAddr02())
126 1
                        ->setTel01($Customer->getTel01())
127 1
                        ->setTel02($Customer->getTel02())
128 1
                        ->setTel03($Customer->getTel03())
129 1
                        ->setFax01($Customer->getFax01())
130 1
                        ->setFax02($Customer->getFax02())
131 1
                        ->setFax03($Customer->getFax03())
132 1
                        ->setCustomer($Customer);
133
134 1
                    $this->entityManager->persist($CustomerAddress);
135
                }
136
137 2 View Code Duplication
                if ($Customer->getPassword() === $this->eccubeConfig['eccube_default_password']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
                    $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...
139
                } else {
140 2
                    if ($Customer->getSalt() === null) {
141
                        $Customer->setSalt($encoder->createSalt());
142
                        $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
143
                    }
144 2
                    $Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt()));
145
                }
146
147 2
                $this->entityManager->persist($Customer);
148 2
                $this->entityManager->flush();
149
150 2
                log_info('会員登録完了', [$Customer->getId()]);
151
152 2
                $event = new EventArgs(
153
                    [
154 2
                        'form' => $form,
155 2
                        'Customer' => $Customer,
156
                    ],
157 2
                    $request
158
                );
159 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
160
161 2
                $this->addSuccess('admin.customer.save.complete', 'admin');
162
163 2
                return $this->redirectToRoute('admin_customer_edit', [
164 2
                    'id' => $Customer->getId(),
165
                ]);
166
            } else {
167
                $this->addError('admin.customer.save.failed', 'admin');
168
            }
169
        }
170
171
        return [
172 4
            'form' => $form->createView(),
173 4
            'Customer' => $Customer,
174
        ];
175
    }
176
}
177