Completed
Push — sf/csv-headers ( 829ce9...de2c5f )
by Kiyotaka
06:12
created

CustomerEditController::index()   C

Complexity

Conditions 9
Paths 17

Size

Total Lines 92

Duplication

Lines 9
Ratio 9.78 %

Importance

Changes 0
Metric Value
cc 9
nc 17
nop 2
dl 9
loc 92
rs 6.6189
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
/*
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
30
{
31
    /**
32
     * @var CustomerRepository
33
     */
34
    protected $customerRepository;
35
36
    /**
37
     * @var EncoderFactoryInterface
38
     */
39
    protected $encoderFactory;
40
41
    public function __construct(
42
        CustomerRepository $customerRepository,
43
        EncoderFactoryInterface $encoderFactory
44
    ) {
45
        $this->customerRepository = $customerRepository;
46
        $this->encoderFactory = $encoderFactory;
47
    }
48
49
    /**
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
     */
54
    public function index(Request $request, $id = null)
55
    {
56
        $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
57
        // 編集
58
        if ($id) {
59
            $Customer = $this->customerRepository
60
                ->find($id);
61
62
            if (is_null($Customer)) {
63
                throw new NotFoundHttpException();
64
            }
65
66
            $oldStatusId = $Customer->getStatus()->getId();
67
            // 編集用にデフォルトパスワードをセット
68
            $previous_password = $Customer->getPassword();
69
            $Customer->setPassword($this->eccubeConfig['eccube_default_password']);
70
        // 新規登録
71
        } else {
72
            $Customer = $this->customerRepository->newCustomer();
73
            $Customer->setBuyTimes(0);
74
            $Customer->setBuyTotal(0);
75
            $oldStatusId = null;
76
        }
77
78
        // 会員登録フォーム
79
        $builder = $this->formFactory
80
            ->createBuilder(CustomerType::class, $Customer);
81
82
        $event = new EventArgs(
83
            [
84
                'builder' => $builder,
85
                'Customer' => $Customer,
86
            ],
87
            $request
88
        );
89
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
90
91
        $form = $builder->getForm();
92
93
        $form->handleRequest($request);
94
95
        if ($form->isSubmitted()) {
96
            if ($form->isValid()) {
97
                log_info('会員登録開始', [$Customer->getId()]);
98
99
                $encoder = $this->encoderFactory->getEncoder($Customer);
100
101 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...
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
                    if ($Customer->getSalt() === null) {
105
                        $Customer->setSalt($encoder->createSalt());
106
                        $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey());
107
                    }
108
                    $Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt()));
109
                }
110
111
                // 退会ステータスに更新の場合、ダミーのアドレスに更新
112
                $newStatusId = $Customer->getStatus()->getId();
113
                if ($oldStatusId != $newStatusId && $newStatusId == CustomerStatus::WITHDRAWING) {
114
                    $Customer->setEmail(StringUtil::random(60).'@dummy.dummy');
115
                }
116
117
                $this->entityManager->persist($Customer);
118
                $this->entityManager->flush();
119
120
                log_info('会員登録完了', [$Customer->getId()]);
121
122
                $event = new EventArgs(
123
                    [
124
                        'form' => $form,
125
                        'Customer' => $Customer,
126
                    ],
127
                    $request
128
                );
129
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
130
131
                $this->addSuccess('admin.customer.save.complete', 'admin');
132
133
                return $this->redirectToRoute('admin_customer_edit', [
134
                    'id' => $Customer->getId(),
135
                ]);
136
            } else {
137
                $this->addError('admin.customer.save.failed', 'admin');
138
            }
139
        }
140
141
        return [
142
            'form' => $form->createView(),
143
            'Customer' => $Customer,
144
        ];
145
    }
146
}
147