Failed Conditions
Pull Request — 4.0 (#3650)
by chihiro
06:14
created

MailService::sendOrderMail()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 58
ccs 30
cts 30
cp 1
crap 2
rs 8.9163
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\Service;
15
16
use Eccube\Common\EccubeConfig;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\Customer;
19
use Eccube\Entity\MailHistory;
20
use Eccube\Entity\MailTemplate;
21
use Eccube\Entity\Order;
22
use Eccube\Entity\OrderItem;
23
use Eccube\Entity\Shipping;
24
use Eccube\Event\EccubeEvents;
25
use Eccube\Event\EventArgs;
26
use Eccube\Repository\BaseInfoRepository;
27
use Eccube\Repository\MailHistoryRepository;
28
use Eccube\Repository\MailTemplateRepository;
29
use Symfony\Component\EventDispatcher\EventDispatcher;
30
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31
32
class MailService
33
{
34
    /**
35
     * @var \Swift_Mailer
36
     */
37
    protected $mailer;
38
39
    /**
40
     * @var MailTemplateRepository
41
     */
42
    protected $mailTemplateRepository;
43
44
    /**
45
     * @var MailHistoryRepository
46
     */
47
    private $mailHistoryRepository;
48
49
    /**
50
     * @var EventDispatcher
51
     */
52
    protected $eventDispatcher;
53
54
    /**
55
     * @var BaseInfo
56
     */
57
    protected $BaseInfo;
58
59
    /**
60
     * @var EccubeConfig
61
     */
62
    protected $eccubeConfig;
63
64
    /**
65
     * @var \Twig_Environment
66
     */
67
    protected $twig;
68
69
    /**
70
     * MailService constructor.
71
     *
72
     * @param \Swift_Mailer $mailer
73
     * @param MailTemplateRepository $mailTemplateRepository
74
     * @param MailHistoryRepository $mailHistoryRepository
75
     * @param BaseInfoRepository $baseInfoRepository
76
     * @param EventDispatcherInterface $eventDispatcher
77
     * @param \Twig_Environment $twig
78
     * @param EccubeConfig $eccubeConfig
79
     */
80 140
    public function __construct(
81
        \Swift_Mailer $mailer,
82
        MailTemplateRepository $mailTemplateRepository,
83
        MailHistoryRepository $mailHistoryRepository,
84
        BaseInfoRepository $baseInfoRepository,
85
        EventDispatcherInterface $eventDispatcher,
86
        \Twig_Environment $twig,
87
        EccubeConfig $eccubeConfig
88
    ) {
89 140
        $this->mailer = $mailer;
90 140
        $this->mailTemplateRepository = $mailTemplateRepository;
91 140
        $this->mailHistoryRepository = $mailHistoryRepository;
92 140
        $this->BaseInfo = $baseInfoRepository->get();
93 140
        $this->eventDispatcher = $eventDispatcher;
0 ignored issues
show
Documentation Bug introduced by
$eventDispatcher is of type object<Symfony\Component...entDispatcherInterface>, but the property $eventDispatcher was declared to be of type object<Symfony\Component...atcher\EventDispatcher>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
94 140
        $this->eccubeConfig = $eccubeConfig;
95 140
        $this->twig = $twig;
96
    }
97
98
    /**
99
     * Send customer confirm mail.
100
     *
101
     * @param $Customer 会員情報
102
     * @param string $activateUrl アクティベート用url
103
     */
104 2 View Code Duplication
    public function sendCustomerConfirmMail(\Eccube\Entity\Customer $Customer, $activateUrl)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 2
        log_info('仮会員登録メール送信開始');
107
108 2
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_confirm_mail_template_id']);
109
110 2
        $body = $this->twig->render($MailTemplate->getFileName(), [
111 2
            'Customer' => $Customer,
112 2
            'BaseInfo' => $this->BaseInfo,
113 2
            'activateUrl' => $activateUrl,
114 2
        ]);
115 2
116
        $message = (new \Swift_Message())
117
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
118 2
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
119 2
            ->setTo([$Customer->getEmail()])
120 2
            ->setBcc($this->BaseInfo->getEmail01())
121 2
            ->setReplyTo($this->BaseInfo->getEmail03())
122 2
            ->setReturnPath($this->BaseInfo->getEmail04());
123 2
124 2
        // HTMLテンプレートが存在する場合
125 2
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
126
        if (!is_null($htmlFileName)) {
127 2
            $htmlBody = $this->twig->render($htmlFileName, [
128
                'Customer' => $Customer,
129 2
                'BaseInfo' => $this->BaseInfo,
130 2
                'activateUrl' => $activateUrl,
131 2
            ]);
132 2
133
            $message
134 2
                ->setContentType('text/plain; charset=UTF-8')
135
                ->setBody($body, 'text/plain')
136 2
                ->addPart($htmlBody, 'text/html');
137
        } else {
138 2
            $message->setBody($body);
139
        }
140 2
141
        $event = new EventArgs(
142 2
            [
143
                'message' => $message,
144
                'Customer' => $Customer,
145
                'BaseInfo' => $this->BaseInfo,
146
                'activateUrl' => $activateUrl,
147
            ],
148
            null
149
        );
150 2
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CUSTOMER_CONFIRM, $event);
151
152 2
        $count = $this->mailer->send($message, $failures);
153
154 2
        log_info('仮会員登録メール送信完了', ['count' => $count]);
155
156 2
        return $count;
157 2
    }
