From 3b6e0653d6f82448a5a8f9b5b985491cd152e99d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 25 Jul 2023 19:30:15 +0200 Subject: [PATCH] fix: Move Sharing events to IEventDispatcher Signed-off-by: Joas Schilling --- lib/private/Share20/LegacyHooks.php | 47 ++++++++++++++++++--------- lib/private/Share20/Manager.php | 4 +-- tests/lib/Share20/LegacyHooksTest.php | 6 ++-- tests/lib/Share20/ManagerTest.php | 4 +-- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/lib/private/Share20/LegacyHooks.php b/lib/private/Share20/LegacyHooks.php index feb4604e8848e..c185c5d6ed73c 100644 --- a/lib/private/Share20/LegacyHooks.php +++ b/lib/private/Share20/LegacyHooks.php @@ -26,35 +26,50 @@ */ namespace OC\Share20; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Share; use OCP\Share\IShare; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; class LegacyHooks { - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ private $eventDispatcher; - /** - * LegacyHooks constructor. - * - * @param EventDispatcherInterface $eventDispatcher - */ - public function __construct(EventDispatcherInterface $eventDispatcher) { + public function __construct(IEventDispatcher $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; - $this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']); - $this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']); - $this->eventDispatcher->addListener('OCP\Share::postUnshareFromSelf', [$this, 'postUnshareFromSelf']); - $this->eventDispatcher->addListener('OCP\Share::preShare', [$this, 'preShare']); - $this->eventDispatcher->addListener('OCP\Share::postShare', [$this, 'postShare']); + $this->eventDispatcher->addListener('OCP\Share::preUnshare', function ($event) { + if ($event instanceof GenericEvent) { + $this->preUnshare($event); + } + }); + $this->eventDispatcher->addListener('OCP\Share::postUnshare', function ($event) { + if ($event instanceof GenericEvent) { + $this->postUnshare($event); + } + }); + $this->eventDispatcher->addListener('OCP\Share::postUnshareFromSelf', function ($event) { + if ($event instanceof GenericEvent) { + $this->postUnshareFromSelf($event); + } + }); + $this->eventDispatcher->addListener('OCP\Share::preShare', function ($event) { + if ($event instanceof GenericEvent) { + $this->preShare($event); + } + }); + $this->eventDispatcher->addListener('OCP\Share::postShare', function ($event) { + if ($event instanceof GenericEvent) { + $this->postShare($event); + } + }); } /** * @param GenericEvent $e */ - public function preUnshare(GenericEvent $e) { + public function preUnshare($e) { /** @var IShare $share */ $share = $e->getSubject(); @@ -65,7 +80,7 @@ public function preUnshare(GenericEvent $e) { /** * @param GenericEvent $e */ - public function postUnshare(GenericEvent $e) { + public function postUnshare($e) { /** @var IShare $share */ $share = $e->getSubject(); @@ -86,7 +101,7 @@ public function postUnshare(GenericEvent $e) { /** * @param GenericEvent $e */ - public function postUnshareFromSelf(GenericEvent $e) { + public function postUnshareFromSelf($e) { /** @var IShare $share */ $share = $e->getSubject(); diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index 732bd5bb97d39..3f5dbd7cd289a 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -157,7 +157,7 @@ public function __construct( $this->sharingDisabledForUsersCache = new CappedMemoryCache(); // The constructor of LegacyHooks registers the listeners of share events // do not remove if those are not properly migrated - $this->legacyHooks = new LegacyHooks($this->legacyDispatcher); + $this->legacyHooks = new LegacyHooks($dispatcher); $this->mailer = $mailer; $this->urlGenerator = $urlGenerator; $this->defaults = $defaults; @@ -194,7 +194,7 @@ protected function verifyPassword($password) { // Let others verify the password try { - $this->legacyDispatcher->dispatch(new ValidatePasswordPolicyEvent($password)); + $this->dispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password)); } catch (HintException $e) { throw new \Exception($e->getHint()); } diff --git a/tests/lib/Share20/LegacyHooksTest.php b/tests/lib/Share20/LegacyHooksTest.php index b8b005abd4e28..54d8e0be72534 100644 --- a/tests/lib/Share20/LegacyHooksTest.php +++ b/tests/lib/Share20/LegacyHooksTest.php @@ -27,12 +27,12 @@ use OC\Share20\LegacyHooks; use OC\Share20\Manager; use OCP\Constants; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICacheEntry; use OCP\Files\File; use OCP\IServerContainer; use OCP\Share\IShare; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\GenericEvent; use Test\TestCase; @@ -40,7 +40,7 @@ class LegacyHooksTest extends TestCase { /** @var LegacyHooks */ private $hooks; - /** @var EventDispatcher */ + /** @var IEventDispatcher */ private $eventDispatcher; /** @var Manager */ @@ -53,7 +53,7 @@ protected function setUp(): void { $logger = $this->createMock(LoggerInterface::class); $eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger); $this->eventDispatcher = new SymfonyAdapter($eventDispatcher, $logger); - $this->hooks = new LegacyHooks($this->eventDispatcher); + $this->hooks = new LegacyHooks($eventDispatcher); $this->manager = \OC::$server->getShareManager(); } diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 0e56592e1a524..a23aa6023af33 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -545,7 +545,7 @@ public function testVerifyPasswordHook() { ['core', 'shareapi_enforce_links_password', 'no', 'no'], ]); - $this->eventDispatcher->expects($this->once())->method('dispatch') + $this->dispatcher->expects($this->once())->method('dispatchTyped') ->willReturnCallback(function (Event $event) { $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event); /** @var ValidatePasswordPolicyEvent $event */ @@ -567,7 +567,7 @@ public function testVerifyPasswordHookFails() { ['core', 'shareapi_enforce_links_password', 'no', 'no'], ]); - $this->eventDispatcher->expects($this->once())->method('dispatch') + $this->dispatcher->expects($this->once())->method('dispatchTyped') ->willReturnCallback(function (Event $event) { $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event); /** @var ValidatePasswordPolicyEvent $event */