Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods to fetch multiple records via repositories #7

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Models/Policies/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
use Doctrine\ORM;
use Doctrine\Persistence;
use FastyBird\SimpleAuth\Entities;
use FastyBird\SimpleAuth\Exceptions;
use FastyBird\SimpleAuth\Queries;
use IPub\DoctrineOrmQuery;
use IPub\DoctrineOrmQuery\Exceptions as DoctrineOrmQueryExceptions;
use Nette;
use Throwable;
use function is_array;

/**
* Security token repository
Expand Down Expand Up @@ -60,6 +64,56 @@ public function findOneBy(
return $queryObject->fetchOne($this->getRepository($type));
}

/**
* @template T of Entities\Policies\Policy
*
* @param Queries\FindPolicies<T> $queryObject
* @param class-string<T> $type
*
* @return array<T>
*
* @throws Exceptions\InvalidState
*/
public function findAllBy(
Queries\FindPolicies $queryObject,
string $type = Entities\Policies\Policy::class,
): array
{
try {
/** @var array<T> $result */
$result = $this->getResultSet($queryObject, $type)->toArray();

return $result;
} catch (Throwable $ex) {
throw new Exceptions\InvalidState('Fetch all data by query failed', $ex->getCode(), $ex);
}
}

/**
* @template T of Entities\Policies\Policy
*
* @param Queries\FindPolicies<T> $queryObject
* @param class-string<T> $type
*
* @return DoctrineOrmQuery\ResultSet<T>
*
* @throws DoctrineOrmQueryExceptions\QueryException
* @throws Exceptions\InvalidState
*/
public function getResultSet(
Queries\FindPolicies $queryObject,
string $type = Entities\Policies\Policy::class,
): DoctrineOrmQuery\ResultSet
{
$result = $queryObject->fetch($this->getRepository($type));

if (is_array($result)) {
throw new Exceptions\InvalidState('Result set could not be created');
}

return $result;
}

/**
* @template T of Entities\Policies\Policy
*
Expand Down
54 changes: 54 additions & 0 deletions src/Models/Tokens/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
use Doctrine\ORM;
use Doctrine\Persistence;
use FastyBird\SimpleAuth\Entities;
use FastyBird\SimpleAuth\Exceptions;
use FastyBird\SimpleAuth\Queries;
use FastyBird\SimpleAuth\Types;
use IPub\DoctrineOrmQuery;
use IPub\DoctrineOrmQuery\Exceptions as DoctrineOrmQueryExceptions;
use Nette;
use Ramsey\Uuid;
use Throwable;
use function assert;
use function is_array;

/**
* Security token repository
Expand Down Expand Up @@ -113,6 +117,56 @@ public function findOneBy(
return $queryObject->fetchOne($this->getRepository($type));
}

/**
* @template T of Entities\Tokens\Token
*
* @param Queries\FindTokens<T> $queryObject
* @param class-string<T> $type
*
* @return array<T>
*
* @throws Exceptions\InvalidState
*/
public function findAllBy(
Queries\FindTokens $queryObject,
string $type = Entities\Tokens\Token::class,
): array
{
try {
/** @var array<T> $result */
$result = $this->getResultSet($queryObject, $type)->toArray();

return $result;
} catch (Throwable $ex) {
throw new Exceptions\InvalidState('Fetch all data by query failed', $ex->getCode(), $ex);
}
}

/**
* @template T of Entities\Tokens\Token
*
* @param Queries\FindTokens<T> $queryObject
* @param class-string<T> $type
*
* @return DoctrineOrmQuery\ResultSet<T>
*
* @throws DoctrineOrmQueryExceptions\QueryException
* @throws Exceptions\InvalidState
*/
public function getResultSet(
Queries\FindTokens $queryObject,
string $type = Entities\Tokens\Token::class,
): DoctrineOrmQuery\ResultSet
{
$result = $queryObject->fetch($this->getRepository($type));

if (is_array($result)) {
throw new Exceptions\InvalidState('Result set could not be created');
}

return $result;
}

/**
* @template T of Entities\Tokens\Token
*
Expand Down
3 changes: 2 additions & 1 deletion src/Queries/FindPolicies.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Closure;
use Doctrine\ORM;
use FastyBird\SimpleAuth\Entities;
use FastyBird\SimpleAuth\Types;
use IPub\DoctrineOrmQuery;
use Ramsey\Uuid;

Expand Down Expand Up @@ -47,7 +48,7 @@ public function byId(Uuid\UuidInterface $id): void
};
}

public function byType(string $type): void
public function byType(Types\PolicyType $type): void
{
$this->filter[] = static function (ORM\QueryBuilder $qb) use ($type): void {
$qb->andWhere('p.type = :type')->setParameter('type', $type);
Expand Down
64 changes: 64 additions & 0 deletions tests/cases/unit/Models/PoliciesRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FastyBird\SimpleAuth\Tests\Cases\Unit\Models;

use Doctrine\ORM;
use FastyBird\SimpleAuth\Exceptions;
use FastyBird\SimpleAuth\Models;
use FastyBird\SimpleAuth\Queries;
use FastyBird\SimpleAuth\Tests\Cases\Unit\BaseTestCase;
Expand Down Expand Up @@ -78,4 +79,67 @@ public function testPolicyEntity(): void
self::assertNull($foundPolicy);
}

/**
* @throws DI\MissingServiceException
* @throws Exceptions\InvalidState
* @throws ORM\Tools\ToolsException
*/
public function testFetchEntities(): void
{
$this->generateDbSchema();

$manager = $this->container->getByType(Models\Policies\Manager::class);

$repository = $this->container->getByType(Models\Policies\Repository::class);

$parentId = Uuid\Uuid::fromString('ff11f4fd-c06b-40a2-9a79-6dd3e3a10373');

$manager->create(Utils\ArrayHash::from([
'entity' => Fixtures\Entities\TestPolicyEntity::class,
'parent' => $parentId,
'type' => Types\PolicyType::POLICY,
'v0' => '2784d750-f085-4580-8525-4d622face83d',
'v1' => 'data1',
'v2' => 'read',
]));

$manager->create(Utils\ArrayHash::from([
'entity' => Fixtures\Entities\TestPolicyEntity::class,
'parent' => $parentId,
'type' => Types\PolicyType::POLICY,
'v0' => '2784d750-f085-4580-8525-4d622face83d',
'v1' => 'data1',
'v2' => 'write',
]));

$manager->create(Utils\ArrayHash::from([
'entity' => Fixtures\Entities\TestPolicyEntity::class,
'parent' => $parentId,
'type' => Types\PolicyType::POLICY,
'v0' => '2784d750-f085-4580-8525-4d622face83d',
'v1' => 'data1',
'v2' => 'remove',
]));

$findQuery = new Queries\FindPolicies();

$policies = $repository->findAllBy($findQuery);

self::assertCount(3, $policies);

$findQuery = new Queries\FindPolicies();
$findQuery->byType(Types\PolicyType::POLICY);

$policies = $repository->findAllBy($findQuery);

self::assertCount(3, $policies);

$findQuery = new Queries\FindPolicies();
$findQuery->byType(Types\PolicyType::ROLE);

$policies = $repository->findAllBy($findQuery);

self::assertCount(0, $policies);
}

}
Loading