Completed
Push — dev/store-tests ( 3e9e56...714865 )
by Kiyotaka
05:47
created

MailController::mailAll()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 10
nop 1
dl 0
loc 99
rs 7.0884
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Order;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\MailHistory;
18
use Eccube\Entity\Order;
19
use Eccube\Event\EccubeEvents;
20
use Eccube\Event\EventArgs;
21
use Eccube\Form\Type\Admin\OrderMailType;
22
use Eccube\Repository\MailHistoryRepository;
23
use Eccube\Repository\OrderRepository;
24
use Eccube\Service\MailService;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use Symfony\Component\Routing\Annotation\Route;
30
use Twig\Environment;
31
32
class MailController extends AbstractController
33
{
34
    /**
35
     * @var MailService
36
     */
37
    protected $mailService;
38
39
    /**
40
     * @var MailHistoryRepository
41
     */
42
    protected $mailHistoryRepository;
43
44
    /**
45
     * @var OrderRepository
46
     */
47
    protected $orderRepository;
48
    /**
49
     * @var Environment
50
     */
51
    protected $twig;
52
53
    /**
54
     * MailController constructor.
55
     *
56
     * @param MailService $mailService
57
     * @param MailHistoryRepository $mailHistoryRepository
58
     * @param OrderRepository $orderRepository
59
     * @param twig $twig
60
     */
61
    public function __construct(
62
        MailService $mailService,
63
        MailHistoryRepository $mailHistoryRepository,
64
        OrderRepository $orderRepository,
65
        Environment $twig
66
    ) {
67
        $this->mailService = $mailService;
68
        $this->mailHistoryRepository = $mailHistoryRepository;
69
        $this->orderRepository = $orderRepository;
70
        $this->twig = $twig;
71
    }
72
73
    /**
74
     * @Route("/%eccube_admin_route%/order/{id}/mail", requirements={"id" = "\d+"}, name="admin_order_mail")
75
     * @Template("@admin/Order/mail.twig")
76
     */
77
    public function index(Request $request, Order $Order)
78
    {
79
        $MailHistories = $this->mailHistoryRepository->findBy(['Order' => $Order]);
80
81
        $builder = $this->formFactory->createBuilder(OrderMailType::class);
82
83
        $event = new EventArgs(
84
            [
85
                'builder' => $builder,
86
                'Order' => $Order,
87
                'MailHistories' => $MailHistories,
88
            ],
89
            $request
90
        );
91
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_INITIALIZE, $event);
92
93
        $form = $builder->getForm();
94
95
        if ('POST' === $request->getMethod()) {
96
            $form->handleRequest($request);
97
98
            $mode = $request->get('mode');
99
100
            $body = null;
101
            // テンプレート変更の場合は. バリデーション前に内容差し替え.
102
            switch ($mode) {
103
                case 'change':
104
                    if ($form->get('template')->isValid()) {
105
                        /** @var $data \Eccube\Entity\MailTemplate */
106
                        $MailTemplate = $form->get('template')->getData();
107
                        $data = $form->getData();
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
109
                        if ($MailTemplate) {
110
                            $twig = $MailTemplate->getFileName();
111
                            if (!$twig) {
112
                                $twig = 'Mail/order.twig';
113
                            }
114
115
                            // 本文確認用
116
                            $body = $this->createBody($Order, $twig);
117
                        }
118
119
                        $form = $builder->getForm();
120
                        $event = new EventArgs(
121
                            [
122
                                'form' => $form,
123
                                'Order' => $Order,
124
                                'MailTemplate' => $MailTemplate,
125
                            ],
126
                            $request
127
                        );
128
                        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CHANGE, $event);
129
                        $form->get('template')->setData($MailTemplate);
130
                        if ($MailTemplate) {
131
                            $form->get('mail_subject')->setData($MailTemplate->getMailSubject());
132
                        }
133
                        $form->get('tpl_data')->setData($body);
134
                    }
135
                    break;
136
                case 'confirm':
137
                    if ($form->isValid()) {
138
                        $builder->setAttribute('freeze', true);
139
                        $builder->setAttribute('freeze_display_text', false);
140
                        $form = $builder->getForm();
141
                        $form->handleRequest($request);
142
143
                        return $this->render('@admin/Order/mail_confirm.twig', [
144
                            'form' => $form->createView(),
145
                            'Order' => $Order,
146
                            'MailHistories' => $MailHistories,
147
                        ]);
148
                    }
149
                    break;
150
                case 'complete':
151
                    if ($form->isValid()) {
152
                        $data = $form->getData();
153
                        $data['tpl_data'] = $form->get('tpl_data')->getData();
154
155
                        // メール送信
156
                        $message = $this->mailService->sendAdminOrderMail($Order, $data);
157
158
                        // 送信履歴を保存.
159
                        $MailTemplate = $form->get('template')->getData();
160
                        $MailHistory = new MailHistory();
161
                        $MailHistory
162
                            ->setMailSubject($message->getSubject())
163
                            ->setMailBody($message->getBody())
164
                            ->setSendDate(new \DateTime())
165
                            ->setOrder($Order);
166
167
                        $this->entityManager->persist($MailHistory);
168
                        $this->entityManager->flush($MailHistory);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $MailHistory.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
169
170
                        $event = new EventArgs(
171
                            [
172
                                'form' => $form,
173
                                'Order' => $Order,
174
                                'MailTemplate' => $MailTemplate,
175
                                'MailHistory' => $MailHistory,
176
                            ],
177
                            $request
178
                        );
179
                        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_COMPLETE, $event);
180
181
                        $this->addSuccess('admin.order.mail_send_complete', 'admin');
182
183
                        return $this->redirectToRoute('admin_order_page', ['page_no' => $this->session->get('eccube.admin.order.search.page_no', 1)]);
184
                    }
185
                    break;
186
                default:
187
                    break;
188
            }
189
        }
190
191
        return [
192
            'form' => $form->createView(),
193
            'Order' => $Order,
194
            'MailHistories' => $MailHistories,
195
        ];
196
    }
197
198
    /**
199
     * @Route("/%eccube_admin_route%/order/mail/view", name="admin_order_mail_view")
200
     * @Template("@admin/Order/mail_view.twig")
201
     */
202
    public function view(Request $request)
203
    {
204
        if (!$request->isXmlHttpRequest()) {
205
            throw new BadRequestHttpException();
206
        }
207
208
        $id = $request->get('id');
209
        $MailHistory = $this->mailHistoryRepository->find($id);
210
211
        if (null === $MailHistory) {
212
            throw new NotFoundHttpException();
213
        }
214
215
        $event = new EventArgs(
216
            [
217
                'MailHistory' => $MailHistory,
218
            ],
219
            $request
220
        );
221
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_VIEW_COMPLETE, $event);
222
223
        return [
224
            'mail_subject' => $MailHistory->getMailSubject(),
225
            'body' => $MailHistory->getMailBody(),
226
            'html_body' => $MailHistory->getMailHtmlBody(),
227
        ];
228
    }
229
230
    private function createBody($Order, $twig = 'Mail/order.twig')
231
    {
232
        return $this->renderView($twig, [
233
            'Order' => $Order,
234
        ]);
235
    }
236
}
237