Completed
Push — experimental/3.1 ( 878c99...05b497 )
by chihiro
50:03
created

ClassNameController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 45.32 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
dl 63
loc 139
ccs 50
cts 54
cp 0.9259
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 23 57 5
B delete() 25 25 2
A moveSortNo() 15 15 3

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\Admin\Product;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Inject;
29
use Eccube\Application;
30
use Eccube\Controller\AbstractController;
31
use Eccube\Entity\ClassName;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Admin\ClassNameType;
35
use Eccube\Repository\ClassNameRepository;
36
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Component\EventDispatcher\EventDispatcher;
40
use Symfony\Component\Form\FormFactory;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
44
/**
45
 * @Route(service=ClassNameController::class)
46
 */
47
class ClassNameController extends AbstractController
48
{
49
    /**
50
     * @Inject("orm.em")
51
     * @var EntityManager
52
     */
53
    protected $entityManager;
54
55
    /**
56
     * @Inject("eccube.event.dispatcher")
57
     * @var EventDispatcher
58
     */
59
    protected $eventDispatcher;
60
61
    /**
62
     * @Inject("form.factory")
63
     * @var FormFactory
64
     */
65
    protected $formFactory;
66
67
    /**
68
     * @Inject(ClassNameRepository::class)
69
     * @var ClassNameRepository
70
     */
71
    protected $classNameRepository;
72
73
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
74
     * @Route("/{_admin}/product/class_name", name="admin_product_class_name")
75
     * @Route("/{_admin}/product/class_name/{id}/edit", requirements={"id" = "\d+"}, name="admin_product_class_name_edit")
76
     * @Template("Product/class_name.twig")
77
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
78 3
    public function index(Application $app, Request $request, $id = null)
79
    {
80 3
        if ($id) {
81 1
            $TargetClassName = $this->classNameRepository->find($id);
82 1
            if (!$TargetClassName) {
83 1
                throw new NotFoundHttpException('商品規格が存在しません');
84
            }
85
        } else {
86 2
            $TargetClassName = new \Eccube\Entity\ClassName();
87
        }
88
89 3
        $builder = $this->formFactory
90 3
            ->createBuilder(ClassNameType::class, $TargetClassName);
91
92 3
        $event = new EventArgs(
93
            array(
94 3
                'builder' => $builder,
95 3
                'TargetClassName' => $TargetClassName,
96
            ),
97 3
            $request
98
        );
99 3
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_INDEX_INITIALIZE, $event);
100
101 3
        $form = $builder->getForm();
102
103 3 View Code Duplication
        if ($request->getMethod() === 'POST') {
104 1
            $form->handleRequest($request);
105 1
            if ($form->isValid()) {
106 1
                log_info('商品規格登録開始', array($id));
107
108 1
                $this->classNameRepository->save($TargetClassName);
109
110 1
                log_info('商品規格登録完了', array($id));
111
112 1
                $event = new EventArgs(
113
                    array(
114 1
                        'form' => $form,
115 1
                        'TargetClassName' => $TargetClassName,
116
                    ),
117 1
                    $request
118
                );
119 1
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_INDEX_COMPLETE, $event);
120
121 1
                $app->addSuccess('admin.class_name.save.complete', 'admin');
122
123 1
                return $app->redirect($app->url('admin_product_class_name'));
124
            }
125
        }
126
127 2
        $ClassNames = $this->classNameRepository->getList();
128
129
        return [
130 2
            'form' => $form->createView(),
131 2
            'ClassNames' => $ClassNames,
132 2
            'TargetClassName' => $TargetClassName,
133
        ];
134
    }
135
136
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$ClassName" missing
Loading history...
137
     * @Method("DELETE")
138
     * @Route("/{_admin}/product/class_name/{id}/delete", requirements={"id" = "\d+"}, name="admin_product_class_name_delete")
139
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
140 1 View Code Duplication
    public function delete(Application $app, Request $request, ClassName $ClassName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
141
    {
142 1
        $this->isTokenValid($app);
143
144 1
        log_info('商品規格削除開始', array($ClassName->getId()));
145
146
        try {
147 1
            $this->classNameRepository->delete($ClassName);
148
149 1
            $event = new EventArgs(['ClassName' => $ClassName,], $request);
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
150 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_DELETE_COMPLETE, $event);
151
152 1
            $app->addSuccess('admin.class_name.delete.complete', 'admin');
153
154 1
            log_info('商品規格削除完了', array($ClassName->getId()));
155
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
156
        } catch (\Exception $e) {
157
            $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => '商品規格']);
158
            $app->addError($message, 'admin');
159
160
            log_error('商品企画削除エラー', [$ClassName->getId(), $e]);
161
        }
162
163 1
        return $app->redirect($app->url('admin_product_class_name'));
164
    }
165
166
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
167
     * @Method("POST")
168
     * @Route("/{_admin}/product/class_name/sort_no/move", name="admin_product_class_name_sort_no_move")
169
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
170 1 View Code Duplication
    public function moveSortNo(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
171
    {
172 1
        if ($request->isXmlHttpRequest()) {
173 1
            $sortNos = $request->request->all();
174 1
            foreach ($sortNos as $classNameId => $sortNo) {
175 1
                $ClassName = $this->classNameRepository
176 1
                    ->find($classNameId);
177 1
                $ClassName->setSortNo($sortNo);
178 1
                $this->entityManager->persist($ClassName);
0 ignored issues
show
Bug introduced by
It seems like $ClassName defined by $this->classNameRepository->find($classNameId) on line 175 can also be of type null; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
179
            }
180 1
            $this->entityManager->flush();
181
        }
182
183 1
        return true;
184
    }
185
}
186