]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix: Move Sharing events to IEventDispatcher
authorJoas Schilling <coding@schilljs.com>
Tue, 25 Jul 2023 17:30:15 +0000 (19:30 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 27 Jul 2023 07:57:53 +0000 (09:57 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Share20/LegacyHooks.php
lib/private/Share20/Manager.php
tests/lib/Share20/LegacyHooksTest.php
tests/lib/Share20/ManagerTest.php

index feb4604e8848e17dd33773dad17a47b485cedeb1..688b22733840a8c5c7894c6c8bcfd6dc605be74b 100644 (file)
  */
 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) {
                /** @var IShare $share */
                $share = $e->getSubject();
@@ -62,9 +74,6 @@ class LegacyHooks {
                \OC_Hook::emit(Share::class, 'pre_unshare', $formatted);
        }
 
-       /**
-        * @param GenericEvent $e
-        */
        public function postUnshare(GenericEvent $e) {
                /** @var IShare $share */
                $share = $e->getSubject();
@@ -83,9 +92,6 @@ class LegacyHooks {
                \OC_Hook::emit(Share::class, 'post_unshare', $formatted);
        }
 
-       /**
-        * @param GenericEvent $e
-        */
        public function postUnshareFromSelf(GenericEvent $e) {
                /** @var IShare $share */
                $share = $e->getSubject();
index 732bd5bb97d397e191acd27ee74b62b303429bdb..3f5dbd7cd289ad45a50b20238b4836eba2a1642f 100644 (file)
@@ -157,7 +157,7 @@ class Manager implements IManager {
                $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 @@ class Manager implements IManager {
 
                // 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());
                }
index b8b005abd4e2813b05b88efe72acf44053d414d0..54d8e0be72534ca8d0e8433f4d09967aebe38564 100644 (file)
@@ -27,12 +27,12 @@ use OC\EventDispatcher\SymfonyAdapter;
 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 @@ class LegacyHooksTest extends TestCase {
                $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();
        }
 
index 0e56592e1a524f59402abe7f48d075e53a6d6dc2..a23aa6023af33190c262ce5463781b4afbb2e466 100644 (file)
@@ -545,7 +545,7 @@ class ManagerTest extends \Test\TestCase {
                        ['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 @@ class ManagerTest extends \Test\TestCase {
                        ['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 */