158 2
159 2
    /**
160 2
     * Send customer complete mail.
161
     *
162
     * @param $Customer 会員情報
163 2
     */
164 2 View Code Duplication
    public function sendCustomerCompleteMail(\Eccube\Entity\Customer $Customer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165 2
    {
166 2
        log_info('会員登録完了メール送信開始');
167 2
168 2
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_complete_mail_template_id']);
169 2
170 2
        $body = $this->twig->render($MailTemplate->getFileName(), [
171
            'Customer' => $Customer,
172 2
            'BaseInfo' => $this->BaseInfo,
173
        ]);
174 2
175 2
        $message = (new \Swift_Message())
176 2
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
177
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
178 2
            ->setTo([$Customer->getEmail()])
179
            ->setBcc($this->BaseInfo->getEmail01())
180 2
            ->setReplyTo($this->BaseInfo->getEmail03())
181
            ->setReturnPath($this->BaseInfo->getEmail04());
182 2
183
        // HTMLテンプレートが存在する場合
184 2
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
185
        if (!is_null($htmlFileName)) {
186 2
            $htmlBody = $this->twig->render($htmlFileName, [
187
                'Customer' => $Customer,
188
                'BaseInfo' => $this->BaseInfo,
189
            ]);
190
191
            $message
192
                ->setContentType('text/plain; charset=UTF-8')
193
                ->setBody($body, 'text/plain')
194
                ->addPart($htmlBody, 'text/html');
195 2
        } else {
196
            $message->setBody($body);
197 2
        }
198
199 2
        $event = new EventArgs(
200
            [
201 2
                'message' => $message,
202 2
                'Customer' => $Customer,
203 2
                'BaseInfo' => $this->BaseInfo,
204 2
            ],
205 2
            null
206
        );
207
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CUSTOMER_COMPLETE, $event);
208 2
209 2
        $count = $this->mailer->send($message);
210 2
211 2
        log_info('会員登録完了メール送信完了', ['count' => $count]);
212 2
213 2
        return $count;
214 2
    }
215 2
216
    /**
217 2
     * Send withdraw mail.
218
     *
219 2
     * @param $Customer Customer
220 2
     * @param $email string
221 2
     */
