Completed
Push — sf/composer-install-update ( a1e834 )
by Kiyotaka
10:44
created

MailService::sendAdminCustomerConfirmMail()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 55

Duplication

Lines 55
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 55
loc 55
rs 8.9818
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
    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
        $this->mailer = $mailer;
90
        $this->mailTemplateRepository = $mailTemplateRepository;
91
        $this->mailHistoryRepository = $mailHistoryRepository;
92
        $this->BaseInfo = $baseInfoRepository->get();
93
        $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
        $this->eccubeConfig = $eccubeConfig;
95
        $this->twig = $twig;
96
    }
97
98
    /**
99
     * Send customer confirm mail.
100
     *
101
     * @param $Customer 会員情報
102
     * @param string $activateUrl アクティベート用url
103
     */
104 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
        log_info('仮会員登録メール送信開始');
107
108
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_confirm_mail_template_id']);
109
110
        $body = $this->twig->render($MailTemplate->getFileName(), [
111
            'Customer' => $Customer,
112
            'BaseInfo' => $this->BaseInfo,
113
            'activateUrl' => $activateUrl,
114
        ]);
115
116
        $message = (new \Swift_Message())
117
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
118
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
119
            ->setTo([$Customer->getEmail()])
120
            ->setBcc($this->BaseInfo->getEmail01())
121
            ->setReplyTo($this->BaseInfo->getEmail03())
122
            ->setReturnPath($this->BaseInfo->getEmail04());
123
124
        // HTMLテンプレートが存在する場合
125
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
126
        if (!is_null($htmlFileName)) {
127
            $htmlBody = $this->twig->render($htmlFileName, [
128
                'Customer' => $Customer,
129
                'BaseInfo' => $this->BaseInfo,
130
                'activateUrl' => $activateUrl,
131
            ]);
132
133
            $message
134
                ->setContentType('text/plain; charset=UTF-8')
135
                ->setBody($body, 'text/plain')
136
                ->addPart($htmlBody, 'text/html');
137
        } else {
138
            $message->setBody($body);
139
        }
140
141
        $event = new EventArgs(
142
            [
143
                'message' => $message,
144
                'Customer' => $Customer,
145
                'BaseInfo' => $this->BaseInfo,
146
                'activateUrl' => $activateUrl,
147
            ],
148
            null
149
        );
150
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CUSTOMER_CONFIRM, $event);
151
152
        $count = $this->mailer->send($message, $failures);
153
154
        log_info('仮会員登録メール送信完了', ['count' => $count]);
155
156
        return $count;
157
    }
158
159
    /**
160
     * Send customer complete mail.
161
     *
162
     * @param $Customer 会員情報
163
     */
164 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
    {
166
        log_info('会員登録完了メール送信開始');
167
168
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_complete_mail_template_id']);
169
170
        $body = $this->twig->render($MailTemplate->getFileName(), [
171
            'Customer' => $Customer,
172
            'BaseInfo' => $this->BaseInfo,
173
        ]);
174
175
        $message = (new \Swift_Message())
176
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
177
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
178
            ->setTo([$Customer->getEmail()])
179
            ->setBcc($this->BaseInfo->getEmail01())
180
            ->setReplyTo($this->BaseInfo->getEmail03())
181
            ->setReturnPath($this->BaseInfo->getEmail04());
182
183
        // HTMLテンプレートが存在する場合
184
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
185
        if (!is_null($htmlFileName)) {
186
            $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
        } else {
196
            $message->setBody($body);
197
        }
198
199
        $event = new EventArgs(
200
            [
201
                'message' => $message,
202
                'Customer' => $Customer,
203
                'BaseInfo' => $this->BaseInfo,
204
            ],
205
            null
206
        );
207
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CUSTOMER_COMPLETE, $event);
208
209
        $count = $this->mailer->send($message);
210
211
        log_info('会員登録完了メール送信完了', ['count' => $count]);
212
213
        return $count;
214
    }
215
216
    /**
217
     * Send withdraw mail.
218
     *
219
     * @param $Customer Customer
220
     * @param $email string
221
     */
222 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
        log_info('退会手続き完了メール送信開始');
225
226
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_customer_withdraw_mail_template_id']);
227
228
        $body = $this->twig->render($MailTemplate->getFileName(), [
229
            'Customer' => $Customer,
230
            'BaseInfo' => $this->BaseInfo,
231
        ]);
