adjust Calendar resource / room interfaces to use class implementation

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
Georg Ehrke 2018-08-24 15:21:04 +02:00
parent 4c47ec005f
commit 1c6f666bbf
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
7 changed files with 144 additions and 58 deletions

View File

@ -113,8 +113,12 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
foreach($sortedResources['new'] as $backendId => $newResources) {
foreach ($newResources as $newResource) {
$resource = $this->resourceManager->getBackend($backendId)
->getResource($newResource);
$backend = $this->resourceManager->getBackend($backendId);
if ($backend === null) {
continue;
}
$resource = $backend->getResource($newResource);
$this->addToCache($this->resourceDbTable, $resource);
}
}
@ -126,8 +130,12 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
}
foreach($sortedResources['edited'] as $backendId => $editedResources) {
foreach ($editedResources as $editedResource) {
$resource = $this->resourceManager->getBackend($backendId)
->getResource($editedResource);
$backend = $this->resourceManager->getBackend($backendId);
if ($backend === null) {
continue;
}
$resource = $backend->getResource($editedResource);
$this->updateCache($this->resourceDbTable, $resource);
}
}
@ -157,8 +165,12 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
foreach($sortedRooms['new'] as $backendId => $newRooms) {
foreach ($newRooms as $newRoom) {
$resource = $this->roomManager->getBackend($backendId)
->getRoom($newRoom);
$backend = $this->roomManager->getBackend($backendId);
if ($backend === null) {
continue;
}
$resource = $backend->getRoom($newRoom);
$this->addToCache($this->roomDbTable, $resource);
}
}
@ -170,8 +182,12 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
}
foreach($sortedRooms['edited'] as $backendId => $editedRooms) {
foreach ($editedRooms as $editedRoom) {
$resource = $this->roomManager->getBackend($backendId)
->getRoom($editedRoom);
$backend = $this->roomManager->getBackend($backendId);
if ($backend === null) {
continue;
}
$resource = $backend->getRoom($editedRoom);
$this->updateCache($this->roomDbTable, $resource);
}
}

View File

