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

MailHelper::send()   F

Complexity

Conditions 16
Paths 1681

Size

Total Lines 96
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

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