Implemented a facade for ObjectRetriever to allow static access
This commit is contained in:
80
CloudObjects/SDK/ObjectRetrieverFacade.php
Normal file
80
CloudObjects/SDK/ObjectRetrieverFacade.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
namespace CloudObjects\SDK;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use ML\IRI\IRI;
|
||||||
|
use ML\JsonLD\Node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static facade for ObjectRetriever. Call setObjectRetriever() once to
|
||||||
|
* initialize; all subsequent static calls are forwarded to that instance.
|
||||||
|
*/
|
||||||
|
class ObjectRetrieverFacade {
|
||||||
|
|
||||||
|
private static ?ObjectRetriever $instance = null;
|
||||||
|
|
||||||
|
public static function setObjectRetriever(ObjectRetriever $retriever) : void {
|
||||||
|
self::$instance = $retriever;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getInstance() : ObjectRetriever {
|
||||||
|
if (self::$instance === null)
|
||||||
|
throw new Exception('ObjectRetrieverFacade has not been initialized. Call setObjectRetriever() first.');
|
||||||
|
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setDefaultReader(NodeReader $reader) : ObjectRetriever {
|
||||||
|
return self::getInstance()->setDefaultReader($reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getDefaultReader() : NodeReader {
|
||||||
|
return self::getInstance()->getDefaultReader();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getClient() {
|
||||||
|
return self::getInstance()->getClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCloudObject(IRI $coid) : ?CloudObject {
|
||||||
|
return self::getInstance()->getCloudObject($coid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getObjectNode(IRI $coid) : ?Node {
|
||||||
|
return self::getInstance()->getObjectNode($coid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fetchObjectsInNamespaceWithType(IRI $namespaceCoid, $type) : array {
|
||||||
|
return self::getInstance()->fetchObjectsInNamespaceWithType($namespaceCoid, $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fetchAllObjectsInNamespace(IRI $namespaceCoid) : array {
|
||||||
|
return self::getInstance()->fetchAllObjectsInNamespace($namespaceCoid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCOIDListForNamespace(IRI $namespaceCoid) : array {
|
||||||
|
return self::getInstance()->getCOIDListForNamespace($namespaceCoid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCOIDListForNamespaceWithType(IRI $namespaceCoid, $type) : array {
|
||||||
|
return self::getInstance()->getCOIDListForNamespaceWithType($namespaceCoid, $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAttachment(IRI $coid, $filename) {
|
||||||
|
return self::getInstance()->getAttachment($coid, $filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAuthenticatingNamespaceObjectNode() : ?Node {
|
||||||
|
return self::getInstance()->getAuthenticatingNamespaceObjectNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAuthenticatingNamespaceCloudObject() : ?CloudObject {
|
||||||
|
return self::getInstance()->getAuthenticatingNamespaceCloudObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
76
tests/OfflineTests/ObjectRetrieverFacadeTest.php
Normal file
76
tests/OfflineTests/ObjectRetrieverFacadeTest.php
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
namespace CloudObjects\SDK;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use ML\IRI\IRI;
|
||||||
|
use ML\JsonLD\Node;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
|
class ObjectRetrieverFacadeTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
|
||||||
|
protected function setUp(): void {
|
||||||
|
$prop = new ReflectionProperty(ObjectRetrieverFacade::class, 'instance');
|
||||||
|
$prop->setAccessible(true);
|
||||||
|
$prop->setValue(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThrowsExceptionBeforeInitialization(): void {
|
||||||
|
$this->expectException(Exception::class);
|
||||||
|
ObjectRetrieverFacade::getObjectNode(new IRI('coid://cloudobjects.io'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testForwardsGetObjectNodeToInstance(): void {
|
||||||
|
$coid = new IRI('coid://cloudobjects.io');
|
||||||
|
$mockNode = $this->createMock(Node::class);
|
||||||
|
|
||||||
|
$mockRetriever = $this->createMock(ObjectRetriever::class);
|
||||||
|
$mockRetriever->expects($this->once())
|
||||||
|
->method('getObjectNode')
|
||||||
|
->with($coid)
|
||||||
|
->willReturn($mockNode);
|
||||||
|
|
||||||
|
ObjectRetrieverFacade::setObjectRetriever($mockRetriever);
|
||||||
|
|
||||||
|
$this->assertSame($mockNode, ObjectRetrieverFacade::getObjectNode($coid));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testForwardsGetCloudObjectToInstance(): void {
|
||||||
|
$coid = new IRI('coid://cloudobjects.io');
|
||||||
|
$mockCloudObject = $this->createMock(CloudObject::class);
|
||||||
|
|
||||||
|
$mockRetriever = $this->createMock(ObjectRetriever::class);
|
||||||
|
$mockRetriever->expects($this->once())
|
||||||
|
->method('getCloudObject')
|
||||||
|
->with($coid)
|
||||||
|
->willReturn($mockCloudObject);
|
||||||
|
|
||||||
|
ObjectRetrieverFacade::setObjectRetriever($mockRetriever);
|
||||||
|
|
||||||
|
$this->assertSame($mockCloudObject, ObjectRetrieverFacade::getCloudObject($coid));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReplacingRetrieverUsesNewInstance(): void {
|
||||||
|
$coid = new IRI('coid://cloudobjects.io');
|
||||||
|
$mockNode = $this->createMock(Node::class);
|
||||||
|
|
||||||
|
$firstRetriever = $this->createMock(ObjectRetriever::class);
|
||||||
|
$firstRetriever->expects($this->never())->method('getObjectNode');
|
||||||
|
|
||||||
|
$secondRetriever = $this->createMock(ObjectRetriever::class);
|
||||||
|
$secondRetriever->expects($this->once())
|
||||||
|
->method('getObjectNode')
|
||||||
|
->with($coid)
|
||||||
|
->willReturn($mockNode);
|
||||||
|
|
||||||
|
ObjectRetrieverFacade::setObjectRetriever($firstRetriever);
|
||||||
|
ObjectRetrieverFacade::setObjectRetriever($secondRetriever);
|
||||||
|
|
||||||
|
$this->assertSame($mockNode, ObjectRetrieverFacade::getObjectNode($coid));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user