diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-05-28 19:46:36 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-06-25 10:02:27 +0200 |
commit | 3174012adf3fde4de74efa12cfa7e480b874b295 (patch) | |
tree | bc58b99dc45b5adcdeaf30269e9a6eab2c853773 /apps/twofactor_backupcodes | |
parent | 817bdc47c804ae8511ad3423eae216f6ccdee6c6 (diff) | |
download | nextcloud-server-3174012adf3fde4de74efa12cfa7e480b874b295.tar.gz nextcloud-server-3174012adf3fde4de74efa12cfa7e480b874b295.zip |
Add event dispatcher to OCP
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/twofactor_backupcodes')
17 files changed, 50 insertions, 93 deletions
diff --git a/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php b/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php index 2038421a3a2..cf5eae2a5df 100644 --- a/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php +++ b/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php @@ -16,7 +16,6 @@ return array( 'OCA\\TwoFactorBackupCodes\\Event\\CodesGenerated' => $baseDir . '/../lib/Event/CodesGenerated.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ActivityPublisher' => $baseDir . '/../lib/Listener/ActivityPublisher.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ClearNotifications' => $baseDir . '/../lib/Listener/ClearNotifications.php', - 'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => $baseDir . '/../lib/Listener/IListener.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderDisabled' => $baseDir . '/../lib/Listener/ProviderDisabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => $baseDir . '/../lib/Listener/ProviderEnabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => $baseDir . '/../lib/Listener/RegistryUpdater.php', diff --git a/apps/twofactor_backupcodes/composer/composer/autoload_static.php b/apps/twofactor_backupcodes/composer/composer/autoload_static.php index d1f124a407d..3dc21274529 100644 --- a/apps/twofactor_backupcodes/composer/composer/autoload_static.php +++ b/apps/twofactor_backupcodes/composer/composer/autoload_static.php @@ -31,7 +31,6 @@ class ComposerStaticInitTwoFactorBackupCodes 'OCA\\TwoFactorBackupCodes\\Event\\CodesGenerated' => __DIR__ . '/..' . '/../lib/Event/CodesGenerated.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ActivityPublisher' => __DIR__ . '/..' . '/../lib/Listener/ActivityPublisher.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ClearNotifications' => __DIR__ . '/..' . '/../lib/Listener/ClearNotifications.php', - 'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => __DIR__ . '/..' . '/../lib/Listener/IListener.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderDisabled' => __DIR__ . '/..' . '/../lib/Listener/ProviderDisabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => __DIR__ . '/..' . '/../lib/Listener/ProviderEnabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => __DIR__ . '/..' . '/../lib/Listener/RegistryUpdater.php', diff --git a/apps/twofactor_backupcodes/lib/AppInfo/Application.php b/apps/twofactor_backupcodes/lib/AppInfo/Application.php index fc6c94d5b7a..041af037067 100644 --- a/apps/twofactor_backupcodes/lib/AppInfo/Application.php +++ b/apps/twofactor_backupcodes/lib/AppInfo/Application.php @@ -29,18 +29,16 @@ use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; -use OCA\TwoFactorBackupCodes\Listener\IListener; use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled; use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled; use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; use OCA\TwoFactorBackupCodes\Notifications\Notifier; use OCP\AppFramework\App; use OCP\Authentication\TwoFactorAuth\IRegistry; -use OCP\Authentication\TwoFactorAuth\RegistryEvent; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IL10N; use OCP\Notification\IManager; use OCP\Util; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Application extends App { public function __construct() { @@ -62,32 +60,14 @@ class Application extends App { Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser'); $container = $this->getContainer(); - /** @var EventDispatcherInterface $eventDispatcher */ - $eventDispatcher = $container->query(EventDispatcherInterface::class); - $eventDispatcher->addListener(CodesGenerated::class, function (CodesGenerated $event) use ($container) { - /** @var IListener[] $listeners */ - $listeners = [ - $container->query(ActivityPublisher::class), - $container->query(RegistryUpdater::class), - $container->query(ClearNotifications::class), - ]; - foreach ($listeners as $listener) { - $listener->handle($event); - } - }); - - $eventDispatcher->addListener(IRegistry::EVENT_PROVIDER_ENABLED, function(RegistryEvent $event) use ($container) { - /** @var IListener $listener */ - $listener = $container->query(ProviderEnabled::class); - $listener->handle($event); - }); - - $eventDispatcher->addListener(IRegistry::EVENT_PROVIDER_DISABLED, function(RegistryEvent $event) use ($container) { - /** @var IListener $listener */ - $listener = $container->query(ProviderDisabled::class); - $listener->handle($event); - }); + /** @var IEventDispatcher $eventDispatcher */ + $eventDispatcher = $container->query(IEventDispatcher::class); + $eventDispatcher->addServiceListener(CodesGenerated::class, ActivityPublisher::class); + $eventDispatcher->addServiceListener(CodesGenerated::class, RegistryUpdater::class); + $eventDispatcher->addServiceListener(CodesGenerated::class, ClearNotifications::class); + $eventDispatcher->addServiceListener(IRegistry::EVENT_PROVIDER_ENABLED, ProviderEnabled::class); + $eventDispatcher->addServiceListener(IRegistry::EVENT_PROVIDER_DISABLED, ProviderDisabled::class); } public function registerNotification() { diff --git a/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php b/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php index c4ffcb37601..1724053281b 100644 --- a/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php +++ b/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php @@ -24,8 +24,8 @@ declare(strict_types=1); namespace OCA\TwoFactorBackupCodes\Event; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; class CodesGenerated extends Event { @@ -33,6 +33,7 @@ class CodesGenerated extends Event { private $user; public function __construct(IUser $user) { + parent::__construct(); $this->user = $user; } diff --git a/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php b/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php index 31e2ac6e508..75a5937b5fa 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php +++ b/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php @@ -27,10 +27,11 @@ namespace OCA\TwoFactorBackupCodes\Listener; use BadMethodCallException; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCP\Activity\IManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; use OCP\ILogger; -use Symfony\Component\EventDispatcher\Event; -class ActivityPublisher implements IListener { +class ActivityPublisher implements IEventListener { /** @var IManager */ private $activityManager; @@ -38,7 +39,8 @@ class ActivityPublisher implements IListener { /** @var ILogger */ private $logger; - public function __construct(IManager $activityManager, ILogger $logger) { + public function __construct(IManager $activityManager, + ILogger $logger) { $this->activityManager = $activityManager; $this->logger = $logger; } @@ -46,7 +48,7 @@ class ActivityPublisher implements IListener { /** * Push an event to the user's activity stream */ - public function handle(Event $event) { + public function handle(Event $event): void { if ($event instanceof CodesGenerated) { $activity = $this->activityManager->generateEvent(); $activity->setApp('twofactor_backupcodes') diff --git a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php index ad7fd188ebc..eb0f7363aab 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php +++ b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php @@ -1,5 +1,7 @@ <?php + declare(strict_types=1); + /** * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> * @@ -25,10 +27,11 @@ declare(strict_types=1); namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; use OCP\Notification\IManager; -use Symfony\Component\EventDispatcher\Event; -class ClearNotifications implements IListener { +class ClearNotifications implements IEventListener { /** @var IManager */ private $manager; @@ -37,7 +40,7 @@ class ClearNotifications implements IListener { $this->manager = $manager; } - public function handle(Event $event) { + public function handle(Event $event): void { if (!($event instanceof CodesGenerated)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/IListener.php b/apps/twofactor_backupcodes/lib/Listener/IListener.php deleted file mode 100644 index ec45de5c075..00000000000 --- a/apps/twofactor_backupcodes/lib/Listener/IListener.php +++ /dev/null @@ -1,33 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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 OCA\TwoFactorBackupCodes\Listener; - -use Symfony\Component\EventDispatcher\Event; - -interface IListener { - - public function handle(Event $event); - -} diff --git a/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php b/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php index 835eb0394f9..bf2899f391f 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php +++ b/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php @@ -1,5 +1,7 @@ <?php + declare(strict_types=1); + /** * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> * @@ -22,16 +24,16 @@ declare(strict_types=1); * */ - namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; -use Symfony\Component\EventDispatcher\Event; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -class ProviderDisabled implements IListener { +class ProviderDisabled implements IEventListener { /** @var IRegistry */ private $registry; @@ -45,7 +47,7 @@ class ProviderDisabled implements IListener { $this->jobList = $jobList; } - public function handle(Event $event) { + public function handle(Event $event): void { if (!($event instanceof RegistryEvent)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php b/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php index 48cbef66f1b..7c258213384 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php +++ b/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php @@ -28,9 +28,10 @@ use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; -use Symfony\Component\EventDispatcher\Event; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -class ProviderEnabled implements IListener { +class ProviderEnabled implements IEventListener { /** @var IRegistry */ private $registry; @@ -44,7 +45,7 @@ class ProviderEnabled implements IListener { $this->jobList = $jobList; } - public function handle(Event $event) { + public function handle(Event $event): void { if (!($event instanceof RegistryEvent)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php b/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php index 95b0db090ee..93e4959b24b 100644 --- a/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php +++ b/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php @@ -27,9 +27,10 @@ namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider; use OCP\Authentication\TwoFactorAuth\IRegistry; -use Symfony\Component\EventDispatcher\Event; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -class RegistryUpdater implements IListener { +class RegistryUpdater implements IEventListener { /** @var IRegistry */ private $registry; @@ -42,9 +43,10 @@ class RegistryUpdater implements IListener { $this->provider = $provider; } - public function handle(Event $event) { + public function handle(Event $event): void { if ($event instanceof CodesGenerated) { $this->registry->enableProviderFor($this->provider, $event->getUser()); } } + } diff --git a/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php b/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php index 74a032dcc0a..88117c37ec0 100644 --- a/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php +++ b/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php @@ -27,6 +27,7 @@ use OCA\TwoFactorBackupCodes\Db\BackupCode; use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCP\Activity\IManager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\ILogger; use OCP\IUser; use OCP\Security\IHasher; @@ -46,13 +47,13 @@ class BackupCodeStorage { /** @var ISecureRandom */ private $random; - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ private $eventDispatcher; public function __construct(BackupCodeMapper $mapper, ISecureRandom $random, IHasher $hasher, - EventDispatcherInterface $eventDispatcher) { + IEventDispatcher $eventDispatcher) { $this->mapper = $mapper; $this->hasher = $hasher; $this->random = $random; diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php index a6c38701a30..44ea88cdb0b 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php @@ -28,15 +28,15 @@ use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; use OCP\Activity\IEvent; use OCP\Activity\IManager; +use OCP\EventDispatcher\Event; use OCP\ILogger; use OCP\IUser; -use PHPUnit_Framework_MockObject_MockObject; -use Symfony\Component\EventDispatcher\Event; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class ActivityPublisherTest extends TestCase { - /** @var IManager|PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|MockObject */ private $activityManager; /** @var ILogger */ diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php index 123c008cbbb..21dde19d329 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php @@ -26,10 +26,10 @@ namespace OCA\TwoFactorBackupCodes\Tests\Unit\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; +use OCP\EventDispatcher\Event; use OCP\IUser; use OCP\Notification\IManager; use OCP\Notification\INotification; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class ClearNotificationsTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php index 1bd5a7ccab0..39df7a417f6 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php @@ -30,8 +30,8 @@ use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class ProviderDisabledTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php index c824ad8e87a..8a4ce56d7cb 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php @@ -29,8 +29,8 @@ use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class ProviderEnabledTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php index 2e75804661b..7ae6eab3358 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php @@ -28,8 +28,8 @@ use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider; use OCP\Authentication\TwoFactorAuth\IRegistry; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class RegistryUpdaterTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php b/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php index 2aa323a0a73..d962961c251 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php @@ -26,11 +26,11 @@ use OCA\TwoFactorBackupCodes\Db\BackupCode; use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; use OCP\Security\IHasher; use OCP\Security\ISecureRandom; use PHPUnit_Framework_MockObject_MockObject; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class BackupCodeStorageTest extends TestCase { @@ -44,7 +44,7 @@ class BackupCodeStorageTest extends TestCase { /** @var IHasher|PHPUnit_Framework_MockObject_MockObject */ private $hasher; - /** @var EventDispatcherInterface|PHPUnit_Framework_MockObject_MockObject */ + /** @var IEventDispatcher|PHPUnit_Framework_MockObject_MockObject */ private $eventDispatcher; /** @var BackupCodeStorage */ @@ -56,7 +56,7 @@ class BackupCodeStorageTest extends TestCase { $this->mapper = $this->createMock(BackupCodeMapper::class); $this->random = $this->createMock(ISecureRandom::class); $this->hasher = $this->createMock(IHasher::class); - $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->storage = new BackupCodeStorage($this->mapper, $this->random, $this->hasher, $this->eventDispatcher); } |