Skip to content

Commit 621e498

Browse files
authored
Merge pull request #1 from vimeo/add-event-logging
Add event logging
2 parents c5876ef + b4f531f commit 621e498

14 files changed

+803
-3
lines changed

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: php
2+
matrix:
3+
include:
4+
- php: '5.3'
5+
env:
6+
- TEST_SUITE='no_psalm'
7+
- php: '5.4'
8+
env:
9+
- TEST_SUITE='no_psalm'
10+
- php: '5.5'
11+
env:
12+
- TEST_SUITE='no_psalm'
13+
- php: '5.6'
14+
env:
15+
- TEST_SUITE='with_psalm'
16+
- COMPOSER='composer-psalm.json'
17+
- php: '7.0'
18+
env:
19+
- TEST_SUITE='with_psalm'
20+
- COMPOSER='composer-psalm.json'
21+
- php: '7.1'
22+
env:
23+
- TEST_SUITE='with_psalm'
24+
- COMPOSER='composer-psalm.json'
25+
install: composer install
26+
script: make $TEST_SUITE
27+
dist: precise
28+
notifications:
29+
email: false

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Vimeo, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Test
2+
3+
all: with_psalm
4+
5+
no_psalm: style test
6+
7+
with_psalm: style psalm test
8+
9+
style:
10+
vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpcs --standard=PSR2 --error-severity=1 --warning-severity=6 tests
11+
12+
test:
13+
vendor/bin/phpunit tests
14+
15+
psalm:
16+
vendor/bin/psalm
17+
18+
# Install
19+
20+
install: install_with_psalm
21+
22+
install_with_psalm:
23+
COMPOSER=composer-psalm.json php composer.phar install
24+
25+
install_no_psalm:
26+
php composer.phar install
27+
28+
# Update
29+
30+
update: update_with_psalm
31+
32+
update_with_psalm:
33+
COMPOSER=composer-psalm.json php composer.phar update
34+
35+
update_no_psalm:
36+
php composer.phar update

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
Logging capabilities for Omnipay gateways
1+
Logging capabilities for Omnipay gateways via an `EventSubscriberInterface` subscriber for Omnipay payment gateway-specific events.
2+
These events are dispatched via the HTTP client's `EventDispatcherInterface`.
3+
* The omnipay gateway needs to be updated to emit any of the `RequestEvent`, `ResponseEvent` or `ErrorEvent` objects.
4+
For example in the gateway's `sendData()` methods we can do:
5+
6+
```PHP
7+
$event_dispatcher = $this->httpClient->getEventDispatcher();
8+
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_BEFORE_SEND, new RequestEvent($request));
9+
```
10+
11+
Logging Errors and Responses events can be emitted like so
12+
```PHP
13+
$event_dispatcher->dispatch(Constants::OMNIPAY_REQUEST_ERROR new ErrorEvent($exception));
14+
$event_dispatcher->dispatch(Constants::OMNIPAY_RESPONSE_SUCCESS, new ResponseEvent($response));
15+
```
16+
17+
`OmnipayGatewayRequestSubscriber.php` takes in a logger of type `LoggerInterface` which will listen to and log these events.
18+
19+
The subscriber can be set up to listen to these events when instantiating the HTTP client for the gateway like so:
20+
21+
```PHP
22+
$httpClient = new GuzzleClient();
23+
$gateway = Omnipay::create('Vindicia', $httpClient);
24+
$eventDispatcher = $httpClient->getEventDispatcher();
25+
$eventDispatcher->addSubscriber(new OmnipayGatewayRequestSubscriber($gateway_name, new LoggerClassThatImplementsInterface()));
26+
```

composer-psalm.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "vimeo/payment-gateway-logger",
3+
"description": "Logging capabilities for Omnipay gateways",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "J.C. Manzo",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"PaymentGatewayLogger\\" : "src/",
15+
"PaymentGatewayLogger\\Test\\" : "tests/"
16+
}
17+
},
18+
"require": {
19+
"guzzle/guzzle": "^3.9",
20+
"psr/log": "^1.0",
21+
"omnipay/common": "~2.0"
22+
},
23+
"require-dev": {
24+
"omnipay/tests": "~2.0",
25+
"vimeo/psalm": "0.3.*"
26+
}
27+
}

composer.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@
99
"email": "[email protected]"
1010
}
1111
],
12-
"require": {},
12+
"autoload": {
13+
"psr-4": {
14+
"PaymentGatewayLogger\\" : "src/",
15+
"PaymentGatewayLogger\\Test\\" : "tests/"
16+
}
17+
},
18+
"require": {
19+
"guzzle/guzzle": "^3.9",
20+
"psr/log": "1.0.0",
21+
"omnipay/common": "~2.0"
22+
},
1323
"require-dev": {
14-
"vimeo/psalm": "^3.2"
24+
"omnipay/tests": "~2.0"
1525
}
1626
}

