Skip to content

Commit

Permalink
fix: Move Sharing events to IEventDispatcher
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Jul 26, 2023
1 parent 37ab7f7 commit 3b6e065
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
47 changes: 31 additions & 16 deletions lib/private/Share20/LegacyHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/Share20/LegacyHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
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;

class LegacyHooksTest extends TestCase {
/** @var LegacyHooks */
private $hooks;

/** @var EventDispatcher */
/** @var IEventDispatcher */
private $eventDispatcher;

/** @var Manager */
Expand All @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Share20/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down

0 comments on commit 3b6e065

Please sign in to comment.