Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"ext-simplexml": "*",
"ext-intl": "*",
"ext-mbstring": "*",
"monolog/monolog": "*"
},
"require-dev" : {
"phpunit/phpunit": "^7",
Expand Down
26 changes: 19 additions & 7 deletions lib/Ogone/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace Ogone;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Ogone\Logger\AdapterInterface;
use Ogone\Logger\MonologAdapter;
use RuntimeException;
use BadMethodCallException;
use Ogone\ShaComposer\ShaComposer;
Expand Down Expand Up @@ -158,11 +159,9 @@ abstract class AbstractRequest implements Request

protected $parameters = array();

/** @var LoggerInterface|null */
/** @var Logger */
protected $logger;



protected $ogoneFields = array(
'orig', 'shoppingcartextensionid', 'pspid', 'orderid', 'com', 'amount', 'currency', 'language', 'cn', 'email',
'cardno', 'cvc', 'ed', 'ownerzip', 'owneraddress', 'ownercty', 'ownertown', 'ownertelno',
Expand Down Expand Up @@ -233,13 +232,26 @@ abstract class AbstractRequest implements Request
/**
* Sets Logger.
*
* @param LoggerInterface|null $logger
* @param AdapterInterface $logger
*
* @return $this
* @throws \Exception
*/
public function setLogger(LoggerInterface $logger = null)
public function setLogger($logger)
{
$this->logger = $logger;
if (interface_exists('\Psr\Log\LoggerInterface') &&
$logger instanceof \Psr\Log\LoggerInterface
) {
$this->logger = new Logger(
new MonologAdapter(['logger' => $logger])
);
}

if (!$logger instanceof AdapterInterface) {
throw new \Exception('Argument $logger must be instance of AdapterInterface.');
}

$this->logger = new Logger($logger);

return $this;
}
Expand Down
24 changes: 19 additions & 5 deletions lib/Ogone/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace Ogone;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Ogone\Logger\AdapterInterface;
use Ogone\Logger\MonologAdapter;

abstract class AbstractResponse implements Response, \ArrayAccess
{
Expand Down Expand Up @@ -100,7 +101,7 @@ abstract class AbstractResponse implements Response, \ArrayAccess
*/
protected $shaSign;

/** @var LoggerInterface|null */
/** @var Logger */
protected $logger;

/**
Expand All @@ -127,13 +128,26 @@ public function __construct(array $httpRequest)
/**
* Sets Logger.
*
* @param LoggerInterface|null $logger
* @param AdapterInterface $logger
*
* @return $this
* @throws \Exception
*/
public function setLogger(LoggerInterface $logger = null)
public function setLogger($logger)
{
$this->logger = $logger;
if (interface_exists('\Psr\Log\LoggerInterface') &&
$logger instanceof \Psr\Log\LoggerInterface
) {
$this->logger = new Logger(
new MonologAdapter(['logger' => $logger])
);
}

if (!$logger instanceof AdapterInterface) {
throw new \Exception('Argument $logger must be instance of AdapterInterface.');
}

$this->logger = new Logger($logger);

return $this;
}
Expand Down
130 changes: 130 additions & 0 deletions lib/Ogone/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Ogone;

use Ogone\Logger\AdapterInterface;

class Logger
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';

/**
* @var AdapterInterface
*/
private $adapter;

/**
* @param AdapterInterface $adapter
*/
public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}

/**
* Logs with emergency level.
*
* @param string $message
* @param array $context
*/
public function emergency($message, array $context = [])
{
$this->log(self::EMERGENCY, $message, $context);
}

/**
* Logs with error level.
*
* @param string $message
* @param array $context
*/
public function alert($message, array $context = [])
{
$this->log(self::ALERT, $message, $context);
}

/**
* Logs with critical level.
*
* @param string $message
* @param array $context
*/
public function critical($message, array $context = [])
{
$this->log(self::CRITICAL, $message, $context);
}

/**
* Logs with error level.
*
* @param string $message
* @param array $context
*/
public function error($message, array $context = [])
{
$this->log(self::ERROR, $message, $context);
}

/**
* Logs with warning level.
*
* @param string $message
* @param array $context
*/
public function warning($message, array $context = [])
{
$this->log(self::WARNING, $message, $context);
}

/**
* Logs with notice level.
*
* @param string $message
* @param array $context
*/
public function notice($message, array $context = [])
{
$this->log(self::NOTICE, $message, $context);
}

/**
* Logs with info level.
*
* @param string $message
* @param array $context
*/
public function info($message, array $context = [])
{
$this->log(self::INFO, $message, $context);
}

/**
* Logs with debug level.
*
* @param string $message
* @param array $context
*/
public function debug($message, array $context = [])
{
$this->log(self::DEBUG, $message, $context);
}

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
$this->adapter->log($level, $message, $context);
}
}
15 changes: 15 additions & 0 deletions lib/Ogone/Logger/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Ogone\Logger;

interface AdapterInterface
{
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = []);
}
44 changes: 44 additions & 0 deletions lib/Ogone/Logger/FileAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ogone\Logger;

class FileAdapter implements AdapterInterface
{
private $file;

/**
* Constructor.
*
* @param array $options
*/
public function __construct(array $options)
{
if (isset($options['file'])) {
$this->file = $options['file'];
} else {
$this->file = sys_get_temp_dir() . '/ingenico_sdk.log';
}
}

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
if ($this->file) {
$message = sprintf(
'[%s] %s %s %s',
date('Y-m-d H:i:s'),
$level,
$message,
count($context) > 0 ? var_export($context, true) : ''
);

file_put_contents($this->file, $message . "\n", FILE_APPEND);
}
}
}
37 changes: 37 additions & 0 deletions lib/Ogone/Logger/MonologAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Ogone\Logger;

class MonologAdapter implements AdapterInterface
{
/**
* @var \Monolog\Logger|\Psr\Log\LoggerInterface
*/
private $logger;

/**
* Constructor.
*
* @param array $options
*/
public function __construct(array $options)
{
if (isset($options['logger'])) {
$this->logger = $options['logger'];
}
}

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
if ($this->logger) {
$this->logger->log($level, $message, $context);
}
}
}
16 changes: 16 additions & 0 deletions tests/Ogone/Tests/AbstractRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Ogone\Tests;

use PHPUnit\Framework\TestCase;
use Ogone\Ecommerce\EcommercePaymentRequest;
use Ogone\Logger\FileAdapter;

class AbstractRequestTest extends TestCase
{
public function testSetLogger()
{
//$request = new EcommercePaymentRequest();
//new FileAdapter(['file' => sys_get_temp_dir() . '/log.txt']);
}
}
22 changes: 22 additions & 0 deletions tests/Ogone/Tests/Logger/FileAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Ogone\Tests\Logger;

use PHPUnit\Framework\TestCase;
use Ogone\Logger;
use Ogone\Logger\AdapterInterface;
use Ogone\Logger\FileAdapter;

class FileAdapterTest extends TestCase
{
public function testLogAdapter()
{
$logFile = sys_get_temp_dir() . '/ingenico_sdk.log';

$adapter = new FileAdapter(['file' => $logFile]);
$this->assertInstanceOf(AdapterInterface::class, $adapter);

$adapter->log(Logger::INFO, 'Test');
$this->assertEquals(true, file_exists($logFile));
}
}
Loading