Failed Conditions
Push — dev/payment-errors ( d6d2cc )
by Kiyotaka
11:23
created

ClassCategoryController::visibility()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 39

Duplication

Lines 10
Ratio 25.64 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 3
dl 10
loc 39
rs 8.9848
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Product;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Event\EccubeEvents;
18
use Eccube\Event\EventArgs;
19
use Eccube\Form\Type\Admin\ClassCategoryType;
20
use Eccube\Repository\ClassCategoryRepository;
21
use Eccube\Repository\ClassNameRepository;
22
use Eccube\Repository\ProductClassRepository;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
use Symfony\Component\Routing\Annotation\Route;
29
30
class ClassCategoryController extends AbstractController
31
{
32
    /**
33
     * @var ProductClassRepository
34
     */
35
    protected $productClassRepository;
36
37
    /**
38
     * @var ClassCategoryRepository
39
     */
40
    protected $classCategoryRepository;
41
42
    /**
43
     * @var ClassNameRepository
44
     */
45
    protected $classNameRepository;
46
47
    /**
48
     * ClassCategoryController constructor.
49
     *
50
     * @param ProductClassRepository $productClassRepository
51
     * @param ClassCategoryRepository $classCategoryRepository
52
     * @param ClassNameRepository $classNameRepository
53
     */
54
    public function __construct(
55
        ProductClassRepository $productClassRepository,
56
        ClassCategoryRepository $classCategoryRepository,
57
        ClassNameRepository $classNameRepository
58
    ) {
59
        $this->productClassRepository = $productClassRepository;
60
        $this->classCategoryRepository = $classCategoryRepository;
61
        $this->classNameRepository = $classNameRepository;
62
    }
63
64
    /**
65
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}", requirements={"class_name_id" = "\d+"}, name="admin_product_class_category")
66
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/edit", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_edit")
67
     * @Template("@admin/Product/class_category.twig")
68
     */
69
    public function index(Request $request, $class_name_id, $id = null)
70
    {
71
        $ClassName = $this->classNameRepository->find($class_name_id);
72
        if (!$ClassName) {
73
            throw new NotFoundHttpException();
74
        }
75
        if ($id) {
76
            $TargetClassCategory = $this->classCategoryRepository->find($id);
77
            if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
78
                throw new NotFoundHttpException();
79
            }
80
        } else {
81
            $TargetClassCategory = new \Eccube\Entity\ClassCategory();
82
            $TargetClassCategory->setClassName($ClassName);
83
        }
84
85
        $builder = $this->formFactory
86
            ->createBuilder(ClassCategoryType::class, $TargetClassCategory);
87
88
        $event = new EventArgs(
89
            [
90
                'builder' => $builder,
91
                'ClassName' => $ClassName,
92
                'TargetClassCategory' => $TargetClassCategory,
93
            ],
94
            $request
95
        );
96
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_INITIALIZE, $event);
97
98
        $ClassCategories = $this->classCategoryRepository->getList($ClassName);
99
100
        $forms = [];
101
        foreach ($ClassCategories as $ClassCategory) {
102
            $id = $ClassCategory->getId();
103
            $forms[$id] = $this->formFactory->createNamed('class_category_'.$id, ClassCategoryType::class, $ClassCategory);
104
        }
105
106
        $form = $builder->getForm();
107
108
        if ($request->getMethod() === 'POST') {
109
            $form->handleRequest($request);
110
            if ($form->isValid()) {
111
                log_info('規格分類登録開始', [$id]);
112
113
                $this->classCategoryRepository->save($TargetClassCategory);
114
115
                log_info('規格分類登録完了', [$id]);
116
117
                $event = new EventArgs(
118
                    [
119
                        'form' => $form,
120
                        'ClassName' => $ClassName,
121
                        'TargetClassCategory' => $TargetClassCategory,
122
                    ],
123
                    $request
124
                );
125
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_COMPLETE, $event);
126
127
                $this->addSuccess('admin.common.save_complete', 'admin');
128
129
                return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
130
            }
131
132 View Code Duplication
            foreach ($forms as $editForm) {
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...
133
                $editForm->handleRequest($request);
134
                if ($editForm->isSubmitted() && $editForm->isValid()) {
135
                    $this->classCategoryRepository->save($editForm->getData());
136
                    $this->addSuccess('admin.common.save_complete', 'admin');
137
138
                    return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
139
                }
140
            }
