EmailService   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 74
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEmailObject() 0 10 2
B sendEmailMessage() 0 46 11
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
15
use Symfony\Component\Mime\Address;
16
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
17
use TYPO3\CMS\Core\Mail\FluidEmail;
18
use TYPO3\CMS\Core\Mail\MailerInterface;
19
use TYPO3\CMS\Core\Mail\MailMessage;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
class EmailService
23
{
24
    /**
25
     * Sends an email, if sender and recipient is an valid email address and if the subject is not empty
26
     *
27
     * @param string $sender The sender
28
     * @param string $recipient The recipient
29
     * @param string $subject The subject
30
     * @param string $body E-Mail body
31
     * @param string|null $name Optional sendername
32
     * @param array $attachments Array of files (e.g. ['/absolute/path/doc.pdf'])
33
     * @param string|null $replyTo The reply-to mail
34
     *
35
     * @return bool true/false if message is sent
36
     */
37
    public function sendEmailMessage(
38
        string $sender,
39
        string $recipient,
40
        string $subject,
41
        string $body,
42
        ?string $name = null,
43
        array $attachments = [],
44
        ?string $replyTo = null
45
    ): bool {
46
        if ($subject === '' || !GeneralUtility::validEmail($sender) || !GeneralUtility::validEmail($recipient)) {
47
            return false;
48
        }
49
50
        $useFluidEmail = (bool)(GeneralUtility::makeInstance(ExtensionConfiguration::class)
51
            ->get('sf_event_mgt', 'useFluidEmail'));
52
53
        $email = $this->getEmailObject($useFluidEmail);
54
        $email->from(new Address($sender, $name));
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Symfony\Component\Mime\Address::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $email->from(new Address($sender, /** @scrutinizer ignore-type */ $name));
Loading history...
55
        $email->to($recipient);
56
        $email->subject($subject);
57
        $email->html($body);
58
        if ($replyTo !== null && $replyTo !== '' && GeneralUtility::validEmail($replyTo)) {
59
            $email->replyTo(new Address($replyTo));
60
        }
61
        foreach ($attachments as $attachment) {
62
            if (file_exists($attachment)) {
63
                $email->attachFromPath($attachment);
64
            }
65
        }
66
67
        if ($email instanceof FluidEmail) {
68
            $email->format('html');
69
            $email->setTemplate('EventManagement');
70
            $email->assignMultiple([
71
                'title' => $subject,
72
                'body' => $body,
73
            ]);
74
        }
75
76
        $mailer = GeneralUtility::makeInstance(MailerInterface::class);
77
78
        try {
79
            $mailer->send($email);
80
            return $mailer->getSentMessage() !== null;
81
        } catch (TransportExceptionInterface $e) {
82
            return false;
83
        }
84
    }
85
86
    private function getEmailObject(bool $useFluidEmail): MailMessage|FluidEmail
87
    {
88
        if ($useFluidEmail) {
89
            /** @var FluidEmail $email */
90
            $email = GeneralUtility::makeInstance(FluidEmail::class);
91
        } else {
92
            /** @var MailMessage $email */
93
            $email = GeneralUtility::makeInstance(MailMessage::class);
94
        }
95
        return $email;
96
    }
97
}
98