Passed
Push — master ( 858391...6fe9b0 )
by Romain
03:44
created

EmailChannel::dispatchEmailSignal()   A

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 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Domain\Channel\Email\TYPO3;
18
19
use CuyZ\Notiz\Core\Channel\AbstractChannel;
20
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Service\EntityEmailAddressMapper;
21
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Service\EntityEmailTemplateBuilder;
22
use CuyZ\Notiz\Domain\Notification\Email\EmailNotification;
23
use TYPO3\CMS\Core\Mail\MailMessage;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
26
27
/**
28
 * Channel using the default `MailMessage` provided by the TYPO3 core.
29
 *
30
 * If you need to do advanced modification on your mail, you can use a PHP
31
 * signal. Register the slot in your `ext_localconf.php` file :
32
 *
33
 * ```
34
 * // my_extension/ext_localconf.php
35
 *
36
 * $dispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
37
 *     \TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class
38
 * );
39
 *
40
 * $dispatcher->connect(
41
 *     \CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder::class,
42
 *     \CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder::COMPONENTS_SIGNAL,
43
 *     \Vendor\MyExtension\Service\Mail\MailTransformer::class,
44
 *     'registerDefinitionComponents'
45
 * );
46
 * ```
47
 *
48
 * Then modify your mail object as you need:
49
 *
50
 * ```
51
 * // my_extension/Classes/Service/Mail/MailTransformer.php
52
 *
53
 * namespace Vendor\MyExtension\Service\Mail;
54
 *
55
 * use CuyZ\Notiz\Core\Channel\Payload;
56
 * use TYPO3\CMS\Core\Mail\MailMessage;
57
 * use TYPO3\CMS\Core\SingletonInterface;
58
 * use TYPO3\CMS\Core\Utility\GeneralUtility;
59
 *
60
 * class MailTransformer implements SingletonInterface
61
 * {
62
 *     public function transform(MailMessage $mailMessage, Payload $payload)
63
 *     {
64
 *         $applicationContext = GeneralUtility::getApplicationContext();
65
 *
66
 *         // We don't change anything in production.
67
 *         if ($applicationContext->isProduction()) {
68
 *             return;
69
 *         }
70
 *
71
 *         // Add a prefix to the mail subject, containing the application context.
72
 *         $subject = "[$applicationContext][NotiZ] " . $mailMessage->getSubject();
73
 *         $mailMessage->setSubject($subject);
74
 *
75
 *         // When not in production, we want the mail to be sent only to us.
76
 *         $mailMessage->setTo('[email protected]');
77
 *         $mailMessage->setCc([]);
78
 *         $mailMessage->setBcc([]);
79
 *     }
80
 * }
81
 * ```
82
 */
83
class EmailChannel extends AbstractChannel
84
{
85
    const EMAIL_SIGNAL = 'sendEmail';
86
87
    /**
88
     * @var array
89
     */
90
    protected static $supportedNotifications = [
91
        EmailNotification::class,
92
    ];
93
94
    /**
95
     * @var EntityEmailTemplateBuilder
96
     */
97
    protected $templateBuilder;
98
99
    /**
100
     * @var EntityEmailAddressMapper
101
     */
102
    protected $addressMapper;
103
104
    /**
105
     * @var Dispatcher
106
     */
107
    protected $slotDispatcher;
108
109
    /**
110
     * Setting up services used by this channel.
111
     */
112
    protected function initialize()
113
    {
114
        $this->templateBuilder = $this->objectManager->get(EntityEmailTemplateBuilder::class, $this->payload);
115
        $this->addressMapper = $this->objectManager->get(EntityEmailAddressMapper::class, $this->payload);
116
    }
117
118
    /**
119
     * Sends the mail with processed recipients and subject/body.
120
     */
121
    protected function process()
122
    {
123
        /** @var MailMessage $mailMessage */
124
        $mailMessage = GeneralUtility::makeInstance(MailMessage::class);
125
126
        $mailMessage
127
            ->setSubject($this->templateBuilder->getSubject())
128
            ->setBody($this->templateBuilder->getBody())
129
            ->setFrom($this->addressMapper->getSender())
130
            ->setTo($this->addressMapper->getSendTo())
131
            ->setCc($this->addressMapper->getSendCc())
132
            ->setBcc($this->addressMapper->getSendBcc())
133
            ->setContentType('text/html');
134
135
        $this->dispatchEmailSignal($mailMessage);
136
137
        $mailMessage->send();
138
    }
139
140
    /**
141
     * @param MailMessage $mailMessage
142
     */
143
    protected function dispatchEmailSignal(MailMessage $mailMessage)
144
    {
145
        $this->slotDispatcher->dispatch(
146
            self::class,
147
            self::EMAIL_SIGNAL,
148
            [
149
                $mailMessage,
150
                $this->payload,
151
            ]
152
        );
153
    }
154
155
    /**
156
     * @param Dispatcher $slotDispatcher
157
     */
158
    public function injectSlotDispatcher(Dispatcher $slotDispatcher)
159
    {
160
        $this->slotDispatcher = $slotDispatcher;
161
    }
162
}
163