Skip to content

Commit

Permalink
Merge pull request #4 from vimeo/event-cleanup
Browse files Browse the repository at this point in the history
Event cleanup
  • Loading branch information
JCManzo authored May 6, 2019
2 parents 7c02f59 + c23af9a commit f01c592
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 29 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ These events are dispatched via the HTTP client's `EventDispatcherInterface`.
For example in the gateway's `sendData()` methods we can do:

```PHP
$request_name; // The name of the API endpoint being called
$event_dispatcher = $this->httpClient->getEventDispatcher();
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_BEFORE_SEND, new RequestEvent($request, $request_name));
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_BEFORE_SEND, new RequestEvent($request));
```

Logging Errors and Responses events can be emitted like so
```PHP
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_ERROR new ErrorEvent($exception', $request_name));
$event_dispatcher->dispatch(Constants::OMNIPAY_RESPONSE_SUCCESS, new ResponseEvent($response, $request_name));
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_ERROR new ErrorEvent($exception, $request));
$event_dispatcher->dispatch(Constants::OMNIPAY_RESPONSE_SUCCESS, new ResponseEvent($response));
```

`OmnipayGatewayRequestSubscriber.php` takes in a logger of type `LoggerInterface` which will listen to and log these events.
Expand Down
1 change: 0 additions & 1 deletion src/Event/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Class events.
*
* @package payment-gateway-logger
* @author manzoj
* @version 1
*/

Expand Down
8 changes: 4 additions & 4 deletions src/Event/ErrorEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
* Error event to be used by the payment gateway logger.
*
* @package payment-gateway-logger
* @author manzoj
* @version 1
*/

namespace PaymentGatewayLogger\Event;

use Exception;
use Guzzle\Common\Event;
use Omnipay\Common\Message\RequestInterface;

class ErrorEvent extends Event
{
/**
* @param Exception $error
* @param string $request_name
* @param RequestInterface $request
*/
public function __construct($error, $request_name)
public function __construct($error, $request)
{
parent::__construct(array('error' => $error, 'request_name' => $request_name));
parent::__construct(array('error' => $error, 'request' => $request));
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Event/RequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Request event to be used by the payment gateway logger.
*
* @package payment-gateway-logger
* @author manzoj
* @version 1
*/

Expand All @@ -16,11 +15,10 @@ class RequestEvent extends Event
{
/**
* @param RequestInterface $request
* @param string $request_name
*/
public function __construct($request, $request_name)
public function __construct($request)
{
parent::__construct(array('request' => $request, 'request_name' => $request_name));
parent::__construct(array('request' => $request));
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Event/ResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Response event to be used by the payment gateway logger.
*
* @package payment-gateway-logger
* @author manzoj
* @version 1
*/

Expand All @@ -16,11 +15,10 @@ class ResponseEvent extends Event
{
/**
* @param ResponseInterface $response
* @param string $request_name
*/
public function __construct($response, $request_name)
public function __construct($response)
{
parent::__construct(array('response' => $response, 'request_name' => $request_name));
parent::__construct(array('response' => $response));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Event/Subscriber/OmnipayGatewayRequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* payment-gateway-logger
*
* @package payment-gateway-logger
* @author manzoj
* @version 1
*/

Expand Down Expand Up @@ -102,7 +101,8 @@ public function onOmnipayResponseSuccess(Event $event)
*
* The event will be converted to an array before being logged. It will contain the following properties:
* array(
* 'error' => Exception
* 'error' => Exception,
* 'request' => \Omnipay\Common\Message\AbstractRequest
* )
* @param Event $event
* @return void
Expand Down
14 changes: 4 additions & 10 deletions tests/OmnipayGatewayRequestSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* payment-gateway-logger
*
* @package payment-gateway-logger
* @author manzoj
* @version 1
*/

Expand All @@ -42,11 +41,6 @@ class OmnipayGatewayRequestSubscriberTest extends TestCase
*/
private $logger;

/**
* @var string
*/
private $requestName = 'test_request_name';

/**
* @return void
*/
Expand All @@ -73,9 +67,9 @@ public function providerLoggingEvents()
/** @var Exception $exception */
$exception = Mockery::mock('Exception');

$requestEvent = new RequestEvent($request, $this->requestName);
$responseEvent = new ResponseEvent($response, $this->requestName);
$errorEvent = new ErrorEvent($exception, $this->requestName);
$requestEvent = new RequestEvent($request);
$responseEvent = new ResponseEvent($response);
$errorEvent = new ErrorEvent($exception, $request);

$requestRecord = array(
'level' => LogLevel::INFO,
Expand Down Expand Up @@ -116,7 +110,6 @@ public function testLogging($event_type, $event, array $record)
$this->eventDispatcher->dispatch($event_type, $event);

$context = $event->toArray();
$this->assertEquals($this->requestName, $context['request_name']);

if ($record['level'] === LogLevel::INFO) {
$this->assertInstanceOf('Omnipay\Common\Message\RequestInterface', $context['request']);
Expand All @@ -128,6 +121,7 @@ public function testLogging($event_type, $event, array $record)
$this->assertTrue($this->logger->hasNotice($record));
} else if ($record['level'] === LogLevel::ERROR) {
$this->assertInstanceOf('\Exception', $context['error']);
$this->assertInstanceOf('Omnipay\Common\Message\RequestInterface', $context['request']);
$this->assertTrue($this->logger->hasErrorRecords());
$this->assertTrue($this->logger->hasError($record));
} else {
Expand Down

0 comments on commit f01c592

Please sign in to comment.