Skip to content

Commit 15deee9

Browse files
authored
Merge pull request #101 from jan-stanek/set_dispatcher
EventDispatcher setter + trace in Post/Fail event
2 parents 207eab3 + 449faf0 commit 15deee9

File tree

8 files changed

+81
-9
lines changed

8 files changed

+81
-9
lines changed

src/Wsdl/Event/RequestFailEvent.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,31 @@ class RequestFailEvent implements Serializable
4646
*/
4747
private $exceptionString;
4848

49+
/**
50+
* @var array<int, array<string, mixed>> Zasobnik volanych funkci
51+
*/
52+
private $trace;
53+
54+
4955
/**
5056
* @param string $fname Nazev volane funkce
5157
* @param array<int|string, mixed> $args Argumenty pozadavku
58+
* @param array<int, array<string, mixed>> $trace Zasobnik volanych funkci
5259
*/
5360
public function __construct(
5461
string $fname,
5562
array $args,
5663
Throwable $throwable,
57-
float $duration
64+
float $duration,
65+
array $trace
5866
) {
5967
$this->fname = $fname;
6068
$this->args = $args;
6169
$this->throwable = $throwable;
6270
$this->exceptionClass = get_class($throwable);
6371
$this->exceptionString = (string) $throwable;
6472
$this->time = $duration;
73+
$this->trace = $trace;
6574
}
6675

6776
/**
@@ -74,6 +83,7 @@ public function __serialize(): array {
7483
'time' => $this->time,
7584
'exception_class' => $this->exceptionClass,
7685
'exception_string' => $this->exceptionString,
86+
'trace' => $this->trace,
7787
];
7888
}
7989

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

96107
/**
@@ -162,4 +173,11 @@ public function getThrowable(): ?Throwable
162173
return $this->throwable;
163174
}
164175

176+
/**
177+
* @return array<int, array<string, mixed>>
178+
*/
179+
public function getTrace(): array
180+
{
181+
return $this->trace;
182+
}
165183
}

src/Wsdl/Event/RequestPostEvent.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,30 @@ class RequestPostEvent implements Serializable
3232
*/
3333
private $result;
3434

35+
/**
36+
* @var array<int, array<string, mixed>> Zasobnik volanych funkci
37+
*/
38+
private $trace;
39+
40+
3541
/**
3642
* @param string $fname Nazev volane funkce
3743
* @param array<int|string, mixed> $args Argumenty pozadavku
3844
* @param array<int|string, mixed>|stdClass|null $result
45+
* @param array<int, array<string, mixed>> $trace Zasobnik volanych funkci
3946
*/
4047
public function __construct(
4148
string $fname,
4249
array $args,
4350
$result,
44-
float $duration
51+
float $duration,
52+
array $trace
4553
) {
4654
$this->fname = $fname;
4755
$this->args = $args;
4856
$this->result = $result;
4957
$this->time = $duration;
58+
$this->trace = $trace;
5059
}
5160

5261
/**
@@ -59,6 +68,7 @@ public function __serialize(): array
5968
'args' => $this->args,
6069
'time' => $this->time,
6170
'result' => $this->result,
71+
'trace' => $this->trace,
6272
];
6373
}
6474

@@ -76,6 +86,7 @@ public function __unserialize(array $data): void
7686
$this->args = (array) $data['args'];
7787
$this->time = (float) $data['time'];
7888
$this->result = (array) $data['result'];
89+
$this->trace = (array) $data['trace'];
7990
}
8091

8192
/**
@@ -116,4 +127,11 @@ public function getResult()
116127
return $this->result;
117128
}
118129

130+
/**
131+
* @return array<int, array<string, mixed>>
132+
*/
133+
public function getTrace(): array
134+
{
135+
return $this->trace;
136+
}
119137
}