222 2 View Code Duplication
    public function sendCustomerWithdrawMail(Customer $Customer, string $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224 2
        log_info('退会手続き完了メール送信開始');
225
226 2
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_customer_withdraw_mail_template_id']);
227
228 2
        $body = $this->twig->render($MailTemplate->getFileName(), [
229
            'Customer' => $Customer,
230 2
            'BaseInfo' => $this->BaseInfo,
231
        ]);
232 2
233
        $message = (new \Swift_Message())
234
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
235
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
236
            ->setTo([$email])
237
            ->setBcc($this->BaseInfo->getEmail01())
238
            ->setReplyTo($this->BaseInfo->getEmail03())
239
            ->setReturnPath($this->BaseInfo->getEmail04());
240 4
241
        // HTMLテンプレートが存在する場合
242 4
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
243
        if (!is_null($htmlFileName)) {
244 4
            $htmlBody = $this->twig->render($htmlFileName, [
245
                'Customer' => $Customer,
246 4
                'BaseInfo' => $this->BaseInfo,
247 4
            ]);
248 4
249 4
            $message
250 4
                ->setContentType('text/plain; charset=UTF-8')
251
                ->setBody($body, 'text/plain')
252
                ->addPart($htmlBody, 'text/html');
253
        } else {
254 4
            $message->setBody($body);
255 4
        }
256 4
257 4
        $event = new EventArgs(
258 4
            [
259 4
                'message' => $message,
260 4
                'Customer' => $Customer,
261 4
                'BaseInfo' => $this->BaseInfo,
262
                'email' => $email,
263 4
            ],
264
            null
265 4
        );
266 4
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CUSTOMER_WITHDRAW, $event);
267 4
268
        $count = $this->mailer->send($message);
269 4
270
        log_info('退会手続き完了メール送信完了', ['count' => $count]);
271 4
272
        return $count;
273 4
    }
274
275 4
    /**
276
     * Send contact mail.
277 4
     *
278
     * @param $formData お問い合わせ内容
279
     */
280 View Code Duplication
    public function sendContactMail($formData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282
        log_info('お問い合わせ受付メール送信開始');
283
284
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_contact_mail_template_id']);
285
286
        $body = $this->twig->render($MailTemplate->getFileName(), [
287
            'data' => $formData,
288
            'BaseInfo' => $this->BaseInfo,
289
        ]);
290
291
        // 問い合わせ者にメール送信
292
        $message = (new \Swift_Message())
293
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
294
            ->setFrom([$this->BaseInfo->getEmail02() => $this->BaseInfo->getShopName()])
295
            ->setTo([$formData['email']])
296
            ->setBcc($this->BaseInfo->getEmail02())
297
            ->setReplyTo($this->BaseInfo->getEmail02())
298
            ->setReturnPath($this->BaseInfo->getEmail04());
299
300
        // HTMLテンプレートが存在する場合
301 7
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
302
        if (!is_null($htmlFileName)) {
303 7
            $htmlBody = $this->twig->render($htmlFileName, [
304
                'data' => $formData,
305 7
                'BaseInfo' => $this->BaseInfo,
306
            ]);
307 7
308 7
            $message
309 7
                ->setContentType('text/plain; charset=UTF-8')
310 7
                ->setBody($body, 'text/plain')
311
                ->addPart($htmlBody, 'text/html');
312
        } else {
313 7
            $message->setBody($body);
314 7
        }
315 7
316 7
        $event = new EventArgs(
317 7
            [
318 7
                'message' => $message,
319 7
                'formData' => $formData,
320 7
                'BaseInfo' => $this->BaseInfo,
321
            ],
322 7
            null
323
        );
324 7
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CONTACT, $event);
325 7
326 7
        $count = $this->mailer->send($message);
327 7
328
        log_info('お問い合わせ受付メール送信完了', ['count' => $count]);
329 7
330
        return $count;
331 7
    }
332
333 7
    /**
334
     * Alias of sendContactMail().
335 7
     *
336
     * @param $formData お問い合わせ内容
337 7
     *
338
     * @see sendContactMail()
339
     * @deprecated since 3.0.0, to be removed in 3.1
340
     * @see https://github.com/EC-CUBE/ec-cube/issues/1315
341
     */
342
    public function sendrContactMail($formData)
343
    {
344
        $this->sendContactMail($formData);
345
    }
346 2
347
    /**
348 2
     * Send order mail.
349
     *
350
     * @param \Eccube\Entity\Order $Order 受注情報
351 2
     *
352
     * @return \Swift_Message
353 2
     */
354 2
    public function sendOrderMail(\Eccube\Entity\Order $Order)
