Explorar el Código

feat!: Migrate AccountManager event to typed event

Signed-off-by: Joas Schilling <coding@schilljs.com>
Joas Schilling hace 9 meses
padre
commit
68fc9b48c2

+ 5 - 7
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());
 		});
 
 

+ 5 - 9
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());
 		});
 	}
 }

+ 1 - 0
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',

+ 1 - 0
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',

+ 7 - 7
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;

+ 58 - 0
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;
+	}
+}

+ 10 - 11
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());
 					}
 				);
 		}