src/Wsdl/WebService.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ protected function soapCall(
8888
) {
8989
$fname = ucfirst($functionName);
9090
$args = $this->prepareArgs($fname, $arguments);
91+
$trace = [];
9192

9293
if ($this->eventDispatcher !== null) {
93-
$event = new RequestPreEvent($fname, $args, $options, $inputHeaders, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
94+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
95+
$event = new RequestPreEvent($fname, $args, $options, $inputHeaders, $trace);
9496
$this->eventDispatcher->dispatch($event);
9597
}
9698

@@ -101,7 +103,7 @@ protected function soapCall(
101103

102104
if ($this->eventDispatcher !== null) {
103105
$duration = microtime(true) - $requestStart;
104-
$event = new RequestPostEvent($fname, $args, $soapResponse, $duration);
106+
$event = new RequestPostEvent($fname, $args, $soapResponse, $duration, $trace);
105107
$this->eventDispatcher->dispatch($event);
106108
}
107109

@@ -110,7 +112,7 @@ protected function soapCall(
110112

111113
if ($this->eventDispatcher !== null) {
112114
$duration = microtime(true) - $requestStart;
113-
$event = new RequestFailEvent($fname, $args, $t, $duration);
115+
$event = new RequestFailEvent($fname, $args, $t, $duration, $trace);
114116
$this->eventDispatcher->dispatch($event);
115117
}
116118

src/Wsdl/WebServiceFactory.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ public function createWebService(string $url, array $options): WebServiceInterfa
4747
$soapClient = new \SoapClient($url, $options);
4848
return new $this->class($soapClient, $options, $this->eventDispatcher);
4949
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
55+
{
56+
if ($this->eventDispatcher != null) {
57+
throw new InvalidArgumentException("Event dispatcher is already set.");
58+
}
59+
60+
$this->eventDispatcher = $eventDispatcher;
61+
}
5062
}

src/Wsdl/WebServiceFactoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Skaut\Skautis\Wsdl;
55

6+
use Psr\EventDispatcher\EventDispatcherInterface;
7+
68
/**
79
* Interface továrny pro vytváření objektů webových služeb
810
*/
@@ -18,4 +20,12 @@ interface WebServiceFactoryInterface
1820
* @return WebServiceInterface
1921
*/
2022
public function createWebService(string $url, array $options): WebServiceInterface;
23+
24+
/**
25+
* Nastaví event dispatcher, pokud uz neni nastaven.
26+
*
27+
* @param EventDispatcherInterface $eventDispatcher
28+
* @return void
29+
*/
30+
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void;
2131
}

src/Wsdl/WsdlManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Skaut\Skautis\Wsdl;
55

6+
use Psr\EventDispatcher\EventDispatcherInterface;
67
use Skaut\Skautis\Config;
78
use Skaut\Skautis\User;
89

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

79+
/**
80+
* Nastaví event dispatcher.
81+
*/
82+
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void {
83+
$this->webServiceFactory->setEventDispatcher($eventDispatcher);
84+
}
85+
7886
/**
7987
* Vrací URL webové služby podle jejího jména
8088
*/

tests/Unit/Wsdl/Event/RequestFailEventTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class RequestFailEventTest extends TestCase
1717
public function testExceptionMessage(): void
1818
{
1919
$throwable = new RuntimeException('my message');
20+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
2021

21-
$event = new RequestFailEvent('asd', [], $throwable, 30);
22+
$event = new RequestFailEvent('asd', [], $throwable, 30, $trace);
2223
$this->assertStringContainsString('my message', $event->getExceptionString());
2324
$this->assertSame(RuntimeException::class, $event->getExceptionClass());
2425
}
@@ -31,8 +32,9 @@ public function testDeserialization(): void
3132
'argument' => 'value',
3233
],
3334
];
35+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
3436

35-
$event = new RequestFailEvent('asd', $args, $throwable, 30.22);
37+
$event = new RequestFailEvent('asd', $args, $throwable, 30.22, $trace);
3638

3739
$serialized = serialize($event);
3840
/** @var RequestFailEvent $unserialized */
@@ -56,8 +58,9 @@ public function testRepeatedSerializationDeserialization(): void
5658
'argument' => 'value',
5759
],
5860
];
61+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
5962

60-
$event = new RequestFailEvent('asd', $args, $throwable, 30.22);
63+
$event = new RequestFailEvent('asd', $args, $throwable, 30.22, $trace);
6164

6265
$serialized = serialize($event);
6366
$unserialized = unserialize($serialized);

tests/Unit/Wsdl/Event/RequestPostEventTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public function testDeserialize(): void
2121
];
2222
$result = [(object)['a' => 'b']];
2323
$duration = 11.11;
24+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
2425

25-
$event = new RequestPostEvent('asd', $args, $result, $duration);
26+
$event = new RequestPostEvent('asd', $args, $result, $duration, $trace);
2627

2728
$serialized = serialize($event);
2829
/** @var RequestPostEvent $unserialized */

0 commit comments

Comments
 (0)