Skip to content

Commit

Permalink
Add isTermsPublished and getTermsVersionSignatureFor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Feb 10, 2023
1 parent 813b238 commit ac5e9c0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/Infrastructure/Repository/TermsVersionSignatureRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use RichId\TermsModuleBundle\Domain\Entity\TermsSubjectInterface;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersionSignature;

/** @extends ServiceEntityRepository<TermsVersionSignature> */
Expand All @@ -16,4 +18,21 @@ public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TermsVersionSignature::class);
}

public function findOneByVersionAndSubject(TermsVersion $termsVersion, TermsSubjectInterface $subject): ?TermsVersionSignature
{
return $this->createQueryBuilder('tvs')
->where('tvs.version = :version')
->andWhere('tvs.subjectType = :subjectType')
->andWhere('tvs.subjectIdentifier = :subjectIdentifier')
->setParameters(
[
'version' => $termsVersion->getId(),
'subjectType' => $subject->getTermsSubjectType(),
'subjectIdentifier' => $subject->getTermsSubjectIdentifier(),
]
)
->getQuery()
->getOneOrNullResult();
}
}
34 changes: 33 additions & 1 deletion src/Infrastructure/TwigExtension/TermsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsSubjectInterface;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersionSignature;
use RichId\TermsModuleBundle\Domain\Port\TermsRepositoryInterface;
use RichId\TermsModuleBundle\Domain\UseCase\GenerateSigningRoute;
use RichId\TermsModuleBundle\Domain\UseCase\GenerateTermsRoute;
use RichId\TermsModuleBundle\Domain\UseCase\HasSignedTerms;
use RichId\TermsModuleBundle\Infrastructure\Repository\TermsVersionSignatureRepository;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -23,14 +27,24 @@ class TermsExtension extends AbstractExtension
/** @var GenerateTermsRoute */
protected $generateTermsRoute;

/** @var TermsVersionSignatureRepository */
protected $termsVersionSignatureRepository;

/** @var TermsRepositoryInterface */
protected $termsRepository;

public function __construct(
HasSignedTerms $hasSignedTerms,
GenerateSigningRoute $generateSigningRoute,
GenerateTermsRoute $generateTermsRoute
GenerateTermsRoute $generateTermsRoute,
TermsVersionSignatureRepository $termsVersionSignatureRepository,
TermsRepositoryInterface $termsRepository
) {
$this->hasSignedTerms = $hasSignedTerms;
$this->generateSigningRoute = $generateSigningRoute;
$this->generateTermsRoute = $generateTermsRoute;
$this->termsVersionSignatureRepository = $termsVersionSignatureRepository;
$this->termsRepository = $termsRepository;
}

public function getFunctions(): array
Expand All @@ -39,7 +53,9 @@ public function getFunctions(): array
new TwigFunction('generateSigningRoute', [$this, 'generateSigningRoute']),
new TwigFunction('generateTermsRoute', [$this, 'generateTermsRoute']),
new TwigFunction('generateTermsRouteFromSlug', [$this, 'generateTermsRouteFromSlug']),
new TwigFunction('getTermsVersionSignatureFor', [$this, 'getTermsVersionSignatureFor']),
new TwigFunction('hasSignedTerms', [$this, 'hasSignedTerms']),
new TwigFunction('isTermsPublished', [$this, 'isTermsPublished']),
];
}

Expand All @@ -58,8 +74,24 @@ public function generateTermsRouteFromSlug(string $termsSlug, TermsSubjectInterf
return ($this->generateTermsRoute)($termsSlug, $subject);
}

public function getTermsVersionSignatureFor(TermsVersion $termsVersion, TermsSubjectInterface $subject): ?TermsVersionSignature
{
return $this->termsVersionSignatureRepository->findOneByVersionAndSubject($termsVersion, $subject);
}

public function hasSignedTerms(Terms $terms, TermsSubjectInterface $subject): int
{
return ($this->hasSignedTerms)($terms->getSlug() ?? '', $subject);
}

public function isTermsPublished(string $termsSlug): bool
{
$terms = $this->termsRepository->findOneBySlug($termsSlug);

if ($terms === null) {
return false;
}

return $terms->isPublished();
}
}
1 change: 1 addition & 0 deletions src/UserInterface/Controller/TermsRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __invoke(string $termsSlug): Response
[
'terms' => $terms,
'lastTermsVersion' => $lastVersion,
'subject' => $subject,
]
);
} catch (NotFoundTermsException | NotPublishedTermsException | TermsHasNoPublishedVersionException $e) {
Expand Down

0 comments on commit ac5e9c0

Please sign in to comment.