Skip to content

Commit

Permalink
Optional tracing, + response/connection time tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
miloshavlicek committed Jul 20, 2016
1 parent a9a063e commit a9b4d38
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 12 deletions.
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/simpleClient/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Ondrejnov\EET\Receipt;

$dispatcher = new Dispatcher(PLAYGROUND_WSDL, DIR_CERT . '/eet.key', DIR_CERT . '/eet.pem');
$dispatcher->trace = TRUE;

// Example receipt
$r = new Receipt();
Expand All @@ -30,7 +31,7 @@
var_dump($e); // Fatal error
}

echo sprintf('Request size: %s bytes | Response size: %s bytes.<br />', $dispatcher->getLastRequestSize(), $dispatcher->getLastResponseSize()); // Size of transferred data
echo sprintf('Request size: %d bytes | Response size: %d bytes | Response time: %f ms | Connection time: %f ms<br />', $dispatcher->getLastRequestSize(), $dispatcher->getLastResponseSize(), $dispatcher->getLastResponseTime(), $dispatcher->getConnectionTime()); // Size of transferred data
// Example of error message
$r->dic_popl = 'x';

Expand All @@ -44,4 +45,4 @@
var_dump($e); // Fatal error
}

echo sprintf('Request size: %s bytes | Response size: %s bytes.<br />', $dispatcher->getLastRequestSize(), $dispatcher->getLastResponseSize()); // Size of transferred data
echo sprintf('Request size: %d bytes | Response size: %d bytes | Response time: %f ms | Connection time: %f ms<br />', $dispatcher->getLastRequestSize(), $dispatcher->getLastResponseSize(), $dispatcher->getLastResponseTime(), $dispatcher->getConnectionTime()); // Size of transferred data
46 changes: 42 additions & 4 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ondrejnov\EET;