psalm.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
name="Psalm for Payment gateway logger driver"
4+
useDocblockTypes="true"
5+
>
6+
<projectFiles>
7+
<directory name="src" />
8+
<directory name="tests" />
9+
</projectFiles>
10+
11+
<fileExtensions>
12+
<extension name=".php" />
13+
</fileExtensions>
14+
15+
<issueHandlers>
16+
<MixedArgument errorLevel="suppress" />
17+
<MixedMethodCall errorLevel="suppress" />
18+
<MixedPropertyFetch errorLevel="suppress" />
19+
<MixedPropertyAssignment errorLevel="suppress" />
20+
<MixedInferredReturnType errorLevel="suppress" />
21+
<MixedArrayAccess errorLevel="suppress" />
22+
23+
<!-- Psalm doesn't like that initialize() sets instance variables for the constructor -->
24+
<PropertyNotSetInConstructor errorLevel="suppress" />
25+
26+
<!-- Unit tests set lots of instance variables inline -->
27+
<UndefinedThisPropertyAssignment>
28+
<errorLevel type="suppress">
29+
<directory name="tests" />
30+
</errorLevel>
31+
</UndefinedThisPropertyAssignment>
32+
<MissingPropertyType>
33+
<errorLevel type="suppress">
34+
<directory name="tests" />
35+
</errorLevel>
36+
</MissingPropertyType>
37+
<InaccessibleProperty>
38+
<errorLevel type="suppress">
39+
<directory name="tests" />
40+
</errorLevel>
41+
</InaccessibleProperty>
42+
<TooManyArguments>
43+
<errorLevel type="suppress">
44+
<directory name="tests" />
45+
</errorLevel>
46+
</TooManyArguments>
47+
<MissingClosureReturnType>
48+
<errorLevel type="suppress">
49+
<file name="src/TestLogger.php" />
50+
</errorLevel>
51+
</MissingClosureReturnType>
52+
<MissingClosureParamType>
53+
<errorLevel type="suppress">
54+
<file name="src/TestLogger.php" />
55+
</errorLevel>
56+
</MissingClosureParamType>
57+
</issueHandlers>
58+
59+
<mockClasses>
60+
<class name="Mockery\MockInterface" />
61+
</mockClasses>
62+
63+
</psalm>

src/Event/Constants.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PaymentGatewayLogger\Event;
4+
5+
/**
6+
* Class events.
7+
*
8+
* @package payment-gateway-logger
9+
* @author manzoj
10+
* @version 1
11+
*/
12+
13+
final class Constants
14+
{
15+
const OMNIPAY_REQUEST_BEFORE_SEND = 'omnipay.request.before_send';
16+
const OMNIPAY_RESPONSE_SUCCESS = 'omnipay.response.success';
17+
const OMNIPAY_REQUEST_ERROR = 'omnipay.request.error';
18+
}

src/Event/ErrorEvent.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Error event to be used by the payment gateway logger.
4+
*
5+
* @package payment-gateway-logger
6+
* @author manzoj
7+
* @version 1
8+
*/
9+
10+
namespace PaymentGatewayLogger\Event;
11+
12+
use Exception;
13+
use Guzzle\Common\Event;
14+
15+
class ErrorEvent extends Event
16+
{
17+
/**
18+
* @var Exception
19+
*/
20+
protected $error;
21+
22+
/**
23+
* @param Exception $error
24+
*/
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()
36+
{
37+
return $this->error;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getType()
44+
{
45+
return Constants::OMNIPAY_REQUEST_ERROR;
46+
}
47+
}

src/Event/RequestEvent.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Request event to be used by the payment gateway logger.
4+
*
5+
* @package payment-gateway-logger
6+
* @author manzoj
7+
* @version 1
8+
*/
9+
10+
namespace PaymentGatewayLogger\Event;
11+
12+
use Guzzle\Common\Event;
13+
use Omnipay\Common\Message\RequestInterface;
14+
15+
class RequestEvent extends Event
16+
{
17+
/**
18+
* @var RequestInterface
19+
*/
20+
protected $request;
21+
22+
/**
23+
* @param RequestInterface $request
24+
*/
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()
36+
{
37+
return $this->request;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getType()
44+
{
45+
return Constants::OMNIPAY_REQUEST_BEFORE_SEND;
46+
}
47+
}

0 commit comments

Comments
 (0)