232
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
241
        // HTMLテンプレートが存在する場合
242
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
243
        if (!is_null($htmlFileName)) {
244
            $htmlBody = $this->twig->render($htmlFileName, [
245
                'Customer' => $Customer,
246
                'BaseInfo' => $this->BaseInfo,
247
            ]);
248
249
            $message
250
                ->setContentType('text/plain; charset=UTF-8')
251
                ->setBody($body, 'text/plain')
252
                ->addPart($htmlBody, 'text/html');
253
        } else {
254
            $message->setBody($body);
255
        }
256
257
        $event = new EventArgs(
258
            [
259
                'message' => $message,
260
                'Customer' => $Customer,
261
                'BaseInfo' => $this->BaseInfo,
262
                'email' => $email,
263
            ],
264
            null
265
        );
266
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CUSTOMER_WITHDRAW, $event);
267
268
        $count = $this->mailer->send($message);
269
270
        log_info('退会手続き完了メール送信完了', ['count' => $count]);
271
272
        return $count;
273
    }
274
275
    /**
276
     * Send contact mail.
277
     *
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
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
302
        if (!is_null($htmlFileName)) {
303
            $htmlBody = $this->twig->render($htmlFileName, [
304
                'data' => $formData,
305
                'BaseInfo' => $this->BaseInfo,
306
            ]);
307
308
            $message
309
                ->setContentType('text/plain; charset=UTF-8')
310
                ->setBody($body, 'text/plain')
311
                ->addPart($htmlBody, 'text/html');
312
        } else {
313
            $message->setBody($body);
314
        }
315
316
        $event = new EventArgs(
317
            [
318
                'message' => $message,
319
                'formData' => $formData,
320
                'BaseInfo' => $this->BaseInfo,
321
            ],
322
            null
323
        );
324
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_CONTACT, $event);
325
326
        $count = $this->mailer->send($message);
327
328
        log_info('お問い合わせ受付メール送信完了', ['count' => $count]);
329
330
        return $count;
331
    }
332
333
    /**
334
     * Alias of sendContactMail().
335
     *
336
     * @param $formData お問い合わせ内容
337
     *
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
347
    /**
348
     * Send order mail.
349
     *
350
     * @param \Eccube\Entity\Order $Order 受注情報
351
     *
352
     * @return \Swift_Message
353
     */
354
    public function sendOrderMail(\Eccube\Entity\Order $Order)
355
    {
356
        log_info('受注メール送信開始');
357
358
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_order_mail_template_id']);
359
360
        $body = $this->twig->render($MailTemplate->getFileName(), [
361
            'Order' => $Order,
362
        ]);
363
364
        $message = (new \Swift_Message())
365
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
366
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
367
            ->setTo([$Order->getEmail()])
368
            ->setBcc($this->BaseInfo->getEmail01())
369
            ->setReplyTo($this->BaseInfo->getEmail03())
370
            ->setReturnPath($this->BaseInfo->getEmail04());
371
372
        // HTMLテンプレートが存在する場合
373
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
374
        if (!is_null($htmlFileName)) {
375
            $htmlBody = $this->twig->render($htmlFileName, [
376
                'Order' => $Order,
377
            ]);
378
379
            $message
380
                ->setContentType('text/plain; charset=UTF-8')
381
                ->setBody($body, 'text/plain')
382
                ->addPart($htmlBody, 'text/html');
383
        } else {
384
            $message->setBody($body);
385
        }
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
        log_info('受注メール送信完了', ['count' => $count]);
401
402
        return $message;
403
    }
404
405
    /**
406
     * Send admin customer confirm mail.
407
     *
408
     * @param $Customer 会員情報
409
     * @param string $activateUrl アクティベート用url
410
     */