use Ondrejnov\EET\Exceptions\ClientException;
use Ondrejnov\EET\Exceptions\RequirementsException;
use Ondrejnov\EET\Exceptions\ServerException;
use Ondrejnov\EET\SoapClient;
Expand All @@ -14,19 +15,27 @@ class Dispatcher {

/**
* Certificate key
* @var string */
* @var string
*/
private $key;

/**
* Certificate
* @var string */
* @var string
*/
private $cert;

/**
* WSDL path or URL
* @var string */
* @var string
*/
private $service;

/**
* @var boolean
*/
public $trace;

/**
*
* @var SoapClient
Expand Down Expand Up @@ -59,11 +68,22 @@ public function check(Receipt $receipt) {
}
}

/**
*
* @param boolean $tillLastRequest optional If not set/FALSE connection time till now is returned.
* @return float
*/
public function getConnectionTime($tillLastRequest = FALSE) {
!$this->trace && $this->throwTraceNotEnabled();
return $this->getSoapClient()->__getConnectionTime($tillLastRequest);
}

/**
*
* @return int
*/
public function getLastResponseSize() {
!$this->trace && $this->throwTraceNotEnabled();
return mb_strlen($this->getSoapClient()->__getLastResponse(), '8bit');
}

Expand All @@ -72,9 +92,27 @@ public function getLastResponseSize() {
* @return int
*/
public function getLastRequestSize() {
!$this->trace && $this->throwTraceNotEnabled();
return mb_strlen($this->getSoapClient()->__getLastRequest(), '8bit');
}

/**
*
* @return float time in ms
*/
public function getLastResponseTime() {
!$this->trace && $this->throwTraceNotEnabled();
return $this->getSoapClient()->__getLastResponseTime();
}

/**
*
* @throws ClientException
*/
private function throwTraceNotEnabled() {
throw new ClientException('Trace is not enabled! Set trace property to TRUE.');
}

/**
*
* @param \Ondrejnov\EET\Receipt $receipt
Expand Down Expand Up @@ -152,7 +190,7 @@ private function getSoapClient() {
* @return void
*/
private function initSoapClient() {
$this->soapClient = new SoapClient($this->service, $this->key, $this->cert);
$this->soapClient = new SoapClient($this->service, $this->key, $this->cert, $this->trace);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptions/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ondrejnov\EET\Exceptions;

class ClientException extends \Exception {

}
72 changes: 68 additions & 4 deletions src/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@

class SoapClient extends \SoapClient {

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

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

public function __construct($service, $key, $cert) {
parent::__construct($service, ['trace' => 1]);
/** @var boolean */
private $traceRequired;

/** @var float */
private $connectionStartTime;

/** @var float */
private $lastResponseStartTime;

/** @var float */
private $lastResponseEndTime;

/**
*
* @param string $service
* @param string $key
* @param string $cert
* @param boolean $trace
*/
public function __construct($service, $key, $cert, $trace = FALSE) {
$this->connectionStartTime = microtime(TRUE);
parent::__construct($service, [
'exceptions' => TRUE,
'trace' => $trace
]);
$this->key = $key;
$this->cert = $cert;
$this->traceRequired = $trace;
}

public function __doRequest($request, $location, $saction, $version, $one_way = NULL) {
Expand All @@ -27,6 +54,43 @@ public function __doRequest($request, $location, $saction, $version, $one_way =
$token = $objWSSE->addBinaryToken(file_get_contents($this->cert));
$objWSSE->attachTokentoSig($token);

return parent::__doRequest($objWSSE->saveXML(), $location, $saction, $version);
$this->traceRequired && $this->lastResponseStartTime = microtime(TRUE);

$response = parent::__doRequest($objWSSE->saveXML(), $location, $saction, $version);

$this->traceRequired && $this->lastResponseEndTime = microtime(TRUE);

return $response;
}
}

/**
*
* @return float
*/
public function __getLastResponseTime() {
return $this->lastResponseEndTime - $this->lastResponseStartTime;
}

/**
*
* @return float
*/
public function __getConnectionTime($tillLastRequest = FALSE) {
return $tillLastRequest ? $this->getConnectionTimeTillLastRequest() : $this->getConnectionTimeTillNow();
}

private function getConnectionTimeTillLastRequest() {
if (!$this->lastResponseEndTime || !$this->connectionStartTime) {
return NULL;
}
return $this->lastResponseEndTime - $this->connectionStartTime;
}

private function getConnectionTimeTillNow() {
if (!$this->connectionStartTime) {
return NULL;
}
return microtime(TRUE) - $this->connectionStartTime;
}

}
38 changes: 38 additions & 0 deletions tests/EET/Dispatcher.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ondrejnov\EET\Test;

use Ondrejnov\EET\Dispatcher as Tested;
use Ondrejnov\EET\Exceptions\ClientException;
use Ondrejnov\EET\Exceptions\ServerException;
use Ondrejnov\EET\Receipt;
use Tester\Assert;
Expand All @@ -24,8 +25,36 @@ class Dispatcher extends \Tester\TestCase {
}, ServerException::class);
}

public function testGetConnectionTime() {
$dispatcher = $this->getTestDispatcher();
$dispatcher->trace = TRUE;
$dispatcher->send($this->getExampleReceipt());
$time = $dispatcher->getConnectionTime();
Assert::type('float', $time);
Assert::true($time > 0);
}

public function testGetConnectionTimeTillLastRequest() {
$dispatcher = $this->getTestDispatcher();
$dispatcher->trace = TRUE;
$dispatcher->send($this->getExampleReceipt());
$time = $dispatcher->getConnectionTime(TRUE);
Assert::type('float', $time);
Assert::true($time > 0);
}

public function testGetLastResponseTime() {
$dispatcher = $this->getTestDispatcher();
$dispatcher->trace = TRUE;
$dispatcher->send($this->getExampleReceipt());
$time = $dispatcher->getLastResponseTime();
Assert::type('float', $time);
Assert::true($time > 0);
}

public function testGetLastRequestSize() {
$dispatcher = $this->getTestDispatcher();
$dispatcher->trace = TRUE;
$dispatcher->send($this->getExampleReceipt());
$size = $dispatcher->getLastRequestSize();
Assert::type('int', $size);
Expand All @@ -34,12 +63,21 @@ class Dispatcher extends \Tester\TestCase {

public function testGetLastResponseSize() {
$dispatcher = $this->getTestDispatcher();
$dispatcher->trace = TRUE;
$dispatcher->send($this->getExampleReceipt());
$size = $dispatcher->getLastResponseSize();
Assert::type('int', $size);
Assert::true($size > 0);
}

public function testTraceNotEnabled() {
$dispatcher = $this->getTestDispatcher();
$dispatcher->send($this->getExampleReceipt());
Assert::exception(function() use ($dispatcher) {
$dispatcher->getLastResponseSize();
}, ClientException::class);
}

/**
*
* @return Tested
Expand Down

0 comments on commit a9b4d38

Please sign in to comment.