Skip to content

Commit

Permalink
Introduce an optional PSR Eventdispatcher for Processor
Browse files Browse the repository at this point in the history
  • Loading branch information
mathielen committed Jun 17, 2024
1 parent 2153c0b commit 605a8de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"beberlei/assert": "@stable",
"jms/serializer": "^3.28.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/event-dispatcher": "^1.0.0",
"doctrine/annotations": "@stable"
},
"suggest": {
Expand Down
28 changes: 28 additions & 0 deletions src/CXml/Processor/Event/CXmlProcessEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CXml\Processor\Event;

use CXml\Context;
use CXml\Model\CXml;

class CXmlProcessEvent
{
private CXml $cxml;
private Context $context;

public function __construct(CXml $cxml, Context $context)
{
$this->cxml = $cxml;
$this->context = $context;
}

public function getCxml(): CXml
{
return $this->cxml;
}

public function getContext(): Context
{
return $this->context;
}
}
11 changes: 10 additions & 1 deletion src/CXml/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use CXml\Model\Response\Response;
use CXml\Model\Response\ResponsePayloadInterface;
use CXml\Model\Status;
use CXml\Processor\Event\CXmlProcessEvent;
use CXml\Processor\Exception\CXmlProcessException;
use Psr\EventDispatcher\EventDispatcherInterface;

class Processor
{
Expand Down Expand Up @@ -108,15 +110,18 @@ class Processor
private HeaderProcessor $headerProcessor;
private HandlerRegistryInterface $handlerRegistry;
private Builder $builder;
private ?EventDispatcherInterface $eventDispatcher;

public function __construct(
HeaderProcessor $requestProcessor,
HandlerRegistryInterface $handlerRepository,
Builder $builder
Builder $builder,
EventDispatcherInterface $eventDispatcher = null
) {
$this->headerProcessor = $requestProcessor;
$this->handlerRegistry = $handlerRepository;
$this->builder = $builder;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -127,6 +132,10 @@ public function process(CXml $cxml, Context $context = null): ?CXml
$context ??= Context::create();
$context->setCXml($cxml);

if ($this->eventDispatcher) {
$this->eventDispatcher->dispatch(new CXmlProcessEvent($cxml, $context));
}

$request = $cxml->getRequest();
if ($request) {
return $this->processRequest($request, $context);
Expand Down

0 comments on commit 605a8de

Please sign in to comment.