411 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...
412
    {
413
        log_info('仮会員登録再送メール送信開始');
414
415
        /* @var $MailTemplate \Eccube\Entity\MailTemplate */
416
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_entry_confirm_mail_template_id']);
417
418
        $body = $this->twig->render($MailTemplate->getFileName(), [
419
            'BaseInfo' => $this->BaseInfo,
420
            'Customer' => $Customer,
421
            'activateUrl' => $activateUrl,
422
        ]);
423
424
        $message = (new \Swift_Message())
425
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
426
            ->setFrom([$this->BaseInfo->getEmail03() => $this->BaseInfo->getShopName()])
427
            ->setTo([$Customer->getEmail()])
428
            ->setBcc($this->BaseInfo->getEmail01())
429
            ->setReplyTo($this->BaseInfo->getEmail03())
430
            ->setReturnPath($this->BaseInfo->getEmail04());
431
432
        // HTMLテンプレートが存在する場合
433
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
434
        if (!is_null($htmlFileName)) {
435
            $htmlBody = $this->twig->render($htmlFileName, [
436
                'BaseInfo' => $this->BaseInfo,
437
                'Customer' => $Customer,
438
                'activateUrl' => $activateUrl,
439
            ]);
440
441
            $message
442
                ->setContentType('text/plain; charset=UTF-8')
443
                ->setBody($body, 'text/plain')
444
                ->addPart($htmlBody, 'text/html');
445
        } else {
446
            $message->setBody($body);
447
        }
448
449
        $event = new EventArgs(
450
            [
451
                'message' => $message,
452
                'Customer' => $Customer,
453
                'BaseInfo' => $this->BaseInfo,
454
                'activateUrl' => $activateUrl,
455
            ],
456
            null
457
        );
458
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_ADMIN_CUSTOMER_CONFIRM, $event);
459
460
        $count = $this->mailer->send($message);
461
462
        log_info('仮会員登録再送メール送信完了', ['count' => $count]);
463
464
        return $count;
465
    }
466
467
    /**
468
     * Send admin order mail.
469
     *
470
     * @param Order $Order 受注情報
471
     * @param $formData 入力内容
472
     *
473
     * @return \Swift_Message
474
     *
475
     * @throws \Twig_Error_Loader
476
     * @throws \Twig_Error_Runtime
477
     * @throws \Twig_Error_Syntax
478
     */
479
    public function sendAdminOrderMail(Order $Order, $formData)
480
    {
481
        log_info('受注管理通知メール送信開始');
482
483
        $message = (new \Swift_Message())
484
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$formData['mail_subject'])
485
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
486
            ->setTo([$Order->getEmail()])
487
            ->setBcc($this->BaseInfo->getEmail01())
488
            ->setReplyTo($this->BaseInfo->getEmail03())
489
            ->setReturnPath($this->BaseInfo->getEmail04());
490
491
        if (is_null($formData['html_tpl_data'])) {
492
            $message->setBody($formData['tpl_data']);
493
        } else {
494
            $message
495
                ->setContentType('text/plain; charset=UTF-8')
496
                ->setBody($formData['tpl_data'], 'text/plain')
497
                ->addPart($formData['html_tpl_data'], 'text/html');
498
        }
499
500
        $event = new EventArgs(
501
            [
502
                'message' => $message,
503
                'Order' => $Order,
504
                'formData' => $formData,
505
                'BaseInfo' => $this->BaseInfo,
506
            ],
507
            null
508
        );
509
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_ADMIN_ORDER, $event);
510
511
        $count = $this->mailer->send($message);
512
513
        log_info('受注管理通知メール送信完了', ['count' => $count]);
514
515
        return $message;
516
    }
517
518
    /**
519
     * Send password reset notification mail.
520
     *
521
     * @param $Customer 会員情報
522
     * @param string $reset_url
523
     */
524
    public function sendPasswordResetNotificationMail(\Eccube\Entity\Customer $Customer, $reset_url)
525
    {
526
        log_info('パスワード再発行メール送信開始');
527
528
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_forgot_mail_template_id']);
529
        $body = $this->twig->render($MailTemplate->getFileName(), [
530
            'BaseInfo' => $this->BaseInfo,
531
            'Customer' => $Customer,
532
            'expire' => $this->eccubeConfig['eccube_customer_reset_expire'],
533
            'reset_url' => $reset_url,
534
        ]);
535
536
        $message = (new \Swift_Message())
537
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
538
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
539
            ->setTo([$Customer->getEmail()])
540
            ->setReplyTo($this->BaseInfo->getEmail03())
541
            ->setReturnPath($this->BaseInfo->getEmail04());
542
543
        // HTMLテンプレートが存在する場合
544
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
545
        if (!is_null($htmlFileName)) {
546
            $htmlBody = $this->twig->render($htmlFileName, [
547
                'BaseInfo' => $this->BaseInfo,
548
                'Customer' => $Customer,
549
                'expire' => $this->eccubeConfig['eccube_customer_reset_expire'],
550
                'reset_url' => $reset_url,
551
            ]);
552
553
            $message
554
                ->setContentType('text/plain; charset=UTF-8')
555
                ->setBody($body, 'text/plain')
556
                ->addPart($htmlBody, 'text/html');
557
        } else {
558
            $message->setBody($body);
559
        }
560
561
        $event = new EventArgs(
562
            [
563
                'message' => $message,
564
                'Customer' => $Customer,
565
                'BaseInfo' => $this->BaseInfo,
566
                'resetUrl' => $reset_url,
567
            ],
568
            null
569
        );
570
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_PASSWORD_RESET, $event);
571
572
        $count = $this->mailer->send($message);
