Failed Conditions
Pull Request — master (#1399)
by Shinichi
31:45
created

EntryController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 5.73 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 78.26%
Metric Value
wmc 11
lcom 0
cbo 11
dl 9
loc 157
ccs 18
cts 23
cp 0.7826
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 9 90 6
A complete() 0 5 1
B activate() 0 38 4

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
25
namespace Eccube\Controller;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Master\CustomerStatus;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\Validator\Constraints as Assert;
32
use Symfony\Component\HttpKernel\Exception as HttpException;
33
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
34
35
class EntryController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
38
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
39
     * Index
40
     *
41
     * @param  Application $app
42
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
43
     */
44 5
    public function index(Application $app, Request $request)
45
    {
46
        /** @var $Customer \Eccube\Entity\Customer */
47
        $Customer = $app['eccube.repository.customer']->newCustomer();
48
49
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
50
        $builder = $app['form.factory']->createBuilder('entry', $Customer);
51
52
        /* @var $form \Symfony\Component\Form\FormInterface */
53
        $form = $builder->getForm();
54
        if ('POST' === $request->getMethod()) {
55
            $form->handleRequest($request);
56
57
            if ($form->isValid()) {
58 3
                switch ($request->get('mode')) {
59 3 View Code Duplication
                    case 'confirm':
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...
60
                        $builder->setAttribute('freeze', true);
61
                        $form = $builder->getForm();
62
                        $form->handleRequest($request);
63
64 1
                        return $app['twig']->render('Entry/confirm.twig', array(
65 1
                            'form' => $form->createView(),
66
                        ));
67
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
68
69 2
                    case 'complete':
70
                        $Customer->setSalt(
71
                            $app['eccube.repository.customer']
72
                                ->createSalt(5)
73
                        );
74
75
                        $Customer->setPassword(
76
                            $app['eccube.repository.customer']->encryptPassword($app, $Customer)
77
                        );
78
79
                        $Customer->setSecretKey(
80
                            $app['eccube.repository.customer']->getUniqueSecretKey($app)
81
                        );
82
83
                        $CustomerAddress = new \Eccube\Entity\CustomerAddress();
84
                        $CustomerAddress->setName01($Customer->getName01())
85
                            ->setName02($Customer->getName02())
86
                            ->setKana01($Customer->getKana01())
87
                            ->setKana02($Customer->getKana02())
88
                            ->setCompanyName($Customer->getCompanyName())
89
                            ->setZip01($Customer->getZip01())
90
                            ->setZip02($Customer->getZip02())
91
                            ->setZipcode($Customer->getZip01() . $Customer->getZip02())
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
92
                            ->setPref($Customer->getPref())
93
                            ->setAddr01($Customer->getAddr01())
94
                            ->setAddr02($Customer->getAddr02())
95
                            ->setTel01($Customer->getTel01())
96
                            ->setTel02($Customer->getTel02())
97
                            ->setTel03($Customer->getTel03())
98
                            ->setFax01($Customer->getFax01())
99
                            ->setFax02($Customer->getFax02())
100
                            ->setFax03($Customer->getFax03())
101 1
                            ->setDelFlg(Constant::DISABLED)
102
                            ->setCustomer($Customer);
103
104
                        $app['orm.em']->persist($Customer);
105
                        $app['orm.em']->persist($CustomerAddress);
106
                        $app['orm.em']->flush();
107
108
                        $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey()));
109
110
                        /** @var $BaseInfo \Eccube\Entity\BaseInfo */
111
                        $BaseInfo = $app['eccube.repository.base_info']->get();
112
                        $activateFlg = $BaseInfo->getOptionCustomerActivate();
113
114
                        // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
115 1
                        if ($activateFlg) {
116
117
                            // メール送信
118
                            $app['eccube.service.mail']->sendCustomerConfirmMail($Customer, $activateUrl);
119
120
                            return $app->redirect($app->url('entry_complete'));
121
122
                        // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
123
                        } else {
124
                            return $app->redirect($activateUrl);
125
                        }
126
                }
127
            }
128
        }
129
130 3
        return $app['view']->render('Entry/index.twig', array(
131 3
            'form' => $form->createView(),
132
        ));
133 5
    }
134
135
    /**
136
     * Complete
137
     *
138
     * @param  Application $app
139
     * @return mixed
140
     */
141 1
    public function complete(Application $app)
142
    {
143 1
        return $app['view']->render('Entry/complete.twig', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
144
        ));
145 1
    }
146
147
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$secret_key" missing
Loading history...
148
     * 会員のアクティベート(本会員化)を行う
149
     *
150
     * @param  Application $app
151
     * @return mixed
152
     */
153 3
    public function activate(Application $app, Request $request, $secret_key)
154
    {
155
        $errors = $app['validator']->validateValue($secret_key, array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
156
                new Assert\NotBlank(),
157
                new Assert\Regex(array(
158
                    'pattern' => '/^[a-zA-Z0-9]+$/',
159
                ))
160 3
            )
161
            );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
162
163
        if ($request->getMethod() === 'GET' && count($errors) === 0) {
164
            try {
165
                $Customer = $app['eccube.repository.customer']
166
                    ->getNonActiveCustomerBySecretKey($secret_key);
167
            } catch (\Exception $e) {
168
                throw new HttpException\NotFoundHttpException('※ 既に会員登録が完了しているか、無効なURLです。');
169 2
            }
170
171
            $CustomerStatus = $app['eccube.repository.customer_status']->find(CustomerStatus::ACTIVE);
172
            $Customer->setStatus($CustomerStatus);
173
            $Customer->setBuyTimes(0);
174
            $Customer->setBuyTotal(0);
175
176
            $app['orm.em']->persist($Customer);
177
            $app['orm.em']->flush();
178
179
            // メール送信
180
            $app['eccube.service.mail']->sendCustomerCompleteMail($Customer);
181
182
            // 本会員登録してログイン状態にする
183
            $token = new UsernamePasswordToken($Customer, null, 'customer', array('ROLE_USER'));
184
            $this->getSecurity($app)->setToken($token);
185
186
            return $app['view']->render('Entry/activate.twig');
187
        } else {
188
            throw new HttpException\AccessDeniedHttpException('不正なアクセスです。');
189
        }
190 3
    }
191
}
192