Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mathielen committed Jun 13, 2024
2 parents 9d929b2 + a95da3f commit 92aa5e6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
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());
}
}

0 comments on commit 92aa5e6

Please sign in to comment.