573
574
        log_info('パスワード再発行メール送信完了', ['count' => $count]);
575
576
        return $count;
577
    }
578
579
    /**
580
     * Send password reset notification mail.
581
     *
582
     * @param $Customer 会員情報
583
     * @param string $password
584
     */
585 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...
586
    {
587
        log_info('パスワード変更完了メール送信開始');
588
589
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_reset_complete_mail_template_id']);
590
591
        $body = $this->twig->render($MailTemplate->getFileName(), [
592
            'BaseInfo' => $this->BaseInfo,
593
            'Customer' => $Customer,
594
            'password' => $password,
595
        ]);
596
597
        $message = (new \Swift_Message())
598
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
599
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
600
            ->setTo([$Customer->getEmail()])
601
            ->setBcc($this->BaseInfo->getEmail01())
602
            ->setReplyTo($this->BaseInfo->getEmail03())
603
            ->setReturnPath($this->BaseInfo->getEmail04());
604
605
        // HTMLテンプレートが存在する場合
606
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
607
        if (!is_null($htmlFileName)) {
608
            $htmlBody = $this->twig->render($htmlFileName, [
609
                'BaseInfo' => $this->BaseInfo,
610
                'Customer' => $Customer,
611
                'password' => $password,
612
            ]);
613
614
            $message
615
                ->setContentType('text/plain; charset=UTF-8')
616
                ->setBody($body, 'text/plain')
617
                ->addPart($htmlBody, 'text/html');
618
        } else {
619
            $message->setBody($body);
620
        }
621
622
        $event = new EventArgs(
623
            [
624
                'message' => $message,
625
                'Customer' => $Customer,
626
                'BaseInfo' => $this->BaseInfo,
627
                'password' => $password,
628
            ],
629
            null
630
        );
631
        $this->eventDispatcher->dispatch(EccubeEvents::MAIL_PASSWORD_RESET_COMPLETE, $event);
632
633
        $count = $this->mailer->send($message);
634
635
        log_info('パスワード変更完了メール送信完了', ['count' => $count]);
636
637
        return $count;
638
    }
639
640
    /**
641
     * ポイントでマイナス発生時にメール通知する。
642
     *
643
     * @param Order $Order
644
     * @param int $currentPoint
645
     * @param int $changePoint
646
     */
647
    public function sendPointNotifyMail(\Eccube\Entity\Order $Order, $currentPoint = 0, $changePoint = 0)
648
    {
649
        $body = $this->twig->render('Mail/point_notify.twig', [
650
            'Order' => $Order,
651
            'currentPoint' => $currentPoint,
652
            'changePoint' => $changePoint,
653
        ]);
654
655
        $message = (new \Swift_Message())
656
            ->setSubject('['.$this->BaseInfo->getShopName().'] ポイント通知')
657
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
658
            ->setTo([$this->BaseInfo->getEmail01()])
659
            ->setBcc($this->BaseInfo->getEmail01())
660
            ->setReplyTo($this->BaseInfo->getEmail03())
661
            ->setReturnPath($this->BaseInfo->getEmail04());
662
663
        // HTMLテンプレートが存在する場合
664
        $htmlFileName = $this->getHtmlTemplate('Mail/point_notify.twig');
665
        if (!is_null($htmlFileName)) {
666
            $htmlBody = $this->twig->render($htmlFileName, [
667
                'Order' => $Order,
668
                'currentPoint' => $currentPoint,
669
                'changePoint' => $changePoint,
670
            ]);
671
672
            $message
673
                ->setContentType('text/plain; charset=UTF-8')
674
                ->setBody($body, 'text/plain')
675
                ->addPart($htmlBody, 'text/html');
676
        } else {
677
            $message->setBody($body);
678
        }
679
680
        $this->mailer->send($message);
681
    }
