aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2023-07-27 19:57:10 +0200
committerJoas Schilling <coding@schilljs.com>2023-07-28 14:11:19 +0200
commit68fc9b48c24bf810488c5910da2feadd10587e51 (patch)
treecf183d9afa8f2fa5a9342341f4cf21d51bc6ad50
parent9bf812ac6c29f7722ba6db876dbabbc1b065dce1 (diff)
downloadnextcloud-server-68fc9b48c24bf810488c5910da2feadd10587e51.tar.gz
nextcloud-server-68fc9b48c24bf810488c5910da2feadd10587e51.zip
feat!: Migrate AccountManager event to typed event
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--apps/dav/lib/AppInfo/Application.php12
-rw-r--r--apps/lookup_server_connector/lib/AppInfo/Application.php14
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Accounts/AccountManager.php14
-rw-r--r--lib/public/Accounts/UserUpdatedEvent.php58
-rw-r--r--tests/lib/Accounts/AccountManagerTest.php21
7 files changed, 87 insertions, 34 deletions
diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php
index 4ed68e68840..9b185963dcc 100644
--- a/apps/dav/lib/AppInfo/Application.php
+++ b/apps/dav/lib/AppInfo/Application.php
@@ -69,6 +69,7 @@ use OCA\DAV\Events\CardDeletedEvent;
use OCA\DAV\Events\CardUpdatedEvent;
use OCA\DAV\Events\SubscriptionCreatedEvent;
use OCA\DAV\Events\SubscriptionDeletedEvent;
+use OCP\Accounts\UserUpdatedEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\Events\TrustedServerRemovedEvent;
use OCA\DAV\HookManager;
@@ -224,13 +225,10 @@ class Application extends App implements IBootstrap {
}
});
- $dispatcher->addListener('OC\AccountManager::userUpdated', function ($event) use ($container) {
- if ($event instanceof GenericEvent) {
- $user = $event->getSubject();
- /** @var SyncService $syncService */
- $syncService = $container->query(SyncService::class);
- $syncService->updateUser($user);
- }
+ $dispatcher->addListener(UserUpdatedEvent::class, function (UserUpdatedEvent $event) use ($container) {
+ /** @var SyncService $syncService */
+ $syncService = \OCP\Server::get(SyncService::class);
+ $syncService->updateUser($event->getUser());
});
diff --git a/apps/lookup_server_connector/lib/AppInfo/Application.php b/apps/lookup_server_connector/lib/AppInfo/Application.php
index 8d6700c0eaa..fbcf54f83de 100644
--- a/apps/lookup_server_connector/lib/AppInfo/Application.php
+++ b/apps/lookup_server_connector/lib/AppInfo/Application.php
@@ -30,6 +30,7 @@ namespace OCA\LookupServerConnector\AppInfo;
use Closure;
use OCA\LookupServerConnector\UpdateLookupServer;
+use OCP\Accounts\UserUpdatedEvent;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -58,15 +59,10 @@ class Application extends App implements IBootstrap {
*/
private function registerEventListeners(IEventDispatcher $dispatcher,
ContainerInterface $appContainer): void {
- $dispatcher->addListener('OC\AccountManager::userUpdated', function ($event) use ($appContainer) {
- if ($event instanceof GenericEvent) {
- /** @var IUser $user */
- $user = $event->getSubject();
-
- /** @var UpdateLookupServer $updateLookupServer */
- $updateLookupServer = $appContainer->get(UpdateLookupServer::class);
- $updateLookupServer->userUpdated($user);
- }
+ $dispatcher->addListener(UserUpdatedEvent::class, function (UserUpdatedEvent $event) use ($appContainer) {
+ /** @var UpdateLookupServer $updateLookupServer */
+ $updateLookupServer = $appContainer->get(UpdateLookupServer::class);
+ $updateLookupServer->userUpdated($event->getUser());
});
}
}
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 38cad1e9eb4..1093b8a2586 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -12,6 +12,7 @@ return array(
'OCP\\Accounts\\IAccountProperty' => $baseDir . '/lib/public/Accounts/IAccountProperty.php',
'OCP\\Accounts\\IAccountPropertyCollection' => $baseDir . '/lib/public/Accounts/IAccountPropertyCollection.php',
'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php',
+ 'OCP\\Accounts\\UserUpdatedEvent' => $baseDir . '/lib/public/Accounts/UserUpdatedEvent.php',
'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php',
'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php',
'OCP\\Activity\\IEvent' => $baseDir . '/lib/public/Activity/IEvent.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 9083632b384..4a74dda6fab 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -45,6 +45,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Accounts\\IAccountProperty' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountProperty.php',
'OCP\\Accounts\\IAccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountPropertyCollection.php',
'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php',
+ 'OCP\\Accounts\\UserUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/Accounts/UserUpdatedEvent.php',
'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php',
'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php',
'OCP\\Activity\\IEvent' => __DIR__ . '/../../..' . '/lib/public/Activity/IEvent.php',
diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php
index 43d18a0d941..95199d8380c 100644
--- a/lib/private/Accounts/AccountManager.php
+++ b/lib/private/Accounts/AccountManager.php
@@ -41,6 +41,7 @@ use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use OC\Profile\TProfileHelper;
+use OCP\Accounts\UserUpdatedEvent;
use OCP\Cache\CappedMemoryCache;
use OCA\Settings\BackgroundJobs\VerifyUserData;
use OCP\Accounts\IAccount;
@@ -51,6 +52,7 @@ use OCP\Accounts\PropertyDoesNotExistException;
use OCP\BackgroundJob\IJobList;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
@@ -62,8 +64,6 @@ use OCP\Security\ICrypto;
use OCP\Security\VerificationToken\IVerificationToken;
use OCP\Util;
use Psr\Log\LoggerInterface;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
use function array_flip;
use function iterator_to_array;
use function json_decode;
@@ -109,7 +109,7 @@ class AccountManager implements IAccountManager {
public function __construct(
private IDBConnection $connection,
private IConfig $config,
- private EventDispatcherInterface $eventDispatcher,
+ private IEventDispatcher $dispatcher,
private IJobList $jobList,
private LoggerInterface $logger,
private IVerificationToken $verificationToken,
@@ -255,10 +255,10 @@ class AccountManager implements IAccountManager {
}
if ($updated) {
- $this->eventDispatcher->dispatch(
- 'OC\AccountManager::userUpdated',
- new GenericEvent($user, $data)
- );
+ $this->dispatcher->dispatchTyped(new UserUpdatedEvent(
+ $user,
+ $data,
+ ));
}
return $data;
diff --git a/lib/public/Accounts/UserUpdatedEvent.php b/lib/public/Accounts/UserUpdatedEvent.php
new file mode 100644
index 00000000000..e6c73f1d1c1
--- /dev/null
+++ b/lib/public/Accounts/UserUpdatedEvent.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCP\Accounts;
+
+use OCP\EventDispatcher\Event;
+use OCP\IUser;
+
+/**
+ * @since 28.0.0
+ */
+class UserUpdatedEvent extends Event {
+ /**
+ * @since 28.0.0
+ */
+ public function __construct(
+ protected IUser $user,
+ protected array $data,
+ ) {
+ parent::__construct();
+ }
+
+ /**
+ * @since 28.0.0
+ */
+ public function getUser(): IUser {
+ return $this->user;
+ }
+
+ /**
+ * @since 28.0.0
+ */
+ public function getData(): array {
+ return $this->data;
+ }
+}
diff --git a/tests/lib/Accounts/AccountManagerTest.php b/tests/lib/Accounts/AccountManagerTest.php
index 3a3405f18a0..d12dfbfacea 100644
--- a/tests/lib/Accounts/AccountManagerTest.php
+++ b/tests/lib/Accounts/AccountManagerTest.php
@@ -28,8 +28,10 @@ use OC\Accounts\Account;
use OC\Accounts\AccountManager;
use OCA\Settings\BackgroundJobs\VerifyUserData;
use OCP\Accounts\IAccountManager;
+use OCP\Accounts\UserUpdatedEvent;
use OCP\BackgroundJob\IJobList;
use OCP\Defaults;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IURLGenerator;
@@ -40,8 +42,6 @@ use OCP\Security\ICrypto;
use OCP\Security\VerificationToken\IVerificationToken;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
/**
@@ -70,7 +70,7 @@ class AccountManagerTest extends TestCase {
/** @var IConfig|MockObject */
private $config;
- /** @var EventDispatcherInterface|MockObject */
+ /** @var IEventDispatcher|MockObject */
private $eventDispatcher;
/** @var IJobList|MockObject */
@@ -86,7 +86,7 @@ class AccountManagerTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
+ $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->connection = \OC::$server->get(IDBConnection::class);
$this->config = $this->createMock(IConfig::class);
$this->jobList = $this->createMock(IJobList::class);
@@ -502,15 +502,14 @@ class AccountManagerTest extends TestCase {
if (!$insertNew && !$updateExisting) {
$accountManager->expects($this->never())->method('updateExistingUser');
$accountManager->expects($this->never())->method('insertNewUser');
- $this->eventDispatcher->expects($this->never())->method('dispatch');
+ $this->eventDispatcher->expects($this->never())->method('dispatchTyped');
} else {
- $this->eventDispatcher->expects($this->once())->method('dispatch')
+ $this->eventDispatcher->expects($this->once())->method('dispatchTyped')
->willReturnCallback(
- function ($eventName, $event) use ($user, $newData) {
- $this->assertSame('OC\AccountManager::userUpdated', $eventName);
- $this->assertInstanceOf(GenericEvent::class, $event);
- $this->assertSame($user, $event->getSubject());
- $this->assertSame($newData, $event->getArguments());
+ function ($event) use ($user, $newData) {
+ $this->assertInstanceOf(UserUpdatedEvent::class, $event);
+ $this->assertSame($user, $event->getUser());
+ $this->assertSame($newData, $event->getData());
}
);
}