Skip to content

Commit

Permalink
Added methods to fetch multiple records via repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jul 18, 2024
1 parent 6ba7ab2 commit b14e6d2
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 1 deletion.
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
58 changes: 58 additions & 0 deletions tests/cases/unit/Models/PoliciesRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,62 @@ public function testPolicyEntity(): void
self::assertNull($foundPolicy);
}

public function testFetchEntities(): void
{
$this->generateDbSchema();

Check failure on line 83 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.1, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Doctrine\ORM\Tools\ToolsException but it's missing from the PHPDoc @throws tag.

Check failure on line 83 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.2, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Doctrine\ORM\Tools\ToolsException but it's missing from the PHPDoc @throws tag.

Check failure on line 83 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.3, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Doctrine\ORM\Tools\ToolsException but it's missing from the PHPDoc @throws tag.

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

Check failure on line 85 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.1, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Nette\DI\MissingServiceException but it's missing from the PHPDoc @throws tag.

Check failure on line 85 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.2, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Nette\DI\MissingServiceException but it's missing from the PHPDoc @throws tag.

Check failure on line 85 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.3, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Nette\DI\MissingServiceException but it's missing from the PHPDoc @throws tag.

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

Check failure on line 87 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.1, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Nette\DI\MissingServiceException but it's missing from the PHPDoc @throws tag.

Check failure on line 87 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.2, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Nette\DI\MissingServiceException but it's missing from the PHPDoc @throws tag.

Check failure on line 87 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.3, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception Nette\DI\MissingServiceException but it's missing from the PHPDoc @throws tag.

$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);

Check failure on line 120 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.1, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

Check failure on line 120 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.2, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

Check failure on line 120 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.3, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

self::assertCount(3, $policies);

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

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

Check failure on line 127 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.1, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

Check failure on line 127 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.2, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

Check failure on line 127 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.3, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

self::assertCount(3, $policies);

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

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

Check failure on line 134 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.1, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

Check failure on line 134 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.2, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

Check failure on line 134 in tests/cases/unit/Models/PoliciesRepositoryTest.php

View workflow job for this annotation

GitHub Actions / Code static analysis (8.3, ubuntu-latest)

Method FastyBird\SimpleAuth\Tests\Cases\Unit\Models\PoliciesRepositoryTest::testFetchEntities() throws checked exception FastyBird\SimpleAuth\Exceptions\InvalidState but it's missing from the PHPDoc @throws tag.

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

}

0 comments on commit b14e6d2

Please sign in to comment.