682
683
    /**
684
     * 発送通知メールを送信する.
685
     * 発送通知メールは受注ごとに送られる
686
     *
687
     * @param Shipping $Shipping
688
     *
689
     * @throws \Twig_Error
690
     */
691
    public function sendShippingNotifyMail(Shipping $Shipping)
692
    {
693
        log_info('出荷通知メール送信処理開始', ['id' => $Shipping->getId()]);
694
695
        $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']);
696
697
        /** @var Order $Order */
698
        $Order = $Shipping->getOrder();
699
        $body = $this->getShippingNotifyMailBody($Shipping, $Order, $MailTemplate->getFileName());
700
701
        $message = (new \Swift_Message())
702
            ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject())
703
            ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()])
704
            ->setTo($Order->getEmail())
705
            ->setBcc($this->BaseInfo->getEmail01())
706
            ->setReplyTo($this->BaseInfo->getEmail03())
707
            ->setReturnPath($this->BaseInfo->getEmail04());
708
709
        // HTMLテンプレートが存在する場合
710
        $htmlFileName = $this->getHtmlTemplate($MailTemplate->getFileName());
711
        if (!is_null($htmlFileName)) {
712
            $htmlBody = $this->getShippingNotifyMailBody($Shipping, $Order, $htmlFileName, true);
713
714
            $message
715
                ->setContentType('text/plain; charset=UTF-8')
716
                ->setBody($body, 'text/plain')
717
                ->addPart($htmlBody, 'text/html');
718
        } else {
719
            $message->setBody($body);
720
        }
721
722
        $this->mailer->send($message);
723
724
        $MailHistory = new MailHistory();
725
        $MailHistory->setMailSubject($message->getSubject())
726
                ->setMailBody($message->getBody())
727
                ->setOrder($Order)
728
                ->setSendDate(new \DateTime());
729
730
        // HTML用メールの設定
731
        $multipart = $message->getChildren();
732
        if (count($multipart) > 0) {
733
            $MailHistory->setMailHtmlBody($multipart[0]->getBody());
734
        }
735
736
        $this->mailHistoryRepository->save($MailHistory);
737
738
        log_info('出荷通知メール送信処理完了', ['id' => $Shipping->getId()]);
739
    }
740
741
    /**
742
     * @param Shipping $Shipping
743
     * @param Order $Order
744
     * @param string|null $templateName
745
     * @param boolean $is_html
746
     *
747
     * @return string
748
     *
749
     * @throws \Twig_Error
750
     */
751
    public function getShippingNotifyMailBody(Shipping $Shipping, Order $Order, $templateName = null, $is_html = false)
752
    {
753
        $ShippingItems = array_filter($Shipping->getOrderItems()->toArray(), function (OrderItem $OrderItem) use ($Order) {
754
            return $OrderItem->getOrderId() === $Order->getId();
755
        });
756
757
        if (is_null($templateName)) {
758
            /** @var MailTemplate $MailTemplate */
759
            $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']);
760
            $fileName = $MailTemplate->getFileName();
761
        } else {
762
            $fileName = $templateName;
763
        }
764
765
        if ($is_html) {
766
            $htmlFileName = $this->getHtmlTemplate($fileName);
767
            $fileName = !is_null($htmlFileName) ? $htmlFileName : $fileName;
768
        }
769
770
        return $this->twig->render($fileName, [
771
            'Shipping' => $Shipping,
772
            'ShippingItems' => $ShippingItems,
773
        ]);
774
    }
775
776
    /**
777
     * [getHtmlTemplate description]
778
     *
779
     * @param  string $templateName  プレーンテキストメールのファイル名
780
     *
781
     * @return string|null  存在する場合はファイル名を返す
782
     */
783
    public function getHtmlTemplate($templateName)
784
    {
785
        // メールテンプレート名からHTMLメール用テンプレート名を生成
786
        $fileName = explode('.', $templateName);
787
        $suffix = '.html';
788
        $htmlFileName = $fileName[0].$suffix.'.'.$fileName[1];
789
790
        // HTMLメール用テンプレートの存在チェック
791
        if ($this->twig->getLoader()->exists($htmlFileName)) {
792
            return $htmlFileName;
793
        } else {
794
            return null;
795
        }
796
    }
797
}
798