Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

Controller/Admin/Product/ClassNameController.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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\Entity\ClassName;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Form\Type\Admin\ClassNameType;
21
use Eccube\Repository\ClassNameRepository;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
use Symfony\Component\Routing\Annotation\Route;
28
29
class ClassNameController extends AbstractController
30
{
31
    /**
32
     * @var ClassNameRepository
33
     */
34
    protected $classNameRepository;
35
36
    /**
37
     * ClassNameController constructor.
38
     *
39
     * @param ClassNameRepository $classNameRepository
40
     */
41 7
    public function __construct(ClassNameRepository $classNameRepository)
42
    {
43 7
        $this->classNameRepository = $classNameRepository;
44
    }
45
46
    /**
47
     * @Route("/%eccube_admin_route%/product/class_name", name="admin_product_class_name")
48
     * @Route("/%eccube_admin_route%/product/class_name/{id}/edit", requirements={"id" = "\d+"}, name="admin_product_class_name_edit")
49
     * @Template("@admin/Product/class_name.twig")
50
     */
51 5
    public function index(Request $request, $id = null)
52
    {
53 5
        if ($id) {
54 2
            $TargetClassName = $this->classNameRepository->find($id);
55 2
            if (!$TargetClassName) {
56 2
                throw new NotFoundHttpException();
57
            }
58
        } else {
59 3
            $TargetClassName = new \Eccube\Entity\ClassName();
60
        }
61
62 5
        $builder = $this->formFactory
63 5
            ->createBuilder(ClassNameType::class, $TargetClassName);
64
65 5
        $event = new EventArgs(
66
            [
67 5
                'builder' => $builder,
68 5
                'TargetClassName' => $TargetClassName,
69
            ],
70 5
            $request
71
        );
72 5
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_INDEX_INITIALIZE, $event);
73
74 5
        $ClassNames = $this->classNameRepository->getList();
75
76
        /**
77
         * 編集用フォーム
78
         */
79 5
        $forms = [];
80 5
        foreach ($ClassNames as $ClassName) {
81 5
            $id = $ClassName->getId();
82 5
            $forms[$id] = $this->formFactory->createNamed('class_name_'.$id, ClassNameType::class, $ClassName);
83
        }
84
85 5
        $form = $builder->getForm();
86
87 5
        if ($request->getMethod() === 'POST') {
88 2
            $form->handleRequest($request);
89 2
            if ($form->isSubmitted() && $form->isValid()) {
90 2
                log_info('商品規格登録開始', [$id]);
91
92 2
                $this->classNameRepository->save($TargetClassName);
93
94 2
                log_info('商品規格登録完了', [$id]);
95
96 2
                $event = new EventArgs(
97
                    [
98 2
                        'form' => $form,
99 2
                        'TargetClassName' => $TargetClassName,
100
                    ],
101 2
                    $request
102
                );
103 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_INDEX_COMPLETE, $event);
104
105 2
                $this->addSuccess('admin.common.save_complete', 'admin');
106
107 2
                return $this->redirectToRoute('admin_product_class_name');
108
            }
109
110
            /*
111
             * 編集処理
112
             */
113 View Code Duplication
            foreach ($forms as $editForm) {
114
                $editForm->handleRequest($request);
115
                if ($editForm->isSubmitted() && $editForm->isValid()) {
116
                    $this->classNameRepository->save($editForm->getData());
117
118
                    $this->addSuccess('admin.common.save_complete', 'admin');
119
120
                    return $this->redirectToRoute('admin_product_class_name');
121
                }
122
            }
123
        }
124 3
        $formViews = [];
125 3
        foreach ($forms as $key => $value) {
126 3
            $formViews[$key] = $value->createView();
127
        }
128
129
        return [
130 3
            'form' => $form->createView(),
131 3
            'ClassNames' => $ClassNames,
132 3
            'TargetClassName' => $TargetClassName,
133 3
            'forms' => $formViews,
134
        ];
135
    }
136
137
    /**
138
     * @Route("/%eccube_admin_route%/product/class_name/{id}/delete", requirements={"id" = "\d+"}, name="admin_product_class_name_delete", methods={"DELETE"})
139
     */
140 View Code Duplication
    public function delete(Request $request, ClassName $ClassName)
141 1
    {
142
        $this->isTokenValid();
143 1
144
        log_info('商品規格削除開始', [$ClassName->getId()]);
145 1
146
        try {
147
            $this->classNameRepository->delete($ClassName);
148 1
149
            $event = new EventArgs(['ClassName' => $ClassName], $request);
150 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_NAME_DELETE_COMPLETE, $event);
151 1
152
            $this->addSuccess('admin.common.delete_complete', 'admin');
153 1
154
            log_info('商品規格削除完了', [$ClassName->getId()]);
155 1
        } catch (\Exception $e) {
156
            $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $ClassName->getName()]);
157
            $this->addError($message, 'admin');
158
159
            log_error('商品規格削除エラー', [$ClassName->getId(), $e]);
160
        }
161
162
        return $this->redirectToRoute('admin_product_class_name');
163 1
    }
164
165
    /**
166
     * @Route("/%eccube_admin_route%/product/class_name/sort_no/move", name="admin_product_class_name_sort_no_move", methods={"POST"})
167
     */
168 View Code Duplication
    public function moveSortNo(Request $request)
169
    {
170 1
        if (!$request->isXmlHttpRequest()) {
171
            throw new BadRequestHttpException();
172 1
        }
173 1
174 1
        if ($this->isTokenValid()) {
175 1
            $sortNos = $request->request->all();
176 1
            foreach ($sortNos as $classNameId => $sortNo) {
177 1
                $ClassName = $this->classNameRepository
178 1
                    ->find($classNameId);
179
                $ClassName->setSortNo($sortNo);
180 1
                $this->entityManager->persist($ClassName);
0 ignored issues
show
It seems like $ClassName defined by $this->classNameRepository->find($classNameId) on line 177 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...
181
            }
182
            $this->entityManager->flush();
183 1
184
            return new Response();
185
        }
186
    }
187
}
188