Skip to content

Commit

Permalink
ResourceInfoRepository methods update (#4)
Browse files Browse the repository at this point in the history
* added replace and exists methods
* insert() now throws exception when resource exists
  • Loading branch information
Mararok authored Apr 4, 2018
1 parent fad3179 commit f721e0c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use Predis\Client;
use Predis\Collection\Iterator\Keyspace;
use SAREhub\MultiTenantUtil\Resource\NotFoundResourceInfoException;
use Predis\Transaction\MultiExec;
use SAREhub\MultiTenantUtil\Resource\ResourceInfo;
use SAREhub\MultiTenantUtil\Resource\ResourceInfoExistsException;
use SAREhub\MultiTenantUtil\Resource\ResourceInfoNotFoundException;
use SAREhub\MultiTenantUtil\Resource\ResourceInfoRepository;

class RedisResourceInfoRepository implements ResourceInfoRepository
Expand Down Expand Up @@ -41,19 +43,40 @@ public function insert(ResourceInfo $resource)
{
$key = $this->getPrefixedKey($resource->getId());
$data = $this->serializeResource($resource);
$this->getRedisClient()->hmset($key, $data);

$options = ["cas" => true, "watch" => $key, "retry" => 0];

$this->redisClient->transaction($options, function (MultiExec $tx) use ($key, $resource, $data) {
if ($tx->exists($key)) {
throw new ResourceInfoExistsException($this->getResourceTypeName(), $resource->getId());
}
$tx->multi();
$tx->hmset($key, $data);
});
}

public function replace(ResourceInfo $resource)
{
$key = $this->getPrefixedKey($resource->getId());
$data = $this->serializeResource($resource);
$this->redisClient->hmset($key, $data);
}

public function exists(string $id): bool
{
return (bool)$this->redisClient->exists($this->getPrefixedKey($id));
}

/**
* @param string $id
* @return ResourceInfo
* @throws NotFoundResourceInfoException
* @throws ResourceInfoNotFoundException
*/
public function find(string $id): ResourceInfo
{
$data = $this->getRedisClient()->hgetall($this->getPrefixedKey($id));
if (empty($data)) {
throw new NotFoundResourceInfoException($this->getResourceTypeName(), $id);
throw new ResourceInfoNotFoundException($this->getResourceTypeName(), $id);
}

return $this->deserializeResource($id, $data);
Expand Down
15 changes: 15 additions & 0 deletions src/SAREhub/MultiTenantUtil/Resource/ResourceInfoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace SAREhub\MultiTenantUtil\Resource;


use Throwable;

class ResourceInfoException extends \Exception
{
public function __construct(string $message, Throwable $previous = null)
{
parent::__construct($message, 0, $previous);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace SAREhub\MultiTenantUtil\Resource;


class ResourceInfoExistsException extends ResourceInfoException
{
private $resourceType;
private $resourceId;

public function __construct(string $resourceType, string $resourceId)
{
parent::__construct("Resource of type '$resourceType' and id '$resourceId' exists");
$this->resourceType = $resourceType;
$this->resourceId = $resourceId;
}

public function getResourceType(): string
{
return $this->resourceType;
}

public function getResourceId(): string
{
return $this->resourceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
namespace SAREhub\MultiTenantUtil\Resource;


class NotFoundResourceInfoException extends \Exception
class ResourceInfoNotFoundException extends ResourceInfoException
{
private $resourceType;
private $resourceId;

/**
*
* @param $resourceType
* @param $resourceId
*/
public function __construct(string $resourceType, string $resourceId)
{
parent::__construct("Resource of type '$resourceType' and id '$resourceId' not found");
Expand Down
20 changes: 19 additions & 1 deletion src/SAREhub/MultiTenantUtil/Resource/ResourceInfoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@

interface ResourceInfoRepository
{
/**
* @param ResourceInfo $resource
* @throws ResourceInfoExistsException When resource with same id exists
*/
public function insert(ResourceInfo $resource);

/**
* @param ResourceInfo $resource
*/
public function replace(ResourceInfo $resource);

/**
* @param string $id
* @return bool
*/
public function exists(string $id): bool;

/**
* @param string $id
* @return ResourceInfo
* @throws NotFoundResourceInfoException
* @throws ResourceInfoNotFoundException When resource with id not exists
*/
public function find(string $id): ResourceInfo;

Expand All @@ -20,5 +35,8 @@ public function find(string $id): ResourceInfo;
*/
public function findAll(): array;

/**
* @param string $id
*/
public function delete(string $id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use SAREhub\MultiTenantUtil\RedisTestCase;
use SAREhub\MultiTenantUtil\Resource\AccountSharedResourceInfo;
use SAREhub\MultiTenantUtil\Resource\NotFoundResourceInfoException;
use SAREhub\MultiTenantUtil\Resource\ResourceInfo;
use SAREhub\MultiTenantUtil\Resource\ResourceInfoExistsException;
use SAREhub\MultiTenantUtil\Resource\ResourceInfoNotFoundException;

class RedisResourceInfoRepositoryITest extends RedisTestCase
{
Expand All @@ -21,6 +22,44 @@ protected function setUp()
$this->repository = new RedisResourceInfoRepository($this->redisClient, "test_prefix", "test_resource_type");
}

public function testInsertWhenExists()
{
$res = $this->createResource();
$this->repository->insert($res);

$this->expectException(ResourceInfoExistsException::class);
$this->repository->insert($res);
}

public function testReplaceWhenNotExists()
{
$res = $this->createResource();
$this->repository->replace($res);
$this->assertEquals($res, $this->repository->find($res->getId()));
}

public function testReplaceWhenExistsThenReplaced()
{
$old = $this->createResource("test", ["field" => "initial"]);
$this->repository->replace($old);

$expected = $this->createResource("test", ["field" => "replaced"]);
$this->repository->replace($expected);

$this->assertEquals($expected, $this->repository->find($expected->getId()));
}

public function testExistsWhenExists()
{
$res = $this->insertResourceToRepository();
$this->assertTrue($this->repository->exists($res->getId()));
}

public function testExistsWhenNotExists()
{
$this->assertFalse($this->repository->exists("not exists"));
}

public function testFindWhenExists()
{
$res = $this->insertResourceToRepository();
Expand All @@ -30,7 +69,7 @@ public function testFindWhenExists()
public function testFindWhenNotExists()
{
$resId = "test_id";
$this->expectException(NotFoundResourceInfoException::class);
$this->expectException(ResourceInfoNotFoundException::class);
$this->expectExceptionMessage("Resource of type 'test_resource_type' and id 'test_id' not found");
$this->repository->find($resId);
}
Expand Down Expand Up @@ -61,8 +100,8 @@ private function insertResourceToRepository(): ResourceInfo
return $res;
}

private function createResource(): ResourceInfo
private function createResource(string $idSuffix = "", array $fields = ["field" => "value"]): ResourceInfo
{
return new ResourceInfo("test_id", ["field" => "value"]);
return new ResourceInfo("test_id" . $idSuffix, $fields);
}
}

0 comments on commit f721e0c

Please sign in to comment.