Skip to content

Commit 7c02f59

Browse files
author
JCManzo
authored
Merge pull request #3 from vimeo/update-logging-methods
Update logging methods usage in subscriber
2 parents 4cb7859 + a1adf8c commit 7c02f59

File tree

6 files changed

+36
-65
lines changed

6 files changed

+36
-65
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ These events are dispatched via the HTTP client's `EventDispatcherInterface`.
44
For example in the gateway's `sendData()` methods we can do:
55

66
```PHP
7+
$request_name; // The name of the API endpoint being called
78
$event_dispatcher = $this->httpClient->getEventDispatcher();
8-
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_BEFORE_SEND, new RequestEvent($request));
9+
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_BEFORE_SEND, new RequestEvent($request, $request_name));
910
```
1011

1112
Logging Errors and Responses events can be emitted like so
1213
```PHP
13-
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_ERROR new ErrorEvent($exception));
14-
$event_dispatcher->dispatch(Constants::OMNIPAY_RESPONSE_SUCCESS, new ResponseEvent($response));
14+
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_ERROR new ErrorEvent($exception', $request_name));
15+
$event_dispatcher->dispatch(Constants::OMNIPAY_RESPONSE_SUCCESS, new ResponseEvent($response, $request_name));
1516
```
1617

1718
`OmnipayGatewayRequestSubscriber.php` takes in a logger of type `LoggerInterface` which will listen to and log these events.
@@ -22,5 +23,5 @@ The subscriber can be set up to listen to these events when instantiating the H
2223
$httpClient = new GuzzleClient();
2324
$gateway = Omnipay::create('Vindicia', $httpClient);
2425
$eventDispatcher = $httpClient->getEventDispatcher();
25-
$eventDispatcher->addSubscriber(new OmnipayGatewayRequestSubscriber($gateway_name, new LoggerClassThatImplementsInterface()));
26+
$eventDispatcher->addSubscriber(new OmnipayGatewayRequestSubscriber($gateway_name, new LoggerClassThatImplementsPSRInterface()));
2627
```

src/Event/ErrorEvent.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,13 @@
1414

1515
class ErrorEvent extends Event
1616
{
17-
/**
18-
* @var Exception
19-
*/
20-
protected $error;
21-
2217
/**
2318
* @param Exception $error
19+
* @param string $request_name
2420
*/
25-
public function __construct($error)
26-
{
27-
$this->error = $error;
28-
29-
parent::__construct(array('error' => $error));
30-
}
31-
32-
/**
33-
* @return Exception
34-
*/
35-
public function getContext()
21+
public function __construct($error, $request_name)
3622
{
37-
return $this->error;
23+
parent::__construct(array('error' => $error, 'request_name' => $request_name));
3824
}
3925

4026
/**

src/Event/RequestEvent.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,13 @@
1414

1515
class RequestEvent extends Event
1616
{
17-
/**
18-
* @var RequestInterface
19-
*/
20-
protected $request;
21-
2217
/**
2318
* @param RequestInterface $request
19+
* @param string $request_name
2420
*/
25-
public function __construct($request)
26-
{
27-
$this->request = $request;
28-
29-
parent::__construct(array('request' => $request));
30-
}
31-
32-
/**
33-
* @return RequestInterface
34-
*/
35-
public function getContext()
21+
public function __construct($request, $request_name)
3622
{
37-
return $this->request;
23+
parent::__construct(array('request' => $request, 'request_name' => $request_name));
3824
}
3925

4026
/**

src/Event/ResponseEvent.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,13 @@
1414

1515
class ResponseEvent extends Event
1616
{
17-
/**
18-
* @var ResponseInterface
19-
*/
20-
protected $response;
21-
2217
/**
2318
* @param ResponseInterface $response
19+
* @param string $request_name
2420
*/
25-
public function __construct($response)
26-
{
27-
$this->response = $response;
28-
29-
parent::__construct(array('response' => $response));
30-
}
31-
32-
/**
33-
* @return ResponseInterface
34-
*/
35-
public function getContext()
21+
public function __construct($response, $request_name)
3622
{
37-
return $this->response;
23+
parent::__construct(array('response' => $response, 'request_name' => $request_name));
3824
}
3925

4026
/**

src/Event/Subscriber/OmnipayGatewayRequestSubscriber.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Guzzle\Common\Event;
1313
use PaymentGatewayLogger\Event\Constants;
1414
use Psr\Log\LoggerInterface;
15-
use Psr\Log\LogLevel;
1615
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1716

1817
class OmnipayGatewayRequestSubscriber implements EventSubscriberInterface
@@ -80,7 +79,7 @@ public static function getSubscribedEvents()
8079
*/
8180
public function onOmnipayRequestBeforeSend(Event $event)
8281
{
83-
$this->logger->log(LogLevel::INFO, $this->gateway_name, $event->toArray());
82+
$this->logger->info($this->gateway_name, $event->toArray());
8483
}
8584

8685
/**
@@ -95,7 +94,7 @@ public function onOmnipayRequestBeforeSend(Event $event)
9594
*/
9695
public function onOmnipayResponseSuccess(Event $event)
9796
{
98-
$this->logger->log(LogLevel::INFO, $this->gateway_name, $event->toArray());
97+
$this->logger->notice($this->gateway_name, $event->toArray());
9998
}
10099

101100
/**
@@ -110,6 +109,6 @@ public function onOmnipayResponseSuccess(Event $event)
110109
*/
111110
public function onOmnipayRequestError(Event $event)
112111
{
113-
$this->logger->log(LogLevel::ERROR, $this->gateway_name, $event->toArray());
112+
$this->logger->error($this->gateway_name, $event->toArray());
114113
}
115114
}

tests/OmnipayGatewayRequestSubscriberTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace PaymentGatewayLogger;
44

55
use Exception;
6-
use InvalidArgumentException;
76
use Guzzle\Common\Event;
7+
use InvalidArgumentException;
88
use Guzzle\Http\Client;
99
use Mockery;
1010
use Omnipay\Common\Message\RequestInterface;
@@ -42,6 +42,11 @@ class OmnipayGatewayRequestSubscriberTest extends TestCase
4242
*/
4343
private $logger;
4444

45+
/**
46+
* @var string
47+
*/
48+
private $requestName = 'test_request_name';
49+
4550
/**
4651
* @return void
4752
*/
@@ -51,7 +56,6 @@ protected function setUp()
5156
$this->eventDispatcher = $httpClient->getEventDispatcher();
5257
$this->logger = new TestLogger();
5358
$this->subscriber = new OmnipayGatewayRequestSubscriber('test', $this->logger);
54-
5559
parent::setUp();
5660
}
5761

@@ -69,17 +73,17 @@ public function providerLoggingEvents()
6973
/** @var Exception $exception */
7074
$exception = Mockery::mock('Exception');
7175

72-
$requestEvent = new RequestEvent($request);
73-
$responseEvent = new ResponseEvent($response);
74-
$errorEvent = new ErrorEvent($exception);
76+
$requestEvent = new RequestEvent($request, $this->requestName);
77+
$responseEvent = new ResponseEvent($response, $this->requestName);
78+
$errorEvent = new ErrorEvent($exception, $this->requestName);
7579

7680
$requestRecord = array(
7781
'level' => LogLevel::INFO,
7882
'message' => 'omnipay_test',
7983
'context' => $requestEvent->toArray(),
8084
);
8185
$responseRecord = array(
82-
'level' => LogLevel::INFO,
86+
'level' => LogLevel::NOTICE,
8387
'message' => 'omnipay_test',
8488
'context' => $responseEvent->toArray(),
8589
);
@@ -111,10 +115,19 @@ public function testLogging($event_type, $event, array $record)
111115
$this->eventDispatcher->addSubscriber($this->subscriber);
112116
$this->eventDispatcher->dispatch($event_type, $event);
113117

118+
$context = $event->toArray();
119+
$this->assertEquals($this->requestName, $context['request_name']);
120+
114121
if ($record['level'] === LogLevel::INFO) {
122+
$this->assertInstanceOf('Omnipay\Common\Message\RequestInterface', $context['request']);
115123
$this->assertTrue($this->logger->hasInfoRecords());
116124
$this->assertTrue($this->logger->hasInfo($record));
125+
} else if ($record['level'] === LogLevel::NOTICE) {
126+
$this->assertInstanceOf('Omnipay\Common\Message\ResponseInterface', $context['response']);
127+
$this->assertTrue($this->logger->hasNoticeRecords());
128+
$this->assertTrue($this->logger->hasNotice($record));
117129
} else if ($record['level'] === LogLevel::ERROR) {
130+
$this->assertInstanceOf('\Exception', $context['error']);
118131
$this->assertTrue($this->logger->hasErrorRecords());
119132
$this->assertTrue($this->logger->hasError($record));
120133
} else {

0 commit comments

Comments
 (0)