141
        }
142
143
        $formViews = [];
144
        foreach ($forms as $key => $value) {
145
            $formViews[$key] = $value->createView();
146
        }
147
148
        return [
149
            'form' => $form->createView(),
150
            'ClassName' => $ClassName,
151
            'ClassCategories' => $ClassCategories,
152
            'TargetClassCategory' => $TargetClassCategory,
153
            'forms' => $formViews,
154
        ];
155
    }
156
157
    /**
158
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/delete", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_delete", methods={"DELETE"})
159
     */
160
    public function delete(Request $request, $class_name_id, $id)
161
    {
162
        $this->isTokenValid();
163
164
        $ClassName = $this->classNameRepository->find($class_name_id);
165
        if (!$ClassName) {
166
            throw new NotFoundHttpException();
167
        }
168
169
        log_info('規格分類削除開始', [$id]);
170
171
        $TargetClassCategory = $this->classCategoryRepository->find($id);
172 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
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...
173
            $this->deleteMessage();
174
175
            return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
176
        }
177
178
        try {
179
            $this->classCategoryRepository->delete($TargetClassCategory);
180
181
            $event = new EventArgs(
182
                [
183
                    'ClassName' => $ClassName,
184
                    'TargetClassCategory' => $TargetClassCategory,
185
                ],
186
                $request
187
            );
188
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
189
190
            $this->addSuccess('admin.common.delete_complete', 'admin');
191
192
            log_info('規格分類削除完了', [$id]);
193
        } catch (\Exception $e) {
194
            log_error('規格分類削除エラー', [$id, $e]);
195
196
            $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetClassCategory->getName()]);
197
            $this->addError($message, 'admin');
198
        }
199
200
        return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
201
    }
202
203
    /**
204
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/visibility", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_visibility", methods={"PUT"})
205
     */
206
    public function visibility(Request $request, $class_name_id, $id)
207
    {
208
        $this->isTokenValid();
209
210
        $ClassName = $this->classNameRepository->find($class_name_id);
211
        if (!$ClassName) {
212
            throw new NotFoundHttpException();
213
        }
214
215
        log_info('規格分類表示変更開始', [$id]);
216
217
        $TargetClassCategory = $this->classCategoryRepository->find($id);
218 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
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...
219
            $this->deleteMessage();
220
221
            return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
222
        }
223
224
        $this->classCategoryRepository->toggleVisibility($TargetClassCategory);
225
226
        log_info('規格分類表示変更完了', [$id]);
227
228
        $event = new EventArgs(
229
            [
230
                'ClassName' => $ClassName,
231
                'TargetClassCategory' => $TargetClassCategory,
232
            ],
233
            $request
234
        );
235
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
236
237 View Code Duplication
        if ($TargetClassCategory->isVisible()) {
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...
238
            $this->addSuccess(trans('admin.common.to_show_complete', ['%name%' => $TargetClassCategory->getName()]), 'admin');
239
        } else {
240
            $this->addSuccess(trans('admin.common.to_hide_complete', ['%name%' => $TargetClassCategory->getName()]), 'admin');
241
        }
242
243
        return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
244
    }
245
246
    /**
247
     * @Route("/%eccube_admin_route%/product/class_category/sort_no/move", name="admin_product_class_category_sort_no_move", methods={"POST"})
248
     */
249 View Code Duplication
    public function moveSortNo(Request $request)
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...
250
    {
251
        if (!$request->isXmlHttpRequest()) {
252
            throw new BadRequestHttpException();
253
        }
254
255
        if ($this->isTokenValid()) {
256
            $sortNos = $request->request->all();
257
            foreach ($sortNos as $categoryId => $sortNo) {
258
                $ClassCategory = $this->classCategoryRepository
259
                    ->find($categoryId);
260
                $ClassCategory->setSortNo($sortNo);
261
                $this->entityManager->persist($ClassCategory);
0 ignored issues
show
Bug introduced by
It seems like $ClassCategory defined by $this->classCategoryRepository->find($categoryId) on line 258 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::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...
262
            }
263
            $this->entityManager->flush();
264
265
            return new Response('Successful');
266
        }
267
    }
268
}
269