Completed
Pull Request — experimental/sf (#3397)
by
unknown
308:10 queued 300:55
created

CustomerEditController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 7.63 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 9
loc 118
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 12

2 Methods

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

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\Entity\Master\CustomerStatus;
20
use Eccube\Form\Type\Admin\CustomerType;
21
use Eccube\Repository\CustomerRepository;
22
use Eccube\Util\StringUtil;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
28
29
class CustomerEditController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
30
{
31
    /**
32
     * @var CustomerRepository
33
     */
34
    protected $customerRepository;
35
36
    /**
37
     * @var EncoderFactoryInterface
38
     */
39
    protected $encoderFactory;
40
41 6
    public function __construct(
42
        CustomerRepository $customerRepository,
43
        EncoderFactoryInterface $encoderFactory
44
    ) {
45 6
        $this->customerRepository = $customerRepository;
46 6
        $this->encoderFactory = $encoderFactory;
47
    }
48
49
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
50
     * @Route("/%eccube_admin_route%/customer/new", name="admin_customer_new")
51
     * @Route("/%eccube_admin_route%/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit")
52
     * @Template("@admin/Customer/edit.twig")
53
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
54 6
    public function index(Request $request, $id = null)
55
    {
56
        //$this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
57
        // 編集
58 6
        if ($id) {
59 4
            $Customer = $this->customerRepository
60 4
                ->find($id);
61
62 4
            if (is_null($Customer)) {
63
                throw new NotFoundHttpException();
64
            }
65
66 4
            $oldStatusId = $Customer->getStatus()->getId();
67
            // 編集用にデフォルトパスワードをセット
68 4
            $previous_password = $Customer->getPassword();
69 4
            $Customer->setPassword($this->eccubeConfig['eccube_default_password']);
70
        // 新規登録
71
        } else {
72 2
            $Customer = $this->customerRepository->newCustomer();
73 2
            $Customer->setBuyTimes(0);
74 2
            $Customer->setBuyTotal(0);
75 2
            $oldStatusId = null;
76
        }
77
78
        // 会員登録フォーム
79 6
        $builder = $this->formFactory
80 6
            ->createBuilder(CustomerType::class, $Customer);
81
82 6
        $event = new EventArgs(
83
            [
84 6
                'builder' => $builder,
85 6
                'Customer' => $Customer,
86
            ],
87 6
            $request
88
        );
89 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
90
91 6
        $form = $builder->getForm();
92
93 6
        $form->handleRequest($request);
94
95 6
        if ($form->isSubmitted()) {
96 2
            if ($form->isValid()) {
97 2
                log_info('会員登録開始', [$Customer->getId()]);
98
99 2
                $encoder = $this->encoderFactory->getEncoder($Customer);
100
101 2 View Code Duplication
                if ($Customer->getPassword() === $this->eccubeConfig['eccube_default_password']) {
102
                    $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...
103
                } else {
104 2
                    if ($Customer->getSalt() === null) {
105 1
                        $Customer->setSalt($encoder->createSalt());
106 1
                        $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
107
                    }
108 2
                    $Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt()));
109
                }
110
111
                // 退会ステータスに更新の場合、ダミーのアドレスに更新
112 2
                $newStatusId = $Customer->getStatus()->getId();
113 2
                if ($oldStatusId != $newStatusId && $newStatusId == CustomerStatus::WITHDRAWING) {
114
                    $Customer->setEmail(StringUtil::random(60).'@dummy.dummy');
115
                }
116
117 2
                $this->entityManager->persist($Customer);
118 2
                $this->entityManager->flush();
119
120 2
                log_info('会員登録完了', [$Customer->getId()]);
121
122 2
                $event = new EventArgs(
123
                    [
124 2
                        'form' => $form,
125 2
                        'Customer' => $Customer,
126
                    ],
127 2
                    $request
128
                );
129 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
130
131 2
                $this->addSuccess('admin.customer.save.complete', 'admin');
132
133 2
                return $this->redirectToRoute('admin_customer_edit', [
134 2
                    'id' => $Customer->getId(),
135
                ]);
136
            } else {
137
                $this->addError('admin.customer.save.failed', 'admin');
138
            }
139
        }
140
141
        return [
142 4
            'form' => $form->createView(),
143 4
            'Customer' => $Customer,
144
        ];
145
    }
146
}
147