355 2
    {
356 2
        log_info('受注メール送信開始');
357 2
358 2
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_order_mail_template_id']);
359
360
        $body = $this->twig->render($MailTemplate->getFileName(), [
361 2
            'Order' => $Order,
362 2
        ]);
363 2
364 2
        $message = (new \Swift_Message())
365 2
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
366 2
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
367 2
            ->setTo([$Order->getEmail()])
368 2
            ->setBcc($this->BaseInfo->getEmail01())
369
            ->setReplyTo($this->BaseInfo->getEmail03())
370 2
            ->setReturnPath($this->BaseInfo->getEmail04());
371
372 2
        // HTMLテンプレートが存在する場合
373 2
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
374 2
        if (!is_null($htmlFileName)) {
375 2
            $htmlBody = $this->twig->render($htmlFileName, [
376
                'Order' => $Order,
377 2
            ]);
378
379 2
            $message
380
                ->setContentType('text/plain; charset=UTF-8')
381 2
                ->setBody($body, 'text/plain')
382
                ->addPart($htmlBody, 'text/html');
383 2
        } else {
384
            $message->setBody($body);
385 2
        }
386
387
        $event = new EventArgs(
388
            [
389
                'message' => $message,
390
                'Order' => $Order,
391
                'MailTemplate' => $MailTemplate,
392
                'BaseInfo' => $this->BaseInfo,
393
            ],
394
            null
395
        );
396
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_ORDER, $event);
397
398
        $count = $this->mailer->send($message);
399
400
        $MailHistory = new MailHistory();
401 3
        $MailHistory->setMailSubject($message->getSubject())
402
            ->setMailBody($message->getBody())
403 3
            ->setOrder($Order)
404
            ->setSendDate(new \DateTime());
405 3
406 3
        $this->mailHistoryRepository->save($MailHistory);
407 3
408 3
        log_info('受注メール送信完了', ['count' => $count]);
409
410
        return $message;
411 3
    }
412 3
413 3
    /**
414 3
     * Send admin customer confirm mail.
415 3
     *
416 3
     * @param $Customer 会員情報
417 3
     * @param string $activateUrl アクティベート用url
418 3
     */
419 View Code Duplication
    public function sendAdminCustomerConfirmMail(\Eccube\Entity\Customer $Customer, $activateUrl)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
420 3
    {
421
        log_info('仮会員登録再送メール送信開始');
422 3
423 3
        /* @var $MailTemplate \Eccube\Entity\MailTemplate */
424 3
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_confirm_mail_template_id']);
425 3
426
        $body = $this->twig->render($MailTemplate->getFileName(), [
427 3
            'BaseInfo' => $this->BaseInfo,
428
            'Customer' => $Customer,
429 3
            'activateUrl' => $activateUrl,
430
        ]);
431 3
432
        $message = (new \Swift_Message())
433 3
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
434
            ->setFrom([$this->BaseInfo->getEmail03() => $this->BaseInfo->getShopName()])
435 3
            ->setTo([$Customer->getEmail()])
436
            ->setBcc($this->BaseInfo->getEmail01())
437
            ->setReplyTo($this->BaseInfo->getEmail03())
438
            ->setReturnPath($this->BaseInfo->getEmail04());
439
440
        // HTMLテンプレートが存在する場合
441
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
442
        if (!is_null($htmlFileName)) {
443
            $htmlBody = $this->twig->render($htmlFileName, [
444 1
                'BaseInfo' => $this->BaseInfo,
445
                'Customer' => $Customer,
446 1
                'activateUrl' => $activateUrl,
447
            ]);
448 1
449 1
            $message
450 1
                ->setContentType('text/plain; charset=UTF-8')
451 1
                ->setBody($body, 'text/plain')
452 1
                ->addPart($htmlBody, 'text/html');
453 1
        } else {
454 1
            $message->setBody($body);
455 1
        }
456
457
        $event = new EventArgs(
458 1
            [
459 1
                'message' => $message,
460 1
                'Customer' => $Customer,
461 1
                'BaseInfo' => $this->BaseInfo,
462 1
                'activateUrl' => $activateUrl,
463 1
            ],
464 1
            null
465 1
        );
466
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_ADMIN_CUSTOMER_CONFIRM, $event);
467 1
468
        $count = $this->mailer->send($message);
