Skip to content

Commit 22c4ee1

Browse files
committed
Create a typed \OCP\Mail\Events\BeforeMessageSent in lib/public/Mail/Events
Signed-off-by: Arne Hamann <kontakt+github@arne.email>
1 parent 0792e1b commit 22c4ee1

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

lib/private/Mail/Mailer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Egulias\EmailValidator\EmailValidator;
3636
use Egulias\EmailValidator\Validation\RFCValidation;
3737
use OCP\Defaults;
38+
use OCP\EventDispatcher\IEventDispatcher;
3839
use OCP\IConfig;
3940
use OCP\IL10N;
4041
use OCP\ILogger;
@@ -43,8 +44,7 @@
4344
use OCP\Mail\IEMailTemplate;
4445
use OCP\Mail\IMailer;
4546
use OCP\Mail\IMessage;
46-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
47-
use Symfony\Component\EventDispatcher\GenericEvent;
47+
use OCP\Mail\Events\BeforeMessageSent;
4848

4949

5050
/**
@@ -78,7 +78,7 @@ class Mailer implements IMailer {
7878
private $urlGenerator;
7979
/** @var IL10N */
8080
private $l10n;
81-
/** @var EventDispatcherInterface */
81+
/** @var IEventDispatcher */
8282
private $dispatcher;
8383

8484
/**
@@ -87,14 +87,14 @@ class Mailer implements IMailer {
8787
* @param Defaults $defaults
8888
* @param IURLGenerator $urlGenerator
8989
* @param IL10N $l10n
90-
* @param EventDispatcherInterface $dispatcher
90+
* @param IEventDispatcher $dispatcher
9191
*/
9292
public function __construct(IConfig $config,
9393
ILogger $logger,
9494
Defaults $defaults,
9595
IURLGenerator $urlGenerator,
9696
IL10N $l10n,
97-
EventDispatcherInterface $dispatcher) {
97+
IEventDispatcher $dispatcher) {
9898
$this->config = $config;
9999
$this->logger = $logger;
100100
$this->defaults = $defaults;
@@ -191,8 +191,8 @@ public function send(IMessage $message): array {
191191
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
192192
}
193193

194-
$this->dispatcher->dispatch('\OC\Mail::preSendMessage',
195-
new GenericEvent($message));
194+
195+
$this->dispatcher->dispatchTyped(new BeforeMessageSent($message));
196196

197197
$mailer->send($message->getSwiftMessage(), $failedRecipients);
198198

lib/private/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ public function __construct($webRoot, \OC\Config $config) {
947947
$c->query(Defaults::class),
948948
$c->getURLGenerator(),
949949
$c->getL10N('lib'),
950-
$c->getEventDispatcher()
950+
$c->query(IEventDispatcher::class)
951951
);
952952
});
953953
$this->registerAlias('Mailer', \OCP\Mail\IMailer::class);

tests/lib/Mail/MailerTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111

1212
namespace Test\Mail;
1313

14-
use OC\Activity\Event;
14+
1515
use OC\Mail\EMailTemplate;
1616
use OC\Mail\Mailer;
1717
use OCP\Defaults;
18+
use OCP\EventDispatcher\IEventDispatcher;
1819
use OCP\IConfig;
1920
use OCP\IL10N;
2021
use OCP\ILogger;
2122
use OCP\IURLGenerator;
23+
use OCP\Mail\Events\BeforeMessageSent;
2224
use OCP\Mail\IMessage;
2325
use Test\TestCase;
2426
use Swift_SwiftException;
25-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26-
use Symfony\Component\EventDispatcher\GenericEvent;
2727

2828
class MailerTest extends TestCase {
2929
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
@@ -38,9 +38,10 @@ class MailerTest extends TestCase {
3838
private $l10n;
3939
/** @var Mailer */
4040
private $mailer;
41-
/** @var EventDispatcherInterface */
41+
/** @var IEventDispatcher */
4242
private $dispatcher;
4343

44+
4445
protected function setUp(): void {
4546
parent::setUp();
4647

@@ -49,7 +50,7 @@ protected function setUp(): void {
4950
$this->logger = $this->createMock(ILogger::class);
5051
$this->urlGenerator = $this->createMock(IURLGenerator::class);
5152
$this->l10n = $this->createMock(IL10N::class);
52-
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
53+
$this->dispatcher = $this->createMock(IEventDispatcher::class);
5354
$this->mailer = new Mailer(
5455
$this->config,
5556
$this->logger,
@@ -136,9 +137,9 @@ public function testEvents() {
136137

137138

138139
$this->dispatcher->expects($this->at(0))
139-
->method('dispatch')
140-
->with('\OC\Mail::preSendMessage', $this->callback(function(GenericEvent $e) use ($test, $message, &$count) {
141-
$m = $e->getSubject('message');
140+
->method('dispatchTyped')
141+
->with($this->callback(function(BeforeMessageSent $e) use ($test, $message, &$count) {
142+
$m = $e->getMessage();
142143
$test->assertEquals($message, $m);
143144
$count++;
144145
return $m === $message;

0 commit comments

Comments
 (0)