Completed
Pull Request — experimental/sf (#3456)
by Kiyotaka
87:43 queued 81:34
created

DeliveryController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
dl 0
loc 151
ccs 58
cts 60
cp 0.9667
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 8 1
B edit() 0 75 6
A delete() 0 28 2
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\Method;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
31
class DeliveryController extends AbstractController
32
{
33
    /**
34
     * @var BaseInfo
35
     */
36
    protected $BaseInfo;
37
38
    /**
39
     * @var CustomerAddressRepository
40
     */
41
    protected $customerAddressRepository;
42
43 7
    public function __construct(BaseInfoRepository $baseInfoRepository, CustomerAddressRepository $customerAddressRepository)
44
    {
45 7
        $this->BaseInfo = $baseInfoRepository->get();
46 7
        $this->customerAddressRepository = $customerAddressRepository;
47
    }
48
49
    /**
50
     * お届け先一覧画面.
51
     *
52
     * @Route("/mypage/delivery", name="mypage_delivery")
53
     * @Template("Mypage/delivery.twig")
54
     */
55 1
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
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...
56
    {
57 1
        $Customer = $this->getUser();
58
59
        return [
60 1
            'Customer' => $Customer,
61
        ];
62
    }
63
64
    /**
65
     * お届け先編集画面.
66
     *
67
     * @Route("/mypage/delivery/new", name="mypage_delivery_new")
68
     * @Route("/mypage/delivery/{id}/edit", name="mypage_delivery_edit", requirements={"id" = "\d+"})
69
     * @Template("Mypage/delivery_edit.twig")
70
     */
71 4
    public function edit(Request $request, $id = null)
72
    {
73 4
        $Customer = $this->getUser();
74
75
        // 配送先住所最大値判定
76
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
77 4
        if (is_null($id)) {
78 2
            $addressCurrNum = count($Customer->getCustomerAddresses());
79 2
            $addressMax = $this->eccubeConfig['eccube_deliv_addr_max'];
80 2
            if ($addressCurrNum >= $addressMax) {
81
                throw new NotFoundHttpException(trans('delivery.text.error.max_delivery_address'));
82
            }
83
        }
84
85 4
        $CustomerAddress = $this->customerAddressRepository->findOrCreateByCustomerAndId($Customer, $id);
0 ignored issues
show
Documentation introduced by
$Customer is of type null|object, but the function expects a object<Eccube\Entity\Customer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Eccube\Repository\Custom...CreateByCustomerAndId() has been deprecated with message: 呼び出し元で制御する

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
87 4
        $parentPage = $request->get('parent_page', null);
88
89
        // 正しい遷移かをチェック
90
        $allowdParents = [
91 4
            $this->generateUrl('mypage_delivery'),
92 4
            $this->generateUrl('shopping_redirect_to'),
93
        ];
94
95
        // 遷移が正しくない場合、デフォルトであるマイページの配送先追加の画面を設定する
96 4
        if (!in_array($parentPage, $allowdParents)) {
97
            // @deprecated 使用されていないコード
98 4
            $parentPage = $this->generateUrl('mypage_delivery');
99
        }
100
101 4
        $builder = $this->formFactory
102 4
            ->createBuilder(CustomerAddressType::class, $CustomerAddress);
103
104 4
        $event = new EventArgs(
105
            [
106 4
                'builder' => $builder,
107 4
                'Customer' => $Customer,
108 4
                'CustomerAddress' => $CustomerAddress,
109
            ],
110 4
            $request
111
        );
112 4
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_INITIALIZE, $event);
113
114 4
        $form = $builder->getForm();
115 4
        $form->handleRequest($request);
116
117 4
        if ($form->isSubmitted() && $form->isValid()) {
118 2
            log_info('お届け先登録開始', [$id]);
119
120 2
            $this->entityManager->persist($CustomerAddress);
121 2
            $this->entityManager->flush();
122
123 2
            log_info('お届け先登録完了', [$id]);
124
125 2
            $event = new EventArgs(
126
                [
127 2
                    'form' => $form,
128 2
                    'Customer' => $Customer,
129 2
                    'CustomerAddress' => $CustomerAddress,
130
                ],
131 2
                $request
132
            );
133 2
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_COMPLETE, $event);
134
135 2
            $this->addSuccess('mypage.delivery.add.complete');
136
137 2
            return $this->redirect($this->generateUrl('mypage_delivery'));
138
        }
139
140
        return [
141 2
            'form' => $form->createView(),
142 2
            'parentPage' => $parentPage,
143 2
            'BaseInfo' => $this->BaseInfo,
144
        ];
145
    }
146
147
    /**
148
     * お届け先を削除する.
149
     *
150
     * @Method("DELETE")
151
     * @Route("/mypage/delivery/{id}/delete", name="mypage_delivery_delete")
152
     */
153 1
    public function delete(Request $request, CustomerAddress $CustomerAddress)
154
    {
155 1
        $this->isTokenValid();
156
157 1
        log_info('お届け先削除開始', [$CustomerAddress->getId()]);
158
159 1
        $Customer = $this->getUser();
160
161 1
        if ($Customer->getId() != $CustomerAddress->getCustomer()->getId()) {
162
            throw new BadRequestHttpException();
163
        }
164
165 1
        $this->customerAddressRepository->delete($CustomerAddress);
166
167 1
        $event = new EventArgs(
168
            [
169 1
                'Customer' => $Customer,
170 1
                'CustomerAddress' => $CustomerAddress,
171 1
            ], $request
172
        );
173 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_DELETE_COMPLETE, $event);
174
175 1
        $this->addSuccess('mypage.address.delete.complete');
176
177 1
        log_info('お届け先削除完了', [$CustomerAddress->getId()]);
178
179 1
        return $this->redirect($this->generateUrl('mypage_delivery'));
180
    }
181
}
182