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

introduced CredentialValidatorInterface #35

Merged
merged 1 commit into from
Jun 13, 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
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ $documentRegistory->register($cXml);
### Process incoming cXML documents

```php
$headerProcessor = new \CXml\Processor\HeaderProcessor($credentialRegistry);
$headerProcessor = new \CXml\Processor\HeaderProcessor($credentialRegistry, $credentialRegistry, $credentialRegistry);

$cXmlProcessor = new \CXml\Processor\Processor(
$headerProcessor,
Expand All @@ -119,7 +119,7 @@ $handlerRegistry = new \CXml\Handler\HandlerRegistry();

$builder = \CXml\Builder::create();

$headerProcessor = new \CXml\Processor\HeaderProcessor($credentialRegistry, $credentialRegistry);
$headerProcessor = new \CXml\Processor\HeaderProcessor($credentialRegistry, $credentialRegistry, $credentialRegistry);
$cXmlProcessor = new \CXml\Processor\Processor(
$headerProcessor,
$handlerRegistry,
Expand Down
14 changes: 14 additions & 0 deletions src/CXml/Credential/CredentialValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace CXml\Credential;

use CXml\Exception\CXmlCredentialInvalidException;
use CXml\Model\Credential;

interface CredentialValidatorInterface
{
/**
* @throws CXmlCredentialInvalidException
*/
public function validate(Credential $credential): void;
}
14 changes: 13 additions & 1 deletion src/CXml/Credential/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use CXml\Model\Credential;
use CXml\Model\Header;

class Registry implements CredentialRepositoryInterface, AuthenticatorInterface
class Registry implements CredentialRepositoryInterface, AuthenticatorInterface, CredentialValidatorInterface
{
/**
* @var Credential[]
Expand Down Expand Up @@ -52,4 +52,16 @@ public function authenticate(Header $header, Context $context): void
throw new CXmlAuthenticationInvalidException($senderCredential);
}
}

/**
* @throws CXmlCredentialInvalidException
*/
public function validate(Credential $credential): void
{
// provoke an exception if credential was not found
$this->getCredentialByDomainAndId(
$credential->getDomain(),
$credential->getIdentity()
);
}
}
29 changes: 11 additions & 18 deletions src/CXml/Processor/HeaderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
use CXml\Authentication\AuthenticatorInterface;
use CXml\Context;
use CXml\Credential\CredentialRepositoryInterface;
use CXml\Credential\CredentialValidatorInterface;
use CXml\Exception\CXmlAuthenticationInvalidException;
use CXml\Exception\CXmlCredentialInvalidException;
use CXml\Model\Credential;
use CXml\Model\Header;

class HeaderProcessor
{
private CredentialRepositoryInterface $credentialRepository;
private CredentialValidatorInterface $credentialValidator;
private AuthenticatorInterface $authenticator;

public function __construct(CredentialRepositoryInterface $credentialRepository, AuthenticatorInterface $authenticator)
{
public function __construct(
CredentialRepositoryInterface $credentialRepository,
CredentialValidatorInterface $credentialValidator,
AuthenticatorInterface $authenticator
) {
$this->credentialRepository = $credentialRepository;
$this->credentialValidator = $credentialValidator;
$this->authenticator = $authenticator;
}

Expand All @@ -37,22 +42,10 @@ public function process(Header $header, Context $context): void
*/
private function validatePartys(Header $header): void
{
$this->checkCredentialIsValid($header->getFrom()->getCredential());

$this->checkCredentialIsValid($header->getTo()->getCredential());
$this->credentialValidator->validate($header->getFrom()->getCredential());

$this->checkCredentialIsValid($header->getSender()->getCredential());
}
$this->credentialValidator->validate($header->getTo()->getCredential());

/**
* @throws CXmlCredentialInvalidException
*/
private function checkCredentialIsValid(Credential $testCredential): void
{
// provoke an exception if credential was not found
$this->credentialRepository->getCredentialByDomainAndId(
$testCredential->getDomain(),
$testCredential->getIdentity()
);
$this->credentialValidator->validate($header->getSender()->getCredential());
}
}