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

Signature generator #6

Merged
merged 8 commits into from
Oct 11, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: [pull_request]

jobs:
build-and-test:
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
Expand Down
12 changes: 12 additions & 0 deletions src/Domain/Entity/TermsUserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Entity;

interface TermsUserInterface
{
public function getTermsDisplayName(): ?string;

public function getTermsDisplayNameForSort(): ?string;
}
61 changes: 59 additions & 2 deletions src/Domain/Entity/TermsVersionSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class TermsVersionSignature
*/
protected $subjectIdentifier;

/**
* @var string
*
* @ORM\Column(type="string", nullable=false, length=255, name="subject_name")
*/
protected $subjectName;

/**
* @var TermsVersion
*
Expand All @@ -56,12 +63,26 @@ class TermsVersionSignature
protected $version;

/**
* @var string
* @var string|null
*
* @ORM\Column(type="string", nullable=true, length=255, name="signed_by")
*/
protected $signedBy;

/**
* @var string|null
*
* @ORM\Column(type="string", nullable=true, length=255, name="signed_by_name")
*/
protected $signedByName;

/**
* @var string|null
*
* @ORM\Column(type="string", nullable=true, length=255, name="signed_by_name_for_sort")
*/
protected $signedByNameForSort;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -103,6 +124,18 @@ public function getSubjectIdentifier(): ?string
return $this->subjectIdentifier;
}

public function getSubjectName(): string
{
return $this->subjectName;
}

public function setSubjectName(string $subjectName): self
{
$this->subjectName = $subjectName;

return $this;
}

public function setVersion(TermsVersion $version): self
{
$this->version = $version;
Expand All @@ -115,7 +148,7 @@ public function getVersion(): ?TermsVersion
return $this->version;
}

public function setSignedBy(string $signedBy): self
public function setSignedBy(?string $signedBy): self
{
$this->signedBy = $signedBy;

Expand All @@ -126,4 +159,28 @@ public function getSignedBy(): ?string
{
return $this->signedBy;
}

public function getSignedByName(): ?string
{
return $this->signedByName;
}

public function setSignedByName(?string $signedByName): self
{
$this->signedByName = $signedByName;

return $this;
}

public function getSignedByNameForSort(): ?string
{
return $this->signedByNameForSort;
}

public function setSignedByNameForSort(?string $signedByNameForSort): self
{
$this->signedByNameForSort = $signedByNameForSort;

return $this;
}
}
23 changes: 22 additions & 1 deletion src/Domain/Factory/TermsVersionSignatureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,56 @@
namespace RichId\TermsModuleBundle\Domain\Factory;

use RichId\TermsModuleBundle\Domain\Entity\TermsSubjectInterface;
use RichId\TermsModuleBundle\Domain\Entity\TermsUserInterface;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersionSignature;
use RichId\TermsModuleBundle\Domain\Guard\TermsGuardManager;
use RichId\TermsModuleBundle\Domain\Port\SecurityInterface;
use RichId\TermsModuleBundle\Domain\Port\TranslatorInterface;

class TermsVersionSignatureFactory
{
/** @var SecurityInterface */
protected $security;

public function __construct(SecurityInterface $security)
/** @var TranslatorInterface */
protected $translator;

/** @var TermsGuardManager */
protected $termsGuardManager;

public function __construct(SecurityInterface $security, TranslatorInterface $translator, TermsGuardManager $termsGuardManager)
{
$this->security = $security;
$this->translator = $translator;
$this->termsGuardManager = $termsGuardManager;
}

public function __invoke(TermsVersion $version, TermsSubjectInterface $subject): TermsVersionSignature
{
$guard = $this->termsGuardManager->getGuardFor($version->getTerms()->getSlug() ?? '', $subject);
$subjectName = $guard !== null ? $guard->getSubjectName($subject) : null;
$subjectName = $subjectName ?? $this->translator->trans('terms_module.pdf_signature.subject_not_found', [], 'terms_module');

$user = $this->security->getUser();

$entity = new TermsVersionSignature();

$entity->setVersion($version);
$entity->setSubjectType($subject->getTermsSubjectType());
$entity->setSubjectIdentifier($subject->getTermsSubjectIdentifier());
$entity->setSubjectName($subjectName);
$entity->setDate(new \DateTime());

if ($user !== null) {
$entity->setSignedBy($user->getUsername());
}

if ($user instanceof TermsUserInterface) {
$entity->setSignedByName($user->getTermsDisplayName());
$entity->setSignedByNameForSort($user->getTermsDisplayNameForSort());
}

return $entity;
}
}
2 changes: 2 additions & 0 deletions src/Domain/Guard/TermsGuardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface TermsGuardInterface
public function supports(string $slug, TermsSubjectInterface $subject): bool;

