Skip to content

Commit

Permalink
Issue #60: Add SentNotificationResponseEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
ademarco committed Oct 20, 2017
1 parent 9657787 commit 7a36132
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 20 deletions.
41 changes: 41 additions & 0 deletions src/Events/NotificationHandler/SentNotificationResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace EC\Poetry\Events\NotificationHandler;

use Symfony\Component\EventDispatcher\Event;

/**
* Class SentNotificationResponseEvent
*
* @package EC\Poetry\Events\NotificationHandler
*/
class SentNotificationResponseEvent extends Event
{
const NAME = 'poetry.notification_handler.sent_notification_response';

/**
* @var string
*/
private $message;

/**
* SentNotificationResponseEvent constructor.
*
* @param string $message
*/
public function __construct($message)
{
$this->message = $message;
}

/**
* Get Message property.
*
* @return string
* Property value.
*/
public function getMessage()
{
return $this->message;
}
}
14 changes: 10 additions & 4 deletions src/NotificationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EC\Poetry;

use EC\Poetry\Events\NotificationHandler\ReceivedNotificationEvent;
use EC\Poetry\Events\NotificationHandler\SentNotificationResponseEvent;
use EC\Poetry\Events\ParseNotificationEvent;
use EC\Poetry\Exceptions\Notifications\CannotAuthenticateException;
use EC\Poetry\Exceptions\ParsingException;
Expand Down Expand Up @@ -81,11 +82,16 @@ public function handle($username, $password, $xml)
$this->dispatchExceptionEvent(new ValidationException($violations));
}
$this->eventDispatcher->dispatch($event->getName(), $event);
$response = $this->getSuccessStatus($event->getMessage());
} catch (\Exception $exception) {
return $this->getErrorStatus($exception->getMessage(), $event->getMessage());
$response = $this->getErrorStatus($exception->getMessage(), $event->getMessage());
}
finally {
$event = new SentNotificationResponseEvent($response);
$this->eventDispatcher->dispatch(SentNotificationResponseEvent::NAME, $event);

return $this->getSuccessStatus($event->getMessage());
return $response;
}
}

/**
Expand Down Expand Up @@ -136,8 +142,8 @@ protected function getSuccessStatus($notification)
}

/**
* @param string $message
* @param \EC\Poetry\Messages\MessageInterface $notification
* @param string $message
* @param \EC\Poetry\Messages\MessageInterface $notification
*
* @return string
*/
Expand Down
43 changes: 33 additions & 10 deletions src/Services/LoggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EC\Poetry\Events\ExceptionEvent;
use EC\Poetry\Events\NotificationEventInterface;
use EC\Poetry\Events\NotificationHandler\ReceivedNotificationEvent;
use EC\Poetry\Events\NotificationHandler\SentNotificationResponseEvent;
use EC\Poetry\Events\Notifications\StatusUpdatedEvent;
use EC\Poetry\Events\Notifications\TranslationReceivedEvent;
use EC\Poetry\Events\ParseNotificationEvent;
Expand Down Expand Up @@ -57,14 +58,15 @@ public function __construct(LoggerInterface $logger, Renderer $renderer, Setting
public static function getSubscribedEvents()
{
return [
ParseResponseEvent::NAME => 'onParseResponseEvent',
ParseNotificationEvent::NAME => 'onParseNotificationEvent',
StatusUpdatedEvent::NAME => 'onNotificationEvent',
TranslationReceivedEvent::NAME => 'onNotificationEvent',
ClientRequestEvent::NAME => 'onClientRequestEvent',
ClientResponseEvent::NAME => 'onClientResponseEvent',
ReceivedNotificationEvent::NAME => 'onReceivedNotificationEvent',
ExceptionEvent::NAME => 'onExceptionEvent',
ParseResponseEvent::NAME => 'onParseResponseEvent',
ParseNotificationEvent::NAME => 'onParseNotificationEvent',
StatusUpdatedEvent::NAME => 'onNotificationEvent',
TranslationReceivedEvent::NAME => 'onNotificationEvent',
ClientRequestEvent::NAME => 'onClientRequestEvent',
ClientResponseEvent::NAME => 'onClientResponseEvent',
ReceivedNotificationEvent::NAME => 'onReceivedNotificationEvent',
SentNotificationResponseEvent::NAME => 'onSentNotificationResponseEvent',
ExceptionEvent::NAME => 'onExceptionEvent',
];
}

Expand All @@ -73,15 +75,15 @@ public static function getSubscribedEvents()
*/
public function onParseResponseEvent(ParseResponseEvent $event)
{
$this->logInfo(ParseResponseEvent::NAME, ['message' => $event->getXml()]);
$this->logDebug(ParseResponseEvent::NAME, ['message' => $event->getXml()]);
}

/**
* @param \EC\Poetry\Events\ParseNotificationEvent $event
*/
public function onParseNotificationEvent(ParseNotificationEvent $event)
{
$this->logInfo(ParseNotificationEvent::NAME, ['message' => $event->getXml()]);
$this->logDebug(ParseNotificationEvent::NAME, ['message' => $event->getXml()]);
}

/**
Expand Down Expand Up @@ -117,6 +119,14 @@ public function onReceivedNotificationEvent(ReceivedNotificationEvent $event)
]);
}

/**
* @param \EC\Poetry\Events\NotificationHandler\SentNotificationResponseEvent $event
*/
public function onSentNotificationResponseEvent(SentNotificationResponseEvent $event)
{
$this->logInfo(SentNotificationResponseEvent::NAME, ['message' => $event->getMessage()]);
}

/**
* @param \EC\Poetry\Events\Client\ClientResponseEvent $event
*/
Expand Down Expand Up @@ -168,6 +178,19 @@ private function logInfo($message, array $context = [])
}
}

/**
* Log debug messages.
*
* @param string $message
* @param array $context
*/
private function logDebug($message, array $context = [])
{
if ($this->canLogLevel(LogLevel::DEBUG)) {
$this->logger->debug($message, $context);
}
}

/**
* Log error messages.
*
Expand Down
15 changes: 9 additions & 6 deletions tests/src/Services/LoggerSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ public function testClientLogging()
'service.password' => 'password',
'soap_client' => $mock,
'logger' => $logger,
'log_level' => LogLevel::INFO,
'log_level' => LogLevel::DEBUG,
]);

$poetry->getClient()->send($request);
$logs = $logger->getLogs()[LogLevel::INFO];

expect($logs)->to->have->keys([
'poetry.client.request',
'poetry.client.response',
'poetry.response.parse',
]);
expect($logs)
->to->have->keys(['poetry.client.request', 'poetry.client.response'])
->and->not->to->have->keys(['poetry.response.parse']);
expect($logs['poetry.client.response']['message'])->to->have->same->xml('messages/responses/response-status.xml');
expect($logs['poetry.client.request']['message'])->to->contain('<request communication="synchrone" id="DGT/2017/00001/3/0/TRA" type="newPost">');
expect($logs['poetry.client.request']['username'])->to->equal('username');
expect($logs['poetry.client.request']['password'])->to->equal('p******d');

$logs = $logger->getLogs()[LogLevel::DEBUG];
expect($logs)
->to->have->keys(['poetry.response.parse'])
->and->not->to->have->keys(['poetry.client.request', 'poetry.client.response']);
expect($logs['poetry.response.parse']['message'])->to->have->same->xml('messages/responses/response-status.xml');
}

Expand Down

0 comments on commit 7a36132

Please sign in to comment.