Merge pull request #10839 from nextcloud/bugfix/10212/use_class_implementation

adjust Calendar resource / room interfaces to use class implementation
This commit is contained in:
Morris Jobke 2018-10-08 17:00:50 +02:00 committed by GitHub
commit fe2a600823
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 194 additions and 60 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

@ -24,52 +24,81 @@
namespace OC\Calendar\Resource;
use OCP\Calendar\Resource\IBackend;
use OCP\IServerContainer;
class Manager implements \OCP\Calendar\Resource\IManager {
/** @var IBackend[] holds all registered resource backends */
/** @var IServerContainer */
private $server;
/** @var string[] holds all registered resource backends */
private $backends = [];
/** @var IBackend[] holds all backends that have been initialized already */
private $initializedBackends = [];
/**
* Manager constructor.
*
* @param IServerContainer $server
*/
public function __construct(IServerContainer $server) {
$this->server = $server;
}
/**
* 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] = $this->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 +108,6 @@ class Manager implements \OCP\Calendar\Resource\IManager {
*/
public function clear() {
$this->backends = [];
$this->initializedBackends = [];
}
}

View file

@ -24,52 +24,81 @@
namespace OC\Calendar\Room;
use OCP\Calendar\Room\IBackend;
use OCP\IServerContainer;
class Manager implements \OCP\Calendar\Room\IManager {
/** @var IBackend[] holds all registered resource backends */
/** @var IServerContainer */
private $server;
/** @var string[] holds all registered resource backends */
private $backends = [];
/** @var IBackend[] holds all backends that have been initialized already */
private $initializedBackends = [];
/**
* Manager constructor.
*
* @param IServerContainer $server
*/
public function __construct(IServerContainer $server) {
$this->server = $server;
}
/**
* 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] = $this->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 +108,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

@ -25,6 +25,7 @@ namespace Test\Calendar\Resource;
use \OC\Calendar\Resource\Manager;
use \OCP\Calendar\Resource\IBackend;
use OCP\IServerContainer;
use \Test\TestCase;
class ManagerTest extends TestCase {
@ -32,27 +33,39 @@ class ManagerTest extends TestCase {
/** @var Manager */
private $manager;
/** @var IServerContainer */
private $server;
protected function setUp() {
parent::setUp();
$this->manager = new Manager();
$this->server = $this->createMock(IServerContainer::class);
$this->manager = new Manager($this->server);
}
public function testRegisterUnregisterBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
$this->server->expects($this->at(0))
->method('query')
->with('calendar_resource_backend1')
->will($this->returnValue($backend1));
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
$this->server->expects($this->at(1))
->method('query')
->with('calendar_resource_backend2')
->will($this->returnValue($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 +75,20 @@ class ManagerTest extends TestCase {
public function testGetBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
$this->server->expects($this->at(0))
->method('query')
->with('calendar_resource_backend1')
->will($this->returnValue($backend1));
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
$this->server->expects($this->at(1))
->method('query')
->with('calendar_resource_backend2')
->will($this->returnValue($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 +97,20 @@ class ManagerTest extends TestCase {
public function testClear() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
$this->server->expects($this->at(0))
->method('query')
->with('calendar_resource_backend1')
->will($this->returnValue($backend1));
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
$this->server->expects($this->at(1))
->method('query')
->with('calendar_resource_backend2')
->will($this->returnValue($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

@ -25,6 +25,7 @@ namespace Test\Calendar\Room;
use \OC\Calendar\Room\Manager;
use \OCP\Calendar\Room\IBackend;
use OCP\IServerContainer;
use \Test\TestCase;
class ManagerTest extends TestCase {
@ -32,27 +33,39 @@ class ManagerTest extends TestCase {
/** @var Manager */
private $manager;
/** @var IServerContainer */
private $server;
protected function setUp() {
parent::setUp();
$this->manager = new Manager();
$this->server = $this->createMock(IServerContainer::class);
$this->manager = new Manager($this->server);
}
public function testRegisterUnregisterBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
$this->server->expects($this->at(0))
->method('query')
->with('calendar_room_backend1')
->will($this->returnValue($backend1));
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
$this->server->expects($this->at(1))
->method('query')
->with('calendar_room_backend2')
->will($this->returnValue($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 +75,20 @@ class ManagerTest extends TestCase {
public function testGetBackend() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
$this->server->expects($this->at(0))
->method('query')
->with('calendar_room_backend1')
->will($this->returnValue($backend1));
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
$this->server->expects($this->at(1))
->method('query')
->with('calendar_room_backend2')
->will($this->returnValue($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 +97,20 @@ class ManagerTest extends TestCase {
public function testClear() {
$backend1 = $this->createMock(IBackend::class);
$backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1'));
$this->server->expects($this->at(0))
->method('query')
->with('calendar_room_backend1')
->will($this->returnValue($backend1));
$backend2 = $this->createMock(IBackend::class);
$backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2'));
$this->server->expects($this->at(1))
->method('query')
->with('calendar_room_backend2')
->will($this->returnValue($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