Passed
Pull Request — master (#5462)
by Angel Fernando Quiroz
07:19
created

MailHelper::send()   F

Complexity

Conditions 16
Paths 2065

Size

Total Lines 97
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 50
c 2
b 0
f 0
nc 2065
nop 11
dl 0
loc 97
rs 1.4

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Exception;
10
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
11
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
12
use Symfony\Component\Mailer\MailerInterface;
13
use Symfony\Component\Mime\Address;
14
use Symfony\Component\Mime\BodyRendererInterface;
15
16
final class MailHelper
17
{
18
    public function __construct(
19
        private readonly MailerInterface $mailer,
20
        private readonly BodyRendererInterface $bodyRenderer,
21
    ) {}
22
23
    public function send(
24
        string $recipientName,
25
        string $recipientEmail,
26
        string $subject,
27
        string $body,
28
        ?string $senderName = null,
29
        ?string $senderEmail = null,
30
        array $extra_headers = [],
31
        array $data_file = [],
32
        bool $embeddedImage = false,
33
        array $additionalParameters = [],
34
        ?string $sendErrorTo = null,
35
    ): bool {
36
        if (!api_valid_email($recipientEmail)) {
37
            return false;
38
        }
39
40
        $templatedEmail = new TemplatedEmail();
41
42
        api_set_noreply_and_from_address_to_mailer(
43
            $templatedEmail,
44
            ['name' => $senderName, 'email' => $senderEmail],
45
            !empty($extra_headers['reply_to']) ? $extra_headers['reply_to'] : []
46
        );
47
48
        if ($sendErrorTo) {
49
            $templatedEmail
50
                ->getHeaders()
51
                ->addIdHeader('Errors-To', $sendErrorTo)
52
            ;
53
        }
54
55
        // Reply to first
56
        $replyToName = '';
57
        $replyToEmail = '';
58
        if (isset($extra_headers['reply_to'])) {
59
            $replyToEmail = $extra_headers['reply_to']['mail'];
60
            $replyToName = $extra_headers['reply_to']['name'];
61
        }
62
63
        try {
64
            $templatedEmail->subject($subject);
65
66
            $list = api_get_setting('announcement.send_all_emails_to', true);
67
68
            if (!empty($list) && isset($list['emails'])) {
69
                foreach ($list['emails'] as $email) {
70
                    $templatedEmail->cc($email);
71
                }
72
            }
73
74
            // Attachment
75
            if (!empty($data_file)) {
76
                foreach ($data_file as $file_attach) {
77
                    if (!empty($file_attach['path']) && !empty($file_attach['filename'])) {
78
                        $templatedEmail->attachFromPath($file_attach['path'], $file_attach['filename']);
79
                    }
80
                }
81
            }
82
83
            $noReply = api_get_setting('noreply_email_address');
84
            $automaticEmailText = '';
85
86
            if (!empty($noReply)) {
87
                $automaticEmailText = '<br />'.get_lang('This is an automatic email message. Please do not reply to it.');
88
            }
89
90
            $params = [
91
                'mail_header_style' => api_get_setting('mail.mail_header_style'),
92
                'mail_content_style' => api_get_setting('mail.mail_content_style'),
93
                'link' => $additionalParameters['link'] ?? '',
94
                'automatic_email_text' => $automaticEmailText,
95
                'content' => $body,
96
                'theme' => api_get_visual_theme(),
97
            ];
98
99
            if (!empty($recipientEmail)) {
100
                $templatedEmail->to(new Address($recipientEmail, $recipientName));
101
            }
102
103
            if (!empty($replyToEmail)) {
104
                $templatedEmail->replyTo(new Address($replyToEmail, $replyToName));
105
            }
106
107
            $templatedEmail
108
                ->htmlTemplate('@ChamiloCore/Mailer/Default/default.html.twig')
109
                ->context($params)
110
            ;
111
112
            $this->bodyRenderer->render($templatedEmail);
113
            $this->mailer->send($templatedEmail);
114
115
            return true;
116
        } catch (Exception|TransportExceptionInterface $e) {
117
            error_log($e->getMessage());
118
119
            return false;
120
        }
121
    }
122
}