Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

ClassCategoryController::index()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 67
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 9.4499

Importance

Changes 0
Metric Value
cc 7
eloc 42
nc 8
nop 4
dl 0
loc 67
ccs 24
cts 38
cp 0.6316
crap 9.4499
rs 7.0237
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
 * 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\Annotation\Component;
30
use Eccube\Application;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Admin\ClassCategoryType;
35
use Eccube\Repository\ClassCategoryRepository;
36
use Eccube\Repository\ClassNameRepository;
37
use Eccube\Repository\ProductClassRepository;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
41
use Symfony\Component\EventDispatcher\EventDispatcher;
42
use Symfony\Component\Form\FormFactory;
43
use Symfony\Component\HttpFoundation\Request;
44
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
45
46
/**
47
 * @Component
48
 * @Route(service=ClassCategoryController::class)
49
 */
50
class ClassCategoryController extends AbstractController
51
{
52
    /**
53
     * @Inject("orm.em")
54
     * @var EntityManager
55
     */
56
    protected $entityManager;
57
58
    /**
59
     * @Inject(ProductClassRepository::class)
60
     * @var ProductClassRepository
61
     */
62
    protected $productClassRepository;
63
64
    /**
65
     * @Inject("eccube.event.dispatcher")
66
     * @var EventDispatcher
67
     */
68
    protected $eventDispatcher;
69
70
    /**
71
     * @Inject("form.factory")
72
     * @var FormFactory
73
     */
74
    protected $formFactory;
75
76
    /**
77
     * @Inject(ClassCategoryRepository::class)
78
     * @var ClassCategoryRepository
79
     */
80
    protected $classCategoryRepository;
81
82
    /**
83
     * @Inject(ClassNameRepository::class)
84
     * @var ClassNameRepository
85
     */
86
    protected $classNameRepository;
87
88
    /**
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 "$class_name_id" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
89
     * @Route("/{_admin}/product/class_category/{class_name_id}", requirements={"class_name_id" = "\d+"}, name="admin_product_class_category")
90
     * @Route("/{_admin}/product/class_category/{class_name_id}/{id}/edit", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_edit")
91
     * @Template("Product/class_category.twig")
92
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
93 2
    public function index(Application $app, Request $request, $class_name_id, $id = null)
94
    {
95
        //
96 2
        $ClassName = $this->classNameRepository->find($class_name_id);
97 2
        if (!$ClassName) {
98
            throw new NotFoundHttpException('商品規格が存在しません');
99
        }
100 2
        if ($id) {
101 1
            $TargetClassCategory = $this->classCategoryRepository->find($id);
102 1
            if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
103 1
                throw new NotFoundHttpException('商品規格が存在しません');
104
            }
105
        } else {
106 1
            $TargetClassCategory = new \Eccube\Entity\ClassCategory();
107 1
            $TargetClassCategory->setClassName($ClassName);
108
        }
109
110
        //
111 2
        $builder = $this->formFactory
112 2
            ->createBuilder(ClassCategoryType::class, $TargetClassCategory);
113
114 2
        $event = new EventArgs(
115
            array(
116 2
                'builder' => $builder,
117 2
                'ClassName' => $ClassName,
118 2
                'TargetClassCategory' => $TargetClassCategory,
119
            ),
120 2
            $request
121
        );
122 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_INITIALIZE, $event);
123
124 2
        $form = $builder->getForm();
125
126 2
        if ($request->getMethod() === 'POST') {
127
            $form->handleRequest($request);
128
            if ($form->isValid()) {
129
                log_info('規格分類登録開始', array($id));
130
131
                $this->classCategoryRepository->save($TargetClassCategory);
132
133
                log_info('規格分類登録完了', array($id));
134
135
                $event = new EventArgs(
136
                    array(
137
                        'form' => $form,
138
                        'ClassName' => $ClassName,
139
                        'TargetClassCategory' => $TargetClassCategory,
140
                    ),
141
                    $request
142
                );
143
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_COMPLETE, $event);
144
145
                $app->addSuccess('admin.class_category.save.complete', 'admin');
146
147
                return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
148
            }
149
        }
150
151 2
        $ClassCategories = $this->classCategoryRepository->getList($ClassName);
152
153
        return [
154 2
            'form' => $form->createView(),
155 2
            'ClassName' => $ClassName,
156 2
            'ClassCategories' => $ClassCategories,
157 2
            'TargetClassCategory' => $TargetClassCategory,
158
        ];
159
    }
160
161
    /**
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 "$class_name_id" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
162
     * @Method("DELETE")
163
     * @Route("/{_admin}/product/class_category/{class_name_id}/{id}/delete", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_delete")
164
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
165 1
    public function delete(Application $app, Request $request, $class_name_id, $id)
166
    {
167 1
        $this->isTokenValid($app);
168
169 1
        $ClassName = $this->classNameRepository->find($class_name_id);
170 1
        if (!$ClassName) {
171
            throw new NotFoundHttpException('商品規格が存在しません');
172
        }
173
174 1
        log_info('規格分類削除開始', array($id));
175
176 1
        $TargetClassCategory = $this->classCategoryRepository->find($id);
177 1 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
178
            $app->deleteMessage();
179
            return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
180
        }
181
182 1
        $num = $this->productClassRepository->createQueryBuilder('pc')
183 1
            ->select('count(pc.id)')
184 1
            ->where('pc.ClassCategory1 = :id OR pc.ClassCategory2 = :id')
185 1
            ->setParameter('id',$id)
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
186 1
            ->getQuery()
187 1
            ->getSingleScalarResult();
188 1
        if ($num > 0) {
189
            $app->addError('admin.class_category.delete.hasproduct', 'admin');
190
        } else {
191
            try {
192 1
                $this->classCategoryRepository->delete($TargetClassCategory);
193
194 1
                $event = new EventArgs(
195
                    array(
196 1
                        'ClassName' => $ClassName,
197 1
                        'TargetClassCategory' => $TargetClassCategory,
198
                    ),
199 1
                    $request
200
                );
201 1
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
202
203 1
                $app->addSuccess('admin.class_category.delete.complete', 'admin');
204
205 1
                log_info('規格分類削除完了', array($id));
206
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
207
            } catch (\Exception $e) {
208
                log_error('規格分類削除エラー', array($id, $e));
209
210
                $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => '規格分類']);
211
                $app->addError($message, 'admin');            }
212
        }
213
214 1
        return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
215
    }
216
217
    /**
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 "$class_name_id" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
218
     * @Method("PUT")
219
     * @Route("/{_admin}/product/class_category/{class_name_id}/{id}/visibility", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_visibility")
220
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
221 1
    public function visibility(Application $app, Request $request, $class_name_id, $id)
222
    {
223 1
        $this->isTokenValid($app);
224
225 1
        $ClassName = $this->classNameRepository->find($class_name_id);
226 1
        if (!$ClassName) {
227
            throw new NotFoundHttpException('商品規格が存在しません');
228
        }
229
230 1
        log_info('規格分類表示変更開始', array($id));
231
232 1
        $TargetClassCategory = $this->classCategoryRepository->find($id);
233 1 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
234
            $app->deleteMessage();
235
            return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
236
        }
237
238 1
        $this->classCategoryRepository->toggleVisibility($TargetClassCategory);
239
240 1
        log_info('規格分類表示変更完了', array($id));
241
242 1
        $event = new EventArgs(
243
            array(
244 1
                'ClassName' => $ClassName,
245 1
                'TargetClassCategory' => $TargetClassCategory,
246
            ),
247 1
            $request
248
        );
249 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
250
251 1
        $app->addSuccess('admin.class_category.delete.complete', 'admin');
252
253 1
        return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
254
    }
255
256
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
257
     * @Method("POST")
258
     * @Route("/product/class_category/rank/move", name="admin_product_class_category_rank_move")
259
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
260 View Code Duplication
    public function moveRank(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...
261
    {
262
        if ($request->isXmlHttpRequest()) {
263
            $ranks = $request->request->all();
264
            foreach ($ranks as $categoryId => $rank) {
265
                $ClassCategory = $this->classCategoryRepository
266
                    ->find($categoryId);
267
                $ClassCategory->setRank($rank);
268
                $this->entityManager->persist($ClassCategory);
0 ignored issues
show
Bug introduced by
It seems like $ClassCategory defined by $this->classCategoryRepository->find($categoryId) on line 265 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...
269
            }
270
            $this->entityManager->flush();
271
        }
272
        return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
273
    }
274
}
275