Completed
Push — 4.0 ( b48f64...137622 )
by chihiro
20:21 queued 10s
created

Eccube/Controller/Mypage/DeliveryController.php (1 issue)

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) 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\Mypage;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\CustomerAddress;
19
use Eccube\Event\EccubeEvents;
20
use Eccube\Event\EventArgs;
21
use Eccube\Form\Type\Front\CustomerAddressType;
22
use Eccube\Repository\BaseInfoRepository;
23
use Eccube\Repository\CustomerAddressRepository;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
use Symfony\Component\Routing\Annotation\Route;
29
30
class DeliveryController extends AbstractController
31
{
32
    /**
33
     * @var BaseInfo
34
     */
35
    protected $BaseInfo;
36
37
    /**
38
     * @var CustomerAddressRepository
39
     */
40
    protected $customerAddressRepository;
41
42
    public function __construct(BaseInfoRepository $baseInfoRepository, CustomerAddressRepository $customerAddressRepository)
43 7
    {
44
        $this->BaseInfo = $baseInfoRepository->get();
45 7
        $this->customerAddressRepository = $customerAddressRepository;
46 7
    }
47
48
    /**
49
     * お届け先一覧画面.
50
     *
51
     * @Route("/mypage/delivery", name="mypage_delivery")
52
     * @Template("Mypage/delivery.twig")
53
     */
54
    public function index(Request $request)
0 ignored issues
show
The parameter $request 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...
55 1
    {
56
        $Customer = $this->getUser();
57 1
58
        return [
59
            'Customer' => $Customer,
60 1
        ];
61
    }
62
63
    /**
64
     * お届け先編集画面.
65
     *
66
     * @Route("/mypage/delivery/new", name="mypage_delivery_new")
67
     * @Route("/mypage/delivery/{id}/edit", name="mypage_delivery_edit", requirements={"id" = "\d+"})
68
     * @Template("Mypage/delivery_edit.twig")
69
     */
70
    public function edit(Request $request, $id = null)
71 4
    {
72
        $Customer = $this->getUser();
73 4
74
        // 配送先住所最大値判定
75
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
76 View Code Duplication
        if (is_null($id)) {
77 4
            $addressCurrNum = count($Customer->getCustomerAddresses());
78 2
            $addressMax = $this->eccubeConfig['eccube_deliv_addr_max'];
79 2
            if ($addressCurrNum >= $addressMax) {
80 2
                throw new NotFoundHttpException();
81
            }
82
            $CustomerAddress = new CustomerAddress();
83
            $CustomerAddress->setCustomer($Customer);
84
        } else {
85 4
            $CustomerAddress = $this->customerAddressRepository->findOneBy(
86
                [
87 4
                    'id' => $id,
88
                    'Customer' => $Customer,
89
                ]
90
            );
91 4
            if (!$CustomerAddress) {
92 4
                throw new NotFoundHttpException();
93
            }
94
        }
95
96 4
        $parentPage = $request->get('parent_page', null);
97
98 4
        // 正しい遷移かをチェック
99
        $allowedParents = [
100
            $this->generateUrl('mypage_delivery'),
101 4
            $this->generateUrl('shopping_redirect_to'),
102 4
        ];
103
104 4
        // 遷移が正しくない場合、デフォルトであるマイページの配送先追加の画面を設定する
105
        if (!in_array($parentPage, $allowedParents)) {
106 4
            // @deprecated 使用されていないコード
107 4
            $parentPage = $this->generateUrl('mypage_delivery');
108 4
        }
109
110 4
        $builder = $this->formFactory
111
            ->createBuilder(CustomerAddressType::class, $CustomerAddress);
112 4
113
        $event = new EventArgs(
114 4
            [
115 4
                'builder' => $builder,
116
                'Customer' => $Customer,
117 4
                'CustomerAddress' => $CustomerAddress,
118 2
            ],
119
            $request
120 2
        );
121 2
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_INITIALIZE, $event);
122
123 2
        $form = $builder->getForm();
124
        $form->handleRequest($request);
125 2
126
        if ($form->isSubmitted() && $form->isValid()) {
127 2
            log_info('お届け先登録開始', [$id]);
128 2
129 2
            $this->entityManager->persist($CustomerAddress);
130
            $this->entityManager->flush();
131 2
132
            log_info('お届け先登録完了', [$id]);
133 2
134
            $event = new EventArgs(
135 2
                [
136
                    'form' => $form,
137 2
                    'Customer' => $Customer,
138
                    'CustomerAddress' => $CustomerAddress,
139
                ],
140
                $request
141 2
            );
142 2
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_COMPLETE, $event);
143 2
144
            $this->addSuccess('mypage.delivery.add.complete');
145
146
            return $this->redirect($this->generateUrl('mypage_delivery'));
147
        }
148
149
        return [
150
            'form' => $form->createView(),
151
            'parentPage' => $parentPage,
152
            'BaseInfo' => $this->BaseInfo,
153 1
        ];
154
    }
155 1
156
    /**
157 1
     * お届け先を削除する.
158
     *
159 1
     * @Route("/mypage/delivery/{id}/delete", name="mypage_delivery_delete", methods={"DELETE"})
160
     */
161 1
    public function delete(Request $request, CustomerAddress $CustomerAddress)
162
    {
163
        $this->isTokenValid();
164
165 1
        log_info('お届け先削除開始', [$CustomerAddress->getId()]);
166
167 1
        $Customer = $this->getUser();
168
169 1
        if ($Customer->getId() != $CustomerAddress->getCustomer()->getId()) {
170 1
            throw new BadRequestHttpException();
171 1
        }
172
173 1
        $this->customerAddressRepository->delete($CustomerAddress);
174
175 1
        $event = new EventArgs(
176
            [
177 1
                'Customer' => $Customer,
178
                'CustomerAddress' => $CustomerAddress,
179 1
            ], $request
180
        );
181
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_DELETE_COMPLETE, $event);
182
183
        $this->addSuccess('mypage.address.delete.complete');
184
185
        log_info('お届け先削除完了', [$CustomerAddress->getId()]);
186
187
        return $this->redirect($this->generateUrl('mypage_delivery'));
188
    }
189
}
190