diff --git a/composer.lock b/composer.lock
index 122f0fd..0ee40d4 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "c658f2c59e9ea90ac5e176dac443cd80",
- "content-hash": "db9c721cc0d3a114db5a58c6a7378e8f",
+ "hash": "bea16da66da6f91e179521c07fc5ae30",
+ "content-hash": "7d487855f7afb2f3e7ab23495527aae4",
"packages": [
{
"name": "robrichards/wse-php",
diff --git a/examples/simpleClient/Example.php b/examples/simpleClient/Example.php
index 6b5b5ef..7ba47da 100644
--- a/examples/simpleClient/Example.php
+++ b/examples/simpleClient/Example.php
@@ -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();
@@ -30,7 +31,7 @@
var_dump($e); // Fatal error
}
-echo sprintf('Request size: %s bytes | Response size: %s bytes.
', $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
', $dispatcher->getLastRequestSize(), $dispatcher->getLastResponseSize(), $dispatcher->getLastResponseTime(), $dispatcher->getConnectionTime()); // Size of transferred data
// Example of error message
$r->dic_popl = 'x';
@@ -44,4 +45,4 @@
var_dump($e); // Fatal error
}
-echo sprintf('Request size: %s bytes | Response size: %s bytes.
', $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
', $dispatcher->getLastRequestSize(), $dispatcher->getLastResponseSize(), $dispatcher->getLastResponseTime(), $dispatcher->getConnectionTime()); // Size of transferred data
diff --git a/src/Dispatcher.php b/src/Dispatcher.php
index 16874a8..9b1ec67 100644
--- a/src/Dispatcher.php
+++ b/src/Dispatcher.php
@@ -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;
@@ -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
@@ -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');
}
@@ -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
@@ -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);
}
/**
diff --git a/src/Exceptions/ClientException.php b/src/Exceptions/ClientException.php
new file mode 100644
index 0000000..cd45e06
--- /dev/null
+++ b/src/Exceptions/ClientException.php
@@ -0,0 +1,7 @@
+ 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) {
@@ -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;
}
-}
\ No newline at end of file
+
+ /**
+ *
+ * @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;
+ }
+
+}
diff --git a/tests/EET/Dispatcher.phpt b/tests/EET/Dispatcher.phpt
index 4ebbb63..c06fb86 100644
--- a/tests/EET/Dispatcher.phpt
+++ b/tests/EET/Dispatcher.phpt
@@ -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;
@@ -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);
@@ -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