Completed
Pull Request — 4.0 (#3795)
by Ryo
05:44
created

MailController::mailAll()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 8.4753

Importance

Changes 0
Metric Value
cc 7
nc 10
nop 1
dl 0
loc 99
ccs 31
cts 45
cp 0.6889
crap 8.4753
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 5
     *
56
     * @param MailService $mailService
57
     * @param MailHistoryRepository $mailHistoryRepository
58
     * @param OrderRepository $orderRepository
59
     * @param twig $twig
60 5
     */
61 5
    public function __construct(
62 5
        MailService $mailService,
63
        MailHistoryRepository $mailHistoryRepository,
64
        OrderRepository $orderRepository,
65
        Environment $twig
66
    ) {
67
        $this->mailService = $mailService;
68
        $this->mailHistoryRepository = $mailHistoryRepository;
69 2
        $this->orderRepository = $orderRepository;
70
        $this->twig = $twig;
71 2
    }
72
73 2
    /**
74
     * @Route("/%eccube_admin_route%/order/{id}/mail", requirements={"id" = "\d+"}, name="admin_order_mail")
75 2
     * @Template("@admin/Order/mail.twig")
76
     */
77 2
    public function index(Request $request, Order $Order)
78 2
    {
79 2
        $MailHistories = $this->mailHistoryRepository->findBy(['Order' => $Order]);
80
81 2
        $builder = $this->formFactory->createBuilder(OrderMailType::class);
82
83 2
        $event = new EventArgs(
84
            [
85 2
                'builder' => $builder,
86
                'Order' => $Order,
87
                'MailHistories' => $MailHistories,
88 2
            ],
89
            $request
90 2
        );
91 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_INITIALIZE, $event);
92
93 1
        $form = $builder->getForm();
94
95
        if ('POST' === $request->getMethod()) {
96 1
            $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 1
                            ],
126 1
                            $request
127
                        );
128 1
                        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CHANGE, $event);
129
                        $form->get('template')->setData($MailTemplate);
130 1
                        if ($MailTemplate) {
131 1
                            $form->get('mail_subject')->setData($MailTemplate->getMailSubject());
132
                        }
133
                        $form->get('tpl_data')->setData($body);
134
                    }
135
                    break;
136 1
                case 'confirm':
137
                    if ($form->isValid()) {
138
                        $builder->setAttribute('freeze', true);
139 1
                        $builder->setAttribute('freeze_display_text', false);
140 1
                        $form = $builder->getForm();
141
                        $form->handleRequest($request);
142 1
143 1
                        return $this->render('@admin/Order/mail_confirm.twig', [
144 1
                            'form' => $form->createView(),
145 1
                            'Order' => $Order,
146
                            'MailHistories' => $MailHistories,
147 1
                        ]);
148 1
                    }
149
                    break;
150 1
                case 'complete':
151
                    if ($form->isValid()) {
152 1
                        $data = $form->getData();
153 1
                        $data['tpl_data'] = $form->get('tpl_data')->getData();
154 1
155 1
                        // メール送信
156
                        $message = $this->mailService->sendAdminOrderMail($Order, $data);
157 1
158
                        // 送信履歴を保存.
159 1
                        $MailTemplate = $form->get('template')->getData();
160
                        $MailHistory = new MailHistory();
161 1
                        $MailHistory
162
                            ->setMailSubject($message->getSubject())
163
                            ->setMailBody($message->getBody())
164
                            ->setSendDate(new \DateTime())
165
                            ->setOrder($Order);
166
167 1
                        $this->entityManager->persist($MailHistory);
168 1
                        $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 1
170 1
                        $event = new EventArgs(
171
                            [
172
                                'form' => $form,
173
                                'Order' => $Order,
174
                                'MailTemplate' => $MailTemplate,
175
                                'MailHistory' => $MailHistory,
176
                            ],
177
                            $request
178 1
                        );
179
                        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_COMPLETE, $event);
180 1
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 1
                    }
185 1
                    break;
186
                default:
187 1
                    break;
188
            }
189
        }
190
191 1
        return [
192
            'form' => $form->createView(),
193 1
            'Order' => $Order,
194
            'MailHistories' => $MailHistories,
195 1
        ];
196
    }
197 1
198
    /**
199
     * @Route("/%eccube_admin_route%/order/mail/view", name="admin_order_mail_view")
200 1
     * @Template("@admin/Order/mail_view.twig")
201 1
     */
202
    public function view(Request $request)
203
    {
204
        if (!$request->isXmlHttpRequest()) {
205
            throw new BadRequestHttpException();
206
        }
207
208
        $id = $request->get('id');
209 2
        $MailHistory = $this->mailHistoryRepository->find($id);
210
211 2
        if (null === $MailHistory) {
212
            throw new NotFoundHttpException();
213 2
        }
214
215 2
        $event = new EventArgs(
216
            [
217 2
                'MailHistory' => $MailHistory,
218
            ],
219 2
            $request
220
        );
221 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_VIEW_COMPLETE, $event);
222
223 2
        return [
224 2
            'mail_subject' => $MailHistory->getMailSubject(),
225 1
            'body' => $MailHistory->getMailBody(),
226
            'html_body' => $MailHistory->getMailHtmlBody(),
227 1
        ];
228
    }
229 1
230
    private function createBody($Order, $twig = 'Mail/order.twig')
231
    {
232 1
        return $this->renderView($twig, [
233
            'Order' => $Order,
234
        ]);
235
    }
236
}
237