Failed Conditions
Push — experimental/3.1 ( 94d0cd...3ded70 )
by chihiro
23s
created

ClassCategoryController::visibility()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 41
Code Lines 24

Duplication

Lines 23
Ratio 56.1 %

Code Coverage

Tests 17
CRAP Score 5.2935

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 4
nop 4
dl 23
loc 41
ccs 17
cts 22
cp 0.7727
crap 5.2935
rs 8.439
c 0
b 0
f 0
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
                $status = $this->classCategoryRepository->save($TargetClassCategory);
131
132
                if ($status) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
133
134
                    log_info('規格分類登録完了', array($id));
135
136
                    $event = new EventArgs(
137
                        array(
138
                            'form' => $form,
139
                            'ClassName' => $ClassName,
140
                            'TargetClassCategory' => $TargetClassCategory,
141
                        ),
142
                        $request
143
                    );
144
                    $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_COMPLETE, $event);
145
146
                    $app->addSuccess('admin.class_category.save.complete', 'admin');
147
148
                    return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
149
                } else {
150
                    log_info('規格分類登録エラー', array($id));
151
                    $app->addError('admin.class_category.save.error', 'admin');
152
                }
153
            }
154
        }
155
156 2
        $ClassCategories = $this->classCategoryRepository->getList($ClassName);
157
158
        return [
159 2
            'form' => $form->createView(),
160 2
            'ClassName' => $ClassName,
161 2
            'ClassCategories' => $ClassCategories,
162 2
            'TargetClassCategory' => $TargetClassCategory,
163
        ];
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...
introduced by
Doc comment for parameter "$class_name_id" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
167
     * @Method("DELETE")
168
     * @Route("/{_admin}/product/class_category/{class_name_id}/{id}/delete", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_delete")
169
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
170 1
    public function delete(Application $app, Request $request, $class_name_id, $id)
171
    {
172 1
        $this->isTokenValid($app);
173
174 1
        $ClassName = $this->classNameRepository->find($class_name_id);
175 1
        if (!$ClassName) {
176
            throw new NotFoundHttpException('商品規格が存在しません');
177
        }
178
179 1
        log_info('規格分類削除開始', array($id));
180
181 1
        $TargetClassCategory = $this->classCategoryRepository->find($id);
182 1 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
183
            $app->deleteMessage();
184
            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...
185
        }
186
187 1
        $num = $this->productClassRepository->createQueryBuilder('pc')
188 1
            ->select('count(pc.id)')
189 1
            ->where('pc.ClassCategory1 = :id OR pc.ClassCategory2 = :id')
190 1
            ->setParameter('id',$id)
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
191 1
            ->getQuery()
192 1
            ->getSingleScalarResult();
193 1
        if ($num > 0) {
194
            $app->addError('admin.class_category.delete.hasproduct', 'admin');
195 View Code Duplication
        } else {
196 1
            $status = $this->classCategoryRepository->delete($TargetClassCategory);
197
198 1
            if ($status === true) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
199
200 1
                log_info('規格分類削除完了', array($id));
201
202 1
                $event = new EventArgs(
203
                    array(
204 1
                        'ClassName' => $ClassName,
205 1
                        'TargetClassCategory' => $TargetClassCategory,
206
                    ),
207 1
                    $request
208
                );
209 1
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
210
211 1
                $app->addSuccess('admin.class_category.delete.complete', 'admin');
212
            } else {
213
                log_info('規格分類削除エラー', array($id));
214
215
                $app->addError('admin.class_category.delete.error', 'admin');
216
            }
217
        }
218
219 1
        return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
220
    }
221
222
    /**
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...
223
     * @Method("PUT")
224
     * @Route("/{_admin}/product/class_category/{class_name_id}/{id}/visibility", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_visibility")
225
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
226 1
    public function visibility(Application $app, Request $request, $class_name_id, $id)
227
    {
228 1
        $this->isTokenValid($app);
229
230 1
        $ClassName = $this->classNameRepository->find($class_name_id);
231 1
        if (!$ClassName) {
232
            throw new NotFoundHttpException('商品規格が存在しません');
233
        }
234
235 1
        log_info('規格分類表示変更開始', array($id));
236
237 1
        $TargetClassCategory = $this->classCategoryRepository->find($id);
238 1 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
239
            $app->deleteMessage();
240
            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...
241
        }
242
243 1
        $status = $this->classCategoryRepository->toggleVisibility($TargetClassCategory);
244
245 1 View Code Duplication
        if ($status === true) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
246
247 1
            log_info('規格分類表示変更完了', array($id));
248
249 1
            $event = new EventArgs(
250
                array(
251 1
                    'ClassName' => $ClassName,
252 1
                    'TargetClassCategory' => $TargetClassCategory,
253
                ),
254 1
                $request
255
            );
256 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
257
258 1
            $app->addSuccess('admin.class_category.delete.complete', 'admin');
259
        } else {
260
            log_info('規格分類表示変更エラー', array($id));
261
262
            $app->addError('admin.class_category.delete.error', 'admin');
263
        }
264
265 1
        return $app->redirect($app->url('admin_product_class_category', array('class_name_id' => $ClassName->getId())));
266
    }
267
268
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
269
     * @Method("POST")
270
     * @Route("/product/class_category/rank/move", name="admin_product_class_category_rank_move")
271
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
272 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...
273
    {
274
        if ($request->isXmlHttpRequest()) {
275
            $ranks = $request->request->all();
276
            foreach ($ranks as $categoryId => $rank) {
277
                $ClassCategory = $this->classCategoryRepository
278
                    ->find($categoryId);
279
                $ClassCategory->setRank($rank);
280
                $this->entityManager->persist($ClassCategory);
0 ignored issues
show
Bug introduced by
It seems like $ClassCategory defined by $this->classCategoryRepository->find($categoryId) on line 277 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...
281
            }
282
            $this->entityManager->flush();
283
        }
284
        return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
285
    }
286
}
287