469 1
470 1
        log_info('仮会員登録再送メール送信完了', ['count' => $count]);
471 1
472 1
        return $count;
473
    }
474 1
475
    /**
476 1
     * Send admin order mail.
477
     *
478 1
     * @param Order $Order 受注情報
479
     * @param $formData 入力内容
480 1
     *
481
     * @return \Swift_Message
482 1
     *
483
     * @throws \Twig_Error_Loader
484
     * @throws \Twig_Error_Runtime
485
     * @throws \Twig_Error_Syntax
486
     */
487
    public function sendAdminOrderMail(Order $Order, $formData)
488
    {
489
        log_info('受注管理通知メール送信開始');
490
491 1
        $message = (new \Swift_Message())
492
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$formData['mail_subject'])
493 1
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
494
            ->setTo([$Order->getEmail()])
495 1
            ->setBcc($this->BaseInfo->getEmail01())
496
            ->setReplyTo($this->BaseInfo->getEmail03())
497 1
            ->setReturnPath($this->BaseInfo->getEmail04());
498 1
499 1
        if (is_null($formData['html_tpl_data'])) {
500 1
            $message->setBody($formData['tpl_data']);
501 1
        } else {
502 1
            $message
503
                ->setContentType('text/plain; charset=UTF-8')
504
                ->setBody($formData['tpl_data'], 'text/plain')
505 1
                ->addPart($formData['html_tpl_data'], 'text/html');
506 1
        }
507 1
508 1
        $event = new EventArgs(
509 1
            [
510 1
                'message' => $message,
511 1
                'Order' => $Order,
512 1
                'formData' => $formData,
513
                'BaseInfo' => $this->BaseInfo,
514 1
            ],
515
            null
516 1
        );
517 1
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_ADMIN_ORDER, $event);
518 1
519 1
        $count = $this->mailer->send($message);
520
521 1
        log_info('受注管理通知メール送信完了', ['count' => $count]);
522
523 1
        return $message;
524
    }
525 1
526
    /**
527 1
     * Send password reset notification mail.
528
     *
529 1
     * @param $Customer 会員情報
530
     * @param string $reset_url
531
     */
532
    public function sendPasswordResetNotificationMail(\Eccube\Entity\Customer $Customer, $reset_url)
533
    {
534
        log_info('パスワード再発行メール送信開始');
535
536
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_forgot_mail_template_id']);
537
        $body = $this->twig->render($MailTemplate->getFileName(), [
538
            'BaseInfo' => $this->BaseInfo,
539
            'Customer' => $Customer,
540
            'expire' => $this->eccubeConfig['eccube_customer_reset_expire'],
541
            'reset_url' => $reset_url,
542
        ]);
543
544
        $message = (new \Swift_Message())
545
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
546
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
547
            ->setTo([$Customer->getEmail()])
548
            ->setReplyTo($this->BaseInfo->getEmail03())
549
            ->setReturnPath($this->BaseInfo->getEmail04());
550
551
        // HTMLテンプレートが存在する場合
552
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
553
        if (!is_null($htmlFileName)) {
554
            $htmlBody = $this->twig->render($htmlFileName, [
555
                'BaseInfo' => $this->BaseInfo,
556
                'Customer' => $Customer,
557
                'expire' => $this->eccubeConfig['eccube_customer_reset_expire'],
558
                'reset_url' => $reset_url,
559
            ]);
560
561
            $message
562
                ->setContentType('text/plain; charset=UTF-8')
563
                ->setBody($body, 'text/plain')
564
                ->addPart($htmlBody, 'text/html');
565
        } else {
566
            $message->setBody($body);
567 4
        }
568
569 4
        $event = new EventArgs(
570
            [
571 4
                'message' => $message,
572
                'Customer' => $Customer,
573
                'BaseInfo' => $this->BaseInfo,
574 4
                'resetUrl' => $reset_url,
575 4
            ],
576 4
            null
577 4
        );
578 4
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_PASSWORD_RESET, $event);
579 4
580 4
        $count = $this->mailer->send($message);
