Skip to content

Commit

Permalink
Merge pull request #101 from jan-stanek/set_dispatcher
Browse files Browse the repository at this point in the history
EventDispatcher setter + trace in Post/Fail event
  • Loading branch information
jan-stanek authored Sep 20, 2022
2 parents 207eab3 + 449faf0 commit 15deee9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 9 deletions.
20 changes: 19 additions & 1 deletion src/Wsdl/Event/RequestFailEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,31 @@ class RequestFailEvent implements Serializable
*/
private $exceptionString;

/**
* @var array<int, array<string, mixed>> Zasobnik volanych funkci
*/
private $trace;


/**
* @param string $fname Nazev volane funkce
* @param array<int|string, mixed> $args Argumenty pozadavku
* @param array<int, array<string, mixed>> $trace Zasobnik volanych funkci
*/
public function __construct(
string $fname,
array $args,
Throwable $throwable,
float $duration
float $duration,
array $trace
) {
$this->fname = $fname;
$this->args = $args;
$this->throwable = $throwable;
$this->exceptionClass = get_class($throwable);
$this->exceptionString = (string) $throwable;
$this->time = $duration;
$this->trace = $trace;
}

/**
Expand All @@ -74,6 +83,7 @@ public function __serialize(): array {
'time' => $this->time,
'exception_class' => $this->exceptionClass,
'exception_string' => $this->exceptionString,
'trace' => $this->trace,
];
}

Expand All @@ -91,6 +101,7 @@ public function __unserialize(array $data): void {
$this->time = (float) $data['time'];
$this->exceptionClass = (string) $data['exception_class'];
$this->exceptionString = (string) $data['exception_string'];
$this->trace = (array) $data['trace'];
}

/**
Expand Down Expand Up @@ -162,4 +173,11 @@ public function getThrowable(): ?Throwable
return $this->throwable;
}

/**
* @return array<int, array<string, mixed>>
*/
public function getTrace(): array
{
return $this->trace;
}
}
20 changes: 19 additions & 1 deletion src/Wsdl/Event/RequestPostEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,30 @@ class RequestPostEvent implements Serializable
*/
private $result;

/**
* @var array<int, array<string, mixed>> Zasobnik volanych funkci
*/
private $trace;


/**
* @param string $fname Nazev volane funkce
* @param array<int|string, mixed> $args Argumenty pozadavku
* @param array<int|string, mixed>|stdClass|null $result
* @param array<int, array<string, mixed>> $trace Zasobnik volanych funkci
*/
public function __construct(
string $fname,
array $args,
$result,
float $duration
float $duration,
array $trace
) {
$this->fname = $fname;
$this->args = $args;
$this->result = $result;
$this->time = $duration;
$this->trace = $trace;
}

