Passed
Pull Request — master (#6396)
by Angel Fernando Quiroz
08:47
created

MailHelper::send()   F

Complexity

Conditions 18
Paths 3121

Size

Total Lines 100
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 51
nc 3121
nop 11
dl 0
loc 100
rs 0.7
c 0
b 0
f 0

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