581 4
582 4
        log_info('パスワード再発行メール送信完了', ['count' => $count]);
583
584 4
        return $count;
585
    }
586 4
587 4
    /**
588 4
     * Send password reset notification mail.
589 4
     *
590 4
     * @param $Customer 会員情報
591
     * @param string $password
592 4
     */
593 View Code Duplication
    public function sendPasswordResetCompleteMail(\Eccube\Entity\Customer $Customer, $password)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
594 4
    {
595
        log_info('パスワード変更完了メール送信開始');
596
597
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_reset_complete_mail_template_id']);
598
599
        $body = $this->twig->render($MailTemplate->getFileName(), [
600
            'BaseInfo' => $this->BaseInfo,
601
            'Customer' => $Customer,
602
            'password' => $password,
603
        ]);
604
605
        $message = (new \Swift_Message())
606
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
607
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
608 4
            ->setTo([$Customer->getEmail()])
609 4
            ->setBcc($this->BaseInfo->getEmail01())
610 4
            ->setReplyTo($this->BaseInfo->getEmail03())
611
            ->setReturnPath($this->BaseInfo->getEmail04());
612
613 4
        // HTMLテンプレートが存在する場合
614
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
615 4
        if (!is_null($htmlFileName)) {
616 4
            $htmlBody = $this->twig->render($htmlFileName, [
617 4
                'BaseInfo' => $this->BaseInfo,
618 4
                'Customer' => $Customer,
619 4
                'password' => $password,
620
            ]);
621
622
            $message
623
                ->setContentType('text/plain; charset=UTF-8')
624
                ->setBody($body, 'text/plain')
625
                ->addPart($htmlBody, 'text/html');
626
        } else {
627
            $message->setBody($body);
628
        }
629
630
        $event = new EventArgs(
631
            [
632
                'message' => $message,
633
                'Customer' => $Customer,
634
                'BaseInfo' => $this->BaseInfo,
635
                'password' => $password,
636
            ],
637
            null
638
        );
639
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_PASSWORD_RESET_COMPLETE, $event);
640
641
        $count = $this->mailer->send($message);
642
643
        log_info('パスワード変更完了メール送信完了', ['count' => $count]);
644
645
        return $count;
646
    }
647
648
    /**
649
     * ポイントでマイナス発生時にメール通知する。
650
     *
651
     * @param Order $Order
652
     * @param int $currentPoint
653
     * @param int $changePoint
654
     */
655
    public function sendPointNotifyMail(\Eccube\Entity\Order $Order, $currentPoint = 0, $changePoint = 0)
656
    {
657
        $body = $this->twig->render('Mail/point_notify.twig', [
658
            'Order' => $Order,
659
            'currentPoint' => $currentPoint,
660
            'changePoint' => $changePoint,
661
        ]);
662
663
        $message = (new \Swift_Message())
664
            ->setSubject('['.$this->BaseInfo->getShopName().'] ポイント通知')
665
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
666
            ->setTo([$this->BaseInfo->getEmail01()])
667
            ->setBcc($this->BaseInfo->getEmail01())
668
            ->setReplyTo($this->BaseInfo->getEmail03())
669
            ->setReturnPath($this->BaseInfo->getEmail04());
670
671
        // HTMLテンプレートが存在する場合
672
        $htmlFileName = $this->getHtmlTemplate('Mail/point_notify.twig');
673
        if (!is_null($htmlFileName)) {
674
            $htmlBody = $this->twig->render($htmlFileName, [
675
                'Order' => $Order,
676
                'currentPoint' => $currentPoint,
677
                'changePoint' => $changePoint,
678
            ]);
679
680
            $message
681
                ->setContentType('text/plain; charset=UTF-8')
682
                ->setBody($body, 'text/plain')
683
                ->addPart($htmlBody, 'text/html');
684
        } else {
685
            $message->setBody($body);
686
        }
687
688
        $this->mailer->send($message);
689
    }
