Completed
Push — master ( 0d01b3...15b45e )
by chihiro
156:47 queued 148:56
created

CustomerEditController::index()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 110
Code Lines 74

Duplication

Lines 10
Ratio 9.09 %

Code Coverage

Tests 74
CRAP Score 8.0412

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 110
ccs 74
cts 81
cp 0.9136
rs 5.2676
cc 8
eloc 74
nc 17
nop 3
crap 8.0412

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 Eccube\Application;
27
use Eccube\Common\Constant;
28
use Eccube\Controller\AbstractController;
29
use Eccube\Event\EccubeEvents;
30
use Eccube\Event\EventArgs;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
34
class CustomerEditController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36 6
    public function index(Application $app, Request $request, $id = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        // 編集
39 6
        if ($id) {
40 4
            $Customer = $app['orm.em']
41 4
                ->getRepository('Eccube\Entity\Customer')
42 4
                ->find($id);
43
44 4
            if (is_null($Customer)) {
45
                throw new NotFoundHttpException();
46
            }
47
            // 編集用にデフォルトパスワードをセット
48 4
            $previous_password = $Customer->getPassword();
49 4
            $Customer->setPassword($app['config']['default_password']);
50
            // 新規登録
51 4
        } else {
52 2
            $Customer = $app['eccube.repository.customer']->newCustomer();
53 2
            $CustomerAddress = new \Eccube\Entity\CustomerAddress();
54 2
            $Customer->setBuyTimes(0);
55 2
            $Customer->setBuyTotal(0);
56
        }
57
58
        // 会員登録フォーム
59 6
        $builder = $app['form.factory']
60 6
            ->createBuilder('admin_customer', $Customer);
61
62 6
        $event = new EventArgs(
63
            array(
64 6
                'builder' => $builder,
65 6
                'Customer' => $Customer,
66 6
            ),
67
            $request
68 6
        );
69 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event);
70
71 6
        $form = $builder->getForm();
72
73 6
        if ('POST' === $request->getMethod()) {
74 3
            $form->handleRequest($request);
75 3
            if ($form->isValid()) {
76 3
                if ($Customer->getId() === null) {
77 1
                    $Customer->setSalt(
78 1
                        $app['eccube.repository.customer']->createSalt(5)
79 1
                    );
80 1
                    $Customer->setSecretKey(
81 1
                        $app['eccube.repository.customer']->getUniqueSecretKey($app)
82 1
                    );
83
84 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...
85 1
                        ->setName02($Customer->getName02())
86 1
                        ->setKana01($Customer->getKana01())
87 1
                        ->setKana02($Customer->getKana02())
88 1
                        ->setCompanyName($Customer->getCompanyName())
89 1
                        ->setZip01($Customer->getZip01())
90 1
                        ->setZip02($Customer->getZip02())
91 1
                        ->setZipcode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
92 1
                        ->setPref($Customer->getPref())
93 1
                        ->setAddr01($Customer->getAddr01())
94 1
                        ->setAddr02($Customer->getAddr02())
95 1
                        ->setTel01($Customer->getTel01())
96 1
                        ->setTel02($Customer->getTel02())
97 1
                        ->setTel03($Customer->getTel03())
98 1
                        ->setFax01($Customer->getFax01())
99 1
                        ->setFax02($Customer->getFax02())
100 1
                        ->setFax03($Customer->getFax03())
101 1
                        ->setDelFlg(Constant::DISABLED)
102 1
                        ->setCustomer($Customer);
103
104 1
                    $app['orm.em']->persist($CustomerAddress);
105 1
                }
106
107 3 View Code Duplication
                if ($Customer->getPassword() === $app['config']['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...
108
                    $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...
109
                } else {
110 3
                    if ($Customer->getSalt() === null) {
111
                        $Customer->setSalt($app['eccube.repository.customer']->createSalt(5));
112
                    }
113 3
                    $Customer->setPassword(
114 3
                        $app['eccube.repository.customer']->encryptPassword($app, $Customer)
115 3
                    );
116
                }
117
118 3
                $app['orm.em']->persist($Customer);
119
120 3
                $app['orm.em']->flush();
121
122 3
                $event = new EventArgs(
123
                    array(
124 3
                        'form' => $form,
125 3
                        'Customer' => $Customer,
126 3
                    ),
127
                    $request
128 3
                );
129 3
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event);
130
131 3
                $app->addSuccess('admin.customer.save.complete', 'admin');
132
133 3
                return $app->redirect($app->url('admin_customer_edit', array(
134 3
                    'id' => $Customer->getId(),
135 3
                )));
136
            } else {
137
                $app->addError('admin.customer.save.failed', 'admin');
138
            }
139
        }
140
141 3
        return $app->render('Customer/edit.twig', array(
142 3
            'form' => $form->createView(),
143 3
            'Customer' => $Customer,
144 3
        ));
145
    }
146
}
147