EmailChannel::dispatchEmailSignal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Domain\Channel\Email\TYPO3;
19
20
use CuyZ\Notiz\Core\Channel\AbstractChannel;
21
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Service\EntityEmailAddressMapper;
22
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Service\EntityEmailTemplateBuilder;
23
use CuyZ\Notiz\Domain\Notification\Email\EmailNotification;
24
use TYPO3\CMS\Core\Mail\MailMessage;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\SignalSlot\Dispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
/**
29
 * Channel using the default `MailMessage` provided by the TYPO3 core.
30
 *
31
 * If you need to do advanced modification on your mail, you can use a PHP
32
 * signal. Register the slot in your `ext_localconf.php` file :
33
 *
34
 * ```
35
 * // my_extension/ext_localconf.php
36
 *
37
 * $dispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
38
 *     \TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class
39
 * );
40
 *
41
 * $dispatcher->connect(
42
 *     \CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder::class,
43
 *     \CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder::COMPONENTS_SIGNAL,
44
 *     \Vendor\MyExtension\Service\Mail\MailTransformer::class,
45
 *     'registerDefinitionComponents'
46
 * );
47
 * ```
48
 *
49
 * Then modify your mail object as you need:
50
 *
51
 * ```
52
 * // my_extension/Classes/Service/Mail/MailTransformer.php
53
 *
54
 * namespace Vendor\MyExtension\Service\Mail;
55
 *
56
 * use CuyZ\Notiz\Core\Channel\Payload;
57
 * use TYPO3\CMS\Core\Mail\MailMessage;
58
 * use TYPO3\CMS\Core\SingletonInterface;
59
 * use TYPO3\CMS\Core\Utility\GeneralUtility;
60
 *
61
 * class MailTransformer implements SingletonInterface
62
 * {
63
 *     public function transform(MailMessage $mailMessage, Payload $payload)
64
 *     {
65
 *         $applicationContext = GeneralUtility::getApplicationContext();
66
 *
67
 *         // We don't change anything in production.
68
 *         if ($applicationContext->isProduction()) {
69
 *             return;
70
 *         }
71
 *
72
 *         // Add a prefix to the mail subject, containing the application context.
73
 *         $subject = "[$applicationContext][NotiZ] " . $mailMessage->getSubject();
74
 *         $mailMessage->setSubject($subject);
75
 *
76
 *         // When not in production, we want the mail to be sent only to us.
77
 *         $mailMessage->setTo('[email protected]');
78
 *         $mailMessage->setCc([]);
79
 *         $mailMessage->setBcc([]);
80
 *     }
81
 * }
82
 * ```
83
 */
84
class EmailChannel extends AbstractChannel
85
{
86
    const EMAIL_SIGNAL = 'sendEmail';
87
88
    /**
89
     * @var array
90
     */
91
    protected static $supportedNotifications = [
92
        EmailNotification::class,
93
    ];
94
95
    /**
96
     * @var EntityEmailTemplateBuilder
97
     */
98
    protected $templateBuilder;
99
100
    /**
101
     * @var EntityEmailAddressMapper
102
     */
103
    protected $addressMapper;
104
105
    /**
106
     * @var Dispatcher
107
     */
108
    protected $slotDispatcher;
109
110
    /**
111
     * Setting up services used by this channel.
112
     */
113
    protected function initialize()
114
    {
115
        $this->templateBuilder = $this->objectManager->get(EntityEmailTemplateBuilder::class, $this->payload);
116
        $this->addressMapper = $this->objectManager->get(EntityEmailAddressMapper::class, $this->payload);
117
    }
118
119
    /**
120
     * Sends the mail with processed recipients and subject/body.
121
     */
122
    protected function process()
123
    {
124
        /** @var MailMessage $mailMessage */
125
        $mailMessage = GeneralUtility::makeInstance(MailMessage::class);
126
127
        $mailMessage
128
            ->setSubject($this->templateBuilder->getSubject())
129
            ->setBody($this->templateBuilder->getBody())
130
            ->setFrom($this->addressMapper->getSender())
131
            ->setTo($this->addressMapper->getSendTo())
132
            ->setCc($this->addressMapper->getSendCc())
133
            ->setBcc($this->addressMapper->getSendBcc())
134
            ->setContentType('text/html');
135
136
        $this->dispatchEmailSignal($mailMessage);
137
138
        $mailMessage->send();
139
    }
140
141
    /**
142
     * @param MailMessage $mailMessage
143
     */
144
    protected function dispatchEmailSignal(MailMessage $mailMessage)
145
    {
146
        $this->slotDispatcher->dispatch(
147
            self::class,
148
            self::EMAIL_SIGNAL,
149
            [
150
                $mailMessage,
151
                $this->payload,
152
            ]
153
        );
154
    }
155
156
    /**
157
     * @param Dispatcher $slotDispatcher
158
     */
159
    public function injectSlotDispatcher(Dispatcher $slotDispatcher)
160
    {
161
        $this->slotDispatcher = $slotDispatcher;
162
    }
163
}
164