690
691
    /**
692
     * 発送通知メールを送信する.
693
     * 発送通知メールは受注ごとに送られる
694
     *
695
     * @param Shipping $Shipping
696
     *
697
     * @throws \Twig_Error
698
     */
699
    public function sendShippingNotifyMail(Shipping $Shipping)
700
    {
701
        log_info('出荷通知メール送信処理開始', ['id' => $Shipping->getId()]);
702
703
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']);
704
705
        /** @var Order $Order */
706
        $Order = $Shipping->getOrder();
707
        $body = $this->getShippingNotifyMailBody($Shipping, $Order, $MailTemplate->getFileName());
708
709
        $message = (new \Swift_Message())
710
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
711
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
712
            ->setTo($Order->getEmail())
713
            ->setBcc($this->BaseInfo->getEmail01())
714
            ->setReplyTo($this->BaseInfo->getEmail03())
715
            ->setReturnPath($this->BaseInfo->getEmail04());
716
717
        // HTMLテンプレートが存在する場合
718
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
719
        if (!is_null($htmlFileName)) {
720
            $htmlBody = $this->getShippingNotifyMailBody($Shipping, $Order, $htmlFileName, true);
721
722
            $message
723
                ->setContentType('text/plain; charset=UTF-8')
724
                ->setBody($body, 'text/plain')
725
                ->addPart($htmlBody, 'text/html');
726
        } else {
727
            $message->setBody($body);
728
        }
729
730
        $this->mailer->send($message);
731
732
        $MailHistory = new MailHistory();
733
        $MailHistory->setMailSubject($message->getSubject())
734
                ->setMailBody($message->getBody())
735
                ->setOrder($Order)
736
                ->setSendDate(new \DateTime());
737
738
        // HTML用メールの設定
739
        $multipart = $message->getChildren();
740
        if (count($multipart) > 0) {
741
            $MailHistory->setMailHtmlBody($multipart[0]->getBody());
742
        }
743
744
        $this->mailHistoryRepository->save($MailHistory);
745
746
        log_info('出荷通知メール送信処理完了', ['id' => $Shipping->getId()]);
747
    }
748
749
    /**
750
     * @param Shipping $Shipping
751
     * @param Order $Order
752
     * @param string|null $templateName
753
     * @param boolean $is_html
754
     *
755
     * @return string
756
     *
757
     * @throws \Twig_Error
758
     */
759
    public function getShippingNotifyMailBody(Shipping $Shipping, Order $Order, $templateName = null, $is_html = false)
760
    {
761
        $ShippingItems = array_filter($Shipping->getOrderItems()->toArray(), function (OrderItem $OrderItem) use ($Order) {
762
            return $OrderItem->getOrderId() === $Order->getId();
763
        });
764
765
        if (is_null($templateName)) {
766
            /** @var MailTemplate $MailTemplate */
767
            $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']);
768
            $fileName = $MailTemplate->getFileName();
769
        } else {
770
            $fileName = $templateName;
771
        }
772
773
        if ($is_html) {
774
            $htmlFileName = $this->getHtmlTemplate($fileName);
775
            $fileName = !is_null($htmlFileName) ? $htmlFileName : $fileName;
776
        }
777
778
        return $this->twig->render($fileName, [
779
            'Shipping' => $Shipping,
780
            'ShippingItems' => $ShippingItems,
781
        ]);
782
    }
783
784
    /**
785
     * [getHtmlTemplate description]
786
     *
787
     * @param  string $templateName  プレーンテキストメールのファイル名
788
     *
789
     * @return string|null  存在する場合はファイル名を返す
790
     */
791
    public function getHtmlTemplate($templateName)
792
    {
793
        // メールテンプレート名からHTMLメール用テンプレート名を生成
794
        $fileName = explode('.', $templateName);
795
        $suffix = '.html';
796
        $htmlFileName = $fileName[0].$suffix.'.'.$fileName[1];
797
798
        // HTMLメール用テンプレートの存在チェック
799
        if ($this->twig->getLoader()->exists($htmlFileName)) {
800
            return $htmlFileName;
801
        } else {
802
            return null;
803
        }
804
    }
805
}
806