@ -27,49 +27,65 @@ use OCP\Calendar\Resource\IBackend;
class Manager implements \OCP\Calendar\Resource\IManager {
/** @var IBackend[] holds all registered resource backends */
/** @var string[] holds all registered resource backends */
private $backends = [];
/** @var IBackend[] holds all backends that have been initialized already */
private $initializedBackends = [];
/**
* Registers a resource backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function registerBackend(IBackend $backend) {
$this->backends[$backend->getBackendIdentifier()] = $backend;
public function registerBackend(string $backendClass) {
$this->backends[$backendClass] = $backendClass;
}
/**
* Unregisters a resource backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function unregisterBackend(IBackend $backend) {
unset($this->backends[$backend->getBackendIdentifier()]);
public function unregisterBackend(string $backendClass) {
unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
}
/**
* @return IBackend[]
* @throws \OCP\AppFramework\QueryException
* @since 14.0.0
*/
public function getBackends():array {
return array_values($this->backends);
foreach($this->backends as $backend) {
if (isset($this->initializedBackends[$backend])) {
continue;
}
$this->initializedBackends[$backend] = \OC::$server->query($backend);
}
return array_values($this->initializedBackends);
}
/**
* @param string $backendId
* @throws \OCP\AppFramework\QueryException
* @return IBackend|null
*/
public function getBackend($backendId):IBackend {
if (!isset($this->backends[$backendId])) {
return null;
public function getBackend($backendId) {
$backends = $this->getBackends();
foreach($backends as $backend) {
if ($backend->getBackendIdentifier() === $backendId) {
return $backend;
}
}
return $this->backends[$backendId];
return null;
}
/**
@ -79,5 +95,6 @@ class Manager implements \OCP\Calendar\Resource\IManager {
*/
public function clear() {
$this->backends = [];
$this->initializedBackends = [];
}
}

View File

@ -27,49 +27,65 @@ use OCP\Calendar\Room\IBackend;
class Manager implements \OCP\Calendar\Room\IManager {
/** @var IBackend[] holds all registered resource backends */
/** @var string[] holds all registered resource backends */
private $backends = [];
/** @var IBackend[] holds all backends that have been initialized already */
private $initializedBackends = [];
/**
* Registers a resource backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function registerBackend(IBackend $backend) {
$this->backends[$backend->getBackendIdentifier()] = $backend;
public function registerBackend(string $backendClass) {
$this->backends[$backendClass] = $backendClass;
}
/**
* Unregisters a resource backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function unregisterBackend(IBackend $backend) {
unset($this->backends[$backend->getBackendIdentifier()]);
public function unregisterBackend(string $backendClass) {
unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
}
/**
* @return IBackend[]
* @throws \OCP\AppFramework\QueryException
* @since 14.0.0
*/
public function getBackends():array {
return array_values($this->backends);
foreach($this->backends as $backend) {
if (isset($this->initializedBackends[$backend])) {
continue;
}
$this->initializedBackends[$backend] = \OC::$server->query($backend);
}
return array_values($this->initializedBackends);
}
/**
* @param string $backendId
* @throws \OCP\AppFramework\QueryException
* @return IBackend|null
*/
public function getBackend($backendId):IBackend {
if (!isset($this->backends[$backendId])) {
return null;
public function getBackend($backendId) {
$backends = $this->getBackends();
foreach($backends as $backend) {
if ($backend->getBackendIdentifier() === $backendId) {
return $backend;
}
}
return $this->backends[$backendId];
return null;
}
/**
@ -79,5 +95,6 @@ class Manager implements \OCP\Calendar\Room\IManager {
*/
public function clear() {
$this->backends = [];
$this->initializedBackends = [];
}
}

View File

@ -34,20 +34,20 @@ interface IManager {
/**
* Registers a resource backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function registerBackend(IBackend $backend);
public function registerBackend(string $backendClass);
/**
* Unregisters a resource backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function unregisterBackend(IBackend $backend);
public function unregisterBackend(string $backendClass);
/**
* @return IBackend[]
@ -57,10 +57,10 @@ interface IManager {
/**
* @param string $backendId
* @return IBackend
* @return IBackend|null
* @since 14.0.0
*/
public function getBackend($backendId):IBackend;
public function getBackend($backendId);
/**
* removes all registered backend instances

View File

@ -34,20 +34,20 @@ interface IManager {
/**
* Registers a room backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function registerBackend(IBackend $backend);
public function registerBackend(string $backendClass);
/**
* Unregisters a room backend
*
* @param IBackend $backend
* @param string $backendClass
* @return void
* @since 14.0.0
*/
public function unregisterBackend(IBackend $backend);
public function unregisterBackend(string $backendClass);
/**
* @return IBackend[]
@ -57,10 +57,10 @@ interface IManager {
/**
* @param string $backendId
* @return IBackend
* @return IBackend|null
* @since 14.0.0
*/
public function getBackend($backendId):IBackend;
public function getBackend($backendId);
/**
* removes all registered backend instances

View File

@ -41,18 +41,24 @@ class ManagerTest extends TestCase {
public function testRegisterUnregisterBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
\OC::$server->registerService('calendar_resource_backend1', function () use ($backend1) {
return $backend1;
});
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
\OC::$server->registerService('calendar_resource_backend2', function () use ($backend2) {
return $backend2;
});
$this->manager->registerBackend($backend1);
$this->manager->registerBackend($backend2);
$this->manager->registerBackend('calendar_resource_backend1');
$this->manager->registerBackend('calendar_resource_backend2');
$this->assertEquals([
$backend1, $backend2
], $this->manager->getBackends());
$this->manager->unregisterBackend($backend1);
$this->manager->unregisterBackend('calendar_resource_backend1');
$this->assertEquals([
$backend2
@ -62,12 +68,18 @@ class ManagerTest extends TestCase {
public function testGetBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
\OC::$server->registerService('calendar_resource_backend1', function () use ($backend1) {
return $backend1;
});
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
\OC::$server->registerService('calendar_resource_backend2', function () use ($backend2) {
return $backend2;
});
$this->manager->registerBackend($backend1);
$this->manager->registerBackend($backend2);
$this->manager->registerBackend('calendar_resource_backend1');
$this->manager->registerBackend('calendar_resource_backend2');
$this->assertEquals($backend1, $this->manager->getBackend('backend_1'));
$this->assertEquals($backend2, $this->manager->getBackend('backend_2'));
@ -76,12 +88,18 @@ class ManagerTest extends TestCase {
public function testClear() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
\OC::$server->registerService('calendar_resource_backend1', function () use ($backend1) {
return $backend1;
});
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
\OC::$server->registerService('calendar_resource_backend2', function () use ($backend2) {
return $backend2;
});
$this->manager->registerBackend($backend1);
$this->manager->registerBackend($backend2);
$this->manager->registerBackend('calendar_resource_backend1');
$this->manager->registerBackend('calendar_resource_backend2');
$this->assertEquals([
$backend1, $backend2

View File

@ -41,18 +41,24 @@ class ManagerTest extends TestCase {
public function testRegisterUnregisterBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
\OC::$server->registerService('calendar_room_backend1', function () use ($backend1) {
return $backend1;
});
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
\OC::$server->registerService('calendar_room_backend2', function () use ($backend2) {
return $backend2;
});
$this->manager->registerBackend($backend1);
$this->manager->registerBackend($backend2);
$this->manager->registerBackend('calendar_room_backend1');
$this->manager->registerBackend('calendar_room_backend2');
$this->assertEquals([
$backend1, $backend2
], $this->manager->getBackends());
$this->manager->unregisterBackend($backend1);
$this->manager->unregisterBackend('calendar_room_backend1');
$this->assertEquals([
$backend2
@ -62,12 +68,18 @@ class ManagerTest extends TestCase {
public function testGetBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
\OC::$server->registerService('calendar_room_backend1', function () use ($backend1) {
return $backend1;
});
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
\OC::$server->registerService('calendar_room_backend2', function () use ($backend2) {
return $backend2;
});
$this->manager->registerBackend($backend1);
$this->manager->registerBackend($backend2);
$this->manager->registerBackend('calendar_room_backend1');
$this->manager->registerBackend('calendar_room_backend2');
$this->assertEquals($backend1, $this->manager->getBackend('backend_1'));
$this->assertEquals($backend2, $this->manager->getBackend('backend_2'));
@ -76,12 +88,18 @@ class ManagerTest extends TestCase {
public function testClear() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
\OC::$server->registerService('calendar_room_backend1', function () use ($backend1) {
return $backend1;
});
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
\OC::$server->registerService('calendar_room_backend2', function () use ($backend2) {
return $backend2;
});
$this->manager->registerBackend($backend1);
$this->manager->registerBackend($backend2);
$this->manager->registerBackend('calendar_room_backend1');
$this->manager->registerBackend('calendar_room_backend2');
$this->assertEquals([
$backend1, $backend2