Completed
Push — experimental/sf ( 833c28...c9c06d )
by Ryo
45:01 queued 38:40
created

CustomerEditController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 8.26 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 9
loc 109
ccs 45
cts 48
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B index() 9 83 7

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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Customer;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Event\EccubeEvents;
18
use Eccube\Event\EventArgs;
19
use Eccube\Form\Type\Admin\CustomerType;
20
use Eccube\Repository\CustomerRepository;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
26
27
class CustomerEditController extends AbstractController
28
{
29
    /**
30
     * @var CustomerRepository
31
     */
32
    protected $customerRepository;
33
34
    /**
35
     * @var EncoderFactoryInterface
36
     */
37
    protected $encoderFactory;
38
39 7
    public function __construct(
40
        CustomerRepository $customerRepository,
41
        EncoderFactoryInterface $encoderFactory
42
    ) {
43 7
        $this->customerRepository = $customerRepository;
44 7
        $this->encoderFactory = $encoderFactory;
45
    }
46
47
    /**
48
     * @Route("/%eccube_admin_route%/customer/new", name="admin_customer_new")
49
     * @Route("/%eccube_admin_route%/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit")
50
     * @Template("@admin/Customer/edit.twig")
51
     */
52 7
    public function index(Request $request, $id = null)
53
    {
54 7
        $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
55
        // 編集
56 7
        if ($id) {
57 5
            $Customer = $this->customerRepository
58 5
                ->find($id);
59
60 5
            if (is_null($Customer)) {
61
                throw new NotFoundHttpException();
62
            }
63
            // 編集用にデフォルトパスワードをセット
64 5
            $previous_password = $Customer->getPassword();
65 5
            $Customer->setPassword($this->eccubeConfig['eccube_default_password']);
66
        // 新規登録
67
        } else {
68 2
            $Customer = $this->customerRepository->newCustomer();
69 2
            $Customer->setBuyTimes(0);
70 2
            $Customer->setBuyTotal(0);
71
        }
72
73
        // 会員登録フォーム
74 7
        $builder = $this->formFactory
75 7
            ->createBuilder(CustomerType::class, $Customer);
76
77 7
        $event = new EventArgs(
78
            [
79 7
                'builder' => $builder,
80 7
                'Customer' => $Customer,
81
            ],
82 7
            $request
83
        );
84 7
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
85
86 7
        $form = $builder->getForm();
87
88 7
        $form->handleRequest($request);
89
90 7
        if ($form->isSubmitted()) {
91 2
            if ($form->isValid()) {
92 2
                log_info('会員登録開始', [$Customer->getId()]);
93
94 2
                $encoder = $this->encoderFactory->getEncoder($Customer);
95
96 2 View Code Duplication
                if ($Customer->getPassword() === $this->eccubeConfig['eccube_default_password']) {
97
                    $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...
98
                } else {
99 2
                    if ($Customer->getSalt() === null) {
100 1
                        $Customer->setSalt($encoder->createSalt());
101 1
                        $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
102
                    }
103 2
                    $Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt()));
104
                }
105
106 2
                $this->entityManager->persist($Customer);
107 2
                $this->entityManager->flush();
108
109 2
                log_info('会員登録完了', [$Customer->getId()]);
110
111 2
                $event = new EventArgs(
112
                    [
113 2
                        'form' => $form,
114 2
                        'Customer' => $Customer,
115
                    ],
116 2
                    $request
117
                );
118 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
119
120 2
                $this->addSuccess('admin.customer.save.complete', 'admin');
121
122 2
                return $this->redirectToRoute('admin_customer_edit', [
123 2
                    'id' => $Customer->getId(),
124
                ]);
125
            } else {
126
                $this->addError('admin.customer.save.failed', 'admin');
127
            }
128
        }
129
130
        return [
131 5
            'form' => $form->createView(),
132 5
            'Customer' => $Customer,
133
        ];
134
    }
135
}
136