public function check(string $slug, TermsSubjectInterface $subject): bool;

public function getSubjectName(TermsSubjectInterface $subject): ?string;
}
30 changes: 30 additions & 0 deletions src/Domain/Guard/TermsGuardManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Guard;

use RichId\TermsModuleBundle\Domain\Entity\TermsSubjectInterface;

class TermsGuardManager
{
/** @var array<TermsGuardInterface> */
protected $guards;

/** @param array<TermsGuardInterface> $guards */
public function setGuards(array $guards): void
{
$this->guards = $guards;
}

public function getGuardFor(string $termsSlug, TermsSubjectInterface $subject): ?TermsGuardInterface
{
foreach ($this->guards as $guard) {
if ($guard->supports($termsSlug, $subject)) {
return $guard;
}
}

return null;
}
}
106 changes: 106 additions & 0 deletions src/Domain/Model/SignatureListForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Model;

use RichId\TermsModuleBundle\Domain\Entity\Terms;

class SignatureListForm
{
public const SORT_SIGNATORY = 'signatory';
public const SORT_DATE = 'date';

public const SORT_ASC = 'asc';
public const SORT_DESC = 'desc';

/** @var string|null */
private $search;

/** @var Terms|null */
private $terms;

/** @var int */
private $page = 1;

/** @var int */
private $numberItemsPerPage = 10;

/** @var string */
private $sort = self::SORT_SIGNATORY;

/** @var string */
private $sortDirection = self::SORT_DESC;

public function getSearch(): ?string
{
return $this->search;
}

public function setSearch(?string $search): self
{
$this->search = $search;

return $this;
}

public function getTerms(): ?Terms
{
return $this->terms;
}

public function setTerms(?Terms $terms): self
{
$this->terms = $terms;

return $this;
}

public function getPage(): int
{
return $this->page;
}

public function setPage(int $page): self
{
$this->page = $page;

return $this;
}

public function getNumberItemsPerPage(): int
{
return $this->numberItemsPerPage;
}

public function setNumberItemsPerPage(int $numberItemsPerPage): self
{
$this->numberItemsPerPage = $numberItemsPerPage;

return $this;
}

public function getSort(): string
{
return $this->sort;
}

public function setSort(string $sort): self
{
$this->sort = $sort;

return $this;
}

public function getSortDirection(): string
{
return $this->sortDirection;
}

public function setSortDirection(string $sortDirection): self
{
$this->sortDirection = $sortDirection;

return $this;
}
}
13 changes: 13 additions & 0 deletions src/Domain/Pdf/TermsVersionSignaturePdfGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Pdf;

use RichId\TermsModuleBundle\Domain\Entity\TermsUserInterface;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersionSignature;

interface TermsVersionSignaturePdfGeneratorInterface
{
public function __invoke(TermsVersionSignature $termsVersionSignature, ?TermsUserInterface $editor = null): string;
}
49 changes: 49 additions & 0 deletions src/Domain/Pdf/TermsVersionSignaturePdfGeneratorManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Pdf;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class TermsVersionSignaturePdfGeneratorManager
{
/** @var array<TermsVersionSignaturePdfGeneratorInterface> */
protected $generators;

/** @var ParameterBagInterface */
protected $parameterBag;

public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
}

/** @param array<TermsVersionSignaturePdfGeneratorInterface> $generators */
public function setGenerators(array $generators): void
{
$this->generators = $generators;
}

public function hasConfiguredGenerator(): bool
{
return $this->getConfiguredGenerator() !== null;
}

public function getConfiguredGenerator(): ?TermsVersionSignaturePdfGeneratorInterface
{
$selectedGenerator = $this->parameterBag->get('rich_id_terms_module.terms_version_signature_pdf_generator') ?? null;

if (empty($selectedGenerator)) {
return null;
}

foreach ($this->generators as $generator) {
if ($selectedGenerator === \get_class($generator)) {
return $generator;
}
}

return null;
}
}
11 changes: 11 additions & 0 deletions src/Domain/Port/TranslatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Port;

interface TranslatorInterface
{
/** @param array<string, string> $parameters */
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string;
}
Loading
Loading