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

ClassCategoryController::moveRank()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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