/**
Expand All @@ -59,6 +68,7 @@ public function __serialize(): array
'args' => $this->args,
'time' => $this->time,
'result' => $this->result,
'trace' => $this->trace,
];
}

Expand All @@ -76,6 +86,7 @@ public function __unserialize(array $data): void
$this->args = (array) $data['args'];
$this->time = (float) $data['time'];
$this->result = (array) $data['result'];
$this->trace = (array) $data['trace'];
}

/**
Expand Down Expand Up @@ -116,4 +127,11 @@ public function getResult()
return $this->result;
}

/**
* @return array<int, array<string, mixed>>
*/
public function getTrace(): array
{
return $this->trace;
}
}
8 changes: 5 additions & 3 deletions src/Wsdl/WebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ protected function soapCall(
) {
$fname = ucfirst($functionName);
$args = $this->prepareArgs($fname, $arguments);
$trace = [];

if ($this->eventDispatcher !== null) {
$event = new RequestPreEvent($fname, $args, $options, $inputHeaders, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$event = new RequestPreEvent($fname, $args, $options, $inputHeaders, $trace);
$this->eventDispatcher->dispatch($event);
}

Expand All @@ -101,7 +103,7 @@ protected function soapCall(

if ($this->eventDispatcher !== null) {
$duration = microtime(true) - $requestStart;
$event = new RequestPostEvent($fname, $args, $soapResponse, $duration);
$event = new RequestPostEvent($fname, $args, $soapResponse, $duration, $trace);
$this->eventDispatcher->dispatch($event);
}

Expand All @@ -110,7 +112,7 @@ protected function soapCall(

if ($this->eventDispatcher !== null) {
$duration = microtime(true) - $requestStart;
$event = new RequestFailEvent($fname, $args, $t, $duration);
$event = new RequestFailEvent($fname, $args, $t, $duration, $trace);
$this->eventDispatcher->dispatch($event);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Wsdl/WebServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public function createWebService(string $url, array $options): WebServiceInterfa
$soapClient = new \SoapClient($url, $options);
return new $this->class($soapClient, $options, $this->eventDispatcher);
}

/**
* @inheritdoc
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
if ($this->eventDispatcher != null) {
throw new InvalidArgumentException("Event dispatcher is already set.");
}

$this->eventDispatcher = $eventDispatcher;
}
}
10 changes: 10 additions & 0 deletions src/Wsdl/WebServiceFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Skaut\Skautis\Wsdl;

use Psr\EventDispatcher\EventDispatcherInterface;

/**
* Interface továrny pro vytváření objektů webových služeb
*/
Expand All @@ -18,4 +20,12 @@ interface WebServiceFactoryInterface
* @return WebServiceInterface
*/
public function createWebService(string $url, array $options): WebServiceInterface;

/**
* Nastaví event dispatcher, pokud uz neni nastaven.
*
* @param EventDispatcherInterface $eventDispatcher
* @return void
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void;
}
8 changes: 8 additions & 0 deletions src/Wsdl/WsdlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Skaut\Skautis\Wsdl;

use Psr\EventDispatcher\EventDispatcherInterface;
use Skaut\Skautis\Config;
use Skaut\Skautis\User;

Expand Down Expand Up @@ -75,6 +76,13 @@ public function createWebService(string $name, array $options = []): WebServiceI
return $this->webServiceFactory->createWebService($this->getWebServiceUrl($name), $options);
}

/**
* Nastaví event dispatcher.
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void {
$this->webServiceFactory->setEventDispatcher($eventDispatcher);
}

/**
* Vrací URL webové služby podle jejího jména
*/
Expand Down
9 changes: 6 additions & 3 deletions tests/Unit/Wsdl/Event/RequestFailEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class RequestFailEventTest extends TestCase
public function testExceptionMessage(): void
{
$throwable = new RuntimeException('my message');
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

$event = new RequestFailEvent('asd', [], $throwable, 30);
$event = new RequestFailEvent('asd', [], $throwable, 30, $trace);
$this->assertStringContainsString('my message', $event->getExceptionString());
$this->assertSame(RuntimeException::class, $event->getExceptionClass());
}
Expand All @@ -31,8 +32,9 @@ public function testDeserialization(): void
'argument' => 'value',
],
];
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

$event = new RequestFailEvent('asd', $args, $throwable, 30.22);
$event = new RequestFailEvent('asd', $args, $throwable, 30.22, $trace);

$serialized = serialize($event);
/** @var RequestFailEvent $unserialized */
Expand All @@ -56,8 +58,9 @@ public function testRepeatedSerializationDeserialization(): void
'argument' => 'value',
],
];
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

$event = new RequestFailEvent('asd', $args, $throwable, 30.22);
$event = new RequestFailEvent('asd', $args, $throwable, 30.22, $trace);

$serialized = serialize($event);
$unserialized = unserialize($serialized);
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Wsdl/Event/RequestPostEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public function testDeserialize(): void
];
$result = [(object)['a' => 'b']];
$duration = 11.11;
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

$event = new RequestPostEvent('asd', $args, $result, $duration);
$event = new RequestPostEvent('asd', $args, $result, $duration, $trace);

$serialized = serialize($event);
/** @var RequestPostEvent $unserialized */
Expand Down

0 comments on commit 15deee9

Please sign in to comment.