Skip to content

Commit

Permalink
Add event dispatcher to OCP
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Jun 25, 2019
1 parent 817bdc4 commit 3174012
Show file tree
Hide file tree
Showing 37 changed files with 524 additions and 123 deletions.
6 changes: 3 additions & 3 deletions apps/dav/lib/HookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class HookManager {

Expand All @@ -57,14 +57,14 @@ class HookManager {
/** @var array */
private $addressBooksToDelete = [];

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

public function __construct(IUserManager $userManager,
SyncService $syncService,
CalDavBackend $calDav,
CardDavBackend $cardDav,
EventDispatcher $eventDispatcher) {
EventDispatcherInterface $eventDispatcher) {
$this->userManager = $userManager;
$this->syncService = $syncService;
$this->calDav = $calDav;
Expand Down
14 changes: 2 additions & 12 deletions apps/dav/tests/unit/CardDAV/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,17 @@
use OCP\IImage;
use OCP\IUser;
use PHPUnit_Framework_MockObject_MockObject;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Test\TestCase;

class ConverterTest extends TestCase {

/** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */
private $accountManager;

/** @var EventDispatcher | PHPUnit_Framework_MockObject_MockObject */
private $eventDispatcher;

/** @var IDBConnection | PHPUnit_Framework_MockObject_MockObject */
private $databaseConnection;

public function setUp() {
parent::setUp();
$this->databaseConnection = $this->getMockBuilder(IDBConnection::class)->getMock();
$this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()->getMock();
$this->accountManager = $this->getMockBuilder(AccountManager::class)
->disableOriginalConstructor()->getMock();

$this->accountManager = $this->createMock(AccountManager::class);
}

public function getAccountManager(IUser $user) {
Expand Down
6 changes: 3 additions & 3 deletions apps/dav/tests/unit/DAV/HookManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;

class HookManagerTest extends TestCase {
/** @var IL10N */
private $l10n;

/** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
/** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */
private $eventDispatcher;

public function setUp() {
parent::setUp();
$this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class)->disableOriginalConstructor()->getMock();
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->l10n = $this->createMock(IL10N::class);
$this->l10n
->expects($this->any())
Expand Down
8 changes: 4 additions & 4 deletions apps/files_trashbin/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
use OCP\Files\Node;
use OCP\ILogger;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Storage extends Wrapper {
/** @var IMountPoint */
Expand All @@ -50,7 +50,7 @@ class Storage extends Wrapper {
/** @var ILogger */
private $logger;

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

/** @var IRootFolder */
Expand All @@ -66,15 +66,15 @@ class Storage extends Wrapper {
* @param ITrashManager $trashManager
* @param IUserManager|null $userManager
* @param ILogger|null $logger
* @param EventDispatcher|null $eventDispatcher
* @param EventDispatcherInterface|null $eventDispatcher
* @param IRootFolder|null $rootFolder
*/
public function __construct(
$parameters,
ITrashManager $trashManager = null,
IUserManager $userManager = null,
ILogger $logger = null,
EventDispatcher $eventDispatcher = null,
EventDispatcherInterface $eventDispatcher = null,
IRootFolder $rootFolder = null
) {
$this->mountPoint = $parameters['mountPoint'];
Expand Down
5 changes: 2 additions & 3 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
use OCP\Files\Node;
use OCP\ILogger;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class Storage
Expand Down Expand Up @@ -543,8 +543,7 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab
$userManager->expects($this->any())
->method('userExists')->willReturn($userExists);
$logger = $this->getMockBuilder(ILogger::class)->getMock();
$eventDispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()->getMock();
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$rootFolder = $this->createMock(IRootFolder::class);
$node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock();
$trashManager = $this->createMock(ITrashManager::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
36 changes: 8 additions & 28 deletions apps/twofactor_backupcodes/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@
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() {
Expand All @@ -62,32 +60,14 @@ public function registerHooksAndEvents() {
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() {
Expand Down
3 changes: 2 additions & 1 deletion apps/twofactor_backupcodes/lib/Event/CodesGenerated.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@

namespace OCA\TwoFactorBackupCodes\Event;

use OCP\EventDispatcher\Event;
use OCP\IUser;
use Symfony\Component\EventDispatcher\Event;

class CodesGenerated extends Event {

/** @var IUser */
private $user;

public function __construct(IUser $user) {
parent::__construct();
$this->user = $user;
}

Expand Down
10 changes: 6 additions & 4 deletions apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@
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;

/** @var ILogger */
private $logger;

public function __construct(IManager $activityManager, ILogger $logger) {
public function __construct(IManager $activityManager,
ILogger $logger) {
$this->activityManager = $activityManager;
$this->logger = $logger;
}

/**
* 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')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
*
Expand All @@ -25,10 +27,11 @@
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;
Expand All @@ -37,7 +40,7 @@ public function __construct(IManager $manager) {
$this->manager = $manager;
}

public function handle(Event $event) {
public function handle(Event $event): void {
if (!($event instanceof CodesGenerated)) {
return;
}
Expand Down
10 changes: 6 additions & 4 deletions apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
Expand All @@ -22,16 +24,16 @@
*
*/


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;
Expand All @@ -45,7 +47,7 @@ public function __construct(IRegistry $registry,
$this->jobList = $jobList;
}

public function handle(Event $event) {
public function handle(Event $event): void {
if (!($event instanceof RegistryEvent)) {
return;
}
Expand Down
7 changes: 4 additions & 3 deletions apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
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;
Expand All @@ -44,7 +45,7 @@ public function __construct(IRegistry $registry,
$this->jobList = $jobList;
}

public function handle(Event $event) {
public function handle(Event $event): void {
if (!($event instanceof RegistryEvent)) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
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;
Expand All @@ -42,9 +43,10 @@ public function __construct(IRegistry $registry, BackupCodesProvider $provider)
$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());
}
}

}
5 changes: 3 additions & 2 deletions apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 3174012

Please sign in to comment.