Skip to content

Commit 4381775

Browse files
Merge pull request #1 from shavounet/v3.0-dev
Added the possibility to configure Soap Headers for call
2 parents 55ab3de + 8455f36 commit 4381775

File tree

3 files changed

+126
-13
lines changed

3 files changed

+126
-13
lines changed

Client/Client.php

+47-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class Client implements ClientInterface
2828
/** @var array */
2929
private $options;
3030

31+
/** @var array */
32+
private $soapOptions;
33+
34+
/** @var null|\SoapHeader[] */
35+
private $soapHeaders;
36+
3137
/** @var LoggerInterface */
3238
private $logger;
3339

@@ -115,6 +121,38 @@ public function setOptions(array $options): void
115121
$this->options = $options;
116122
}
117123

124+
/**
125+
* @return array|null
126+
*/
127+
public function getSoapOptions(): ?array
128+
{
129+
return $this->soapOptions;
130+
}
131+
132+
/**
133+
* @param array|null $soapOptions
134+
*/
135+
public function setSoapOptions(array $soapOptions = null): void
136+
{
137+
$this->soapOptions = $soapOptions;
138+
}
139+
140+
/**
141+
* @return \SoapHeader[]|null
142+
*/
143+
public function getSoapHeaders(): ?array
144+
{
145+
return $this->soapHeaders;
146+
}
147+
148+
/**
149+
* @param \SoapHeader[]|null $soapHeaders
150+
*/
151+
public function setSoapHeaders(array $soapHeaders = null): void
152+
{
153+
$this->soapHeaders = $soapHeaders;
154+
}
155+
118156
/**
119157
* @return \SoapClient|null
120158
*/
@@ -134,63 +172,63 @@ public function setSoapClient(\SoapClient $soapClient): void
134172
/**
135173
* @return string
136174
*/
137-
public function getLastRequest(): string
175+
public function getLastRequest(): ?string
138176
{
139177
return $this->lastRequest;
140178
}
141179

142180
/**
143181
* @param string $lastRequest
144182
*/
145-
public function setLastRequest(string $lastRequest): void
183+
public function setLastRequest(?string $lastRequest): void
146184
{
147185
$this->lastRequest = $lastRequest;
148186
}
149187

150188
/**
151189
* @return string
152190
*/
153-
public function getLastRequestHeaders(): string
191+
public function getLastRequestHeaders(): ?string
154192
{
155193
return $this->lastRequestHeaders;
156194
}
157195

158196
/**
159197
* @param string $lastRequestHeaders
160198
*/
161-
public function setLastRequestHeaders(string $lastRequestHeaders): void
199+
public function setLastRequestHeaders(?string $lastRequestHeaders): void
162200
{
163201
$this->lastRequestHeaders = $lastRequestHeaders;
164202
}
165203

166204
/**
167205
* @return string
168206
*/
169-
public function getLastResponse(): string
207+
public function getLastResponse(): ?string
170208
{
171209
return $this->lastResponse;
172210
}
173211

174212
/**
175213
* @param string $lastResponse
176214
*/
177-
public function setLastResponse(string $lastResponse): void
215+
public function setLastResponse(?string $lastResponse): void
178216
{
179217
$this->lastResponse = $lastResponse;
180218
}
181219

182220
/**
183221
* @return string
184222
*/
185-
public function getLastResponseHeaders(): string
223+
public function getLastResponseHeaders(): ?string
186224
{
187225
return $this->lastResponseHeaders;
188226
}
189227

190228
/**
191229
* @param string $lastResponseHeaders
192230
*/
193-
public function setLastResponseHeaders(string $lastResponseHeaders): void
231+
public function setLastResponseHeaders(?string $lastResponseHeaders): void
194232
{
195233
$this->lastResponseHeaders = $lastResponseHeaders;
196234
}
@@ -226,7 +264,7 @@ protected function doSoapCall(string $method, array $input = [])
226264
throw new \InvalidArgumentException('Soap client is not initialized');
227265
}
228266
try {
229-
$result = $this->getSoapClient()->__soapCall($method, [$input]);
267+
$result = $this->getSoapClient()->__soapCall($method, $input, $this->getSoapOptions(), $this->getSoapHeaders());
230268
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (\SoapFault $e) {
231269
$this->getLastRequestTrace();
232270
$this->getLogger()->alert(

Client/ClientInterface.php

+44-4
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,65 @@ public function getOptions(): array;
6060
*/
6161
public function setOptions(array $options): void;
6262

63+
/**
64+
* Return the Soap call options
65+
*
66+
* @see https://www.php.net/manual/en/soapclient.soapcall.php
67+
*
68+
* @return array|null
69+
*/
70+
public function getSoapOptions(): ?array;
71+
72+
/**
73+
* Set the Soap call options
74+
*
75+
* @see https://www.php.net/manual/en/soapclient.soapcall.php
76+
*
77+
* @param array|null $options
78+
*
79+
* @return void
80+
*/
81+
public function setSoapOptions(array $options = null): void;
82+
83+
/**
84+
* Return the Soap call headers
85+
*
86+
* @see https://www.php.net/manual/en/soapclient.soapcall.php
87+
*
88+
* @return \SoapHeader[]|null
89+
*/
90+
public function getSoapHeaders(): ?array;
91+
92+
/**
93+
* Set the Soap call headers
94+
*
95+
* @see https://www.php.net/manual/en/soapclient.soapcall.php
96+
*
97+
* @param \SoapHeader[]|null $headers
98+
*
99+
* @return void
100+
*/
101+
public function setSoapHeaders(array $headers = null): void;
102+
63103
/**
64104
* @return string
65105
*/
66-
public function getLastRequest(): string;
106+
public function getLastRequest(): ?string;
67107

68108
/**
69109
* @return string
70110
*/
71-
public function getLastRequestHeaders(): string;
111+
public function getLastRequestHeaders(): ?string;
72112

73113
/**
74114
* @return string
75115
*/
76-
public function getLastResponse(): string;
116+
public function getLastResponse(): ?string;
77117

78118
/**
79119
* @return string
80120
*/
81-
public function getLastResponseHeaders(): string;
121+
public function getLastResponseHeaders(): ?string;
82122

83123
/**
84124
* Call Soap method

Task/RequestTask.php

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CleverAge\ProcessBundle\Model\ProcessState;
1616
use CleverAge\SoapProcessBundle\Registry\ClientRegistry;
1717
use Psr\Log\LoggerInterface;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
/**
@@ -54,6 +55,9 @@ public function execute(ProcessState $state): void
5455

5556
$input = $state->getInput() ?: [];
5657

58+
$client->setSoapOptions($this->getOption($state, 'soap_call_options'));
59+
$client->setSoapHeaders($this->getOption($state, 'soap_call_headers'));
60+
5761
$result = $client->call($options['method'], $input);
5862

5963
// Handle empty results
@@ -91,7 +95,38 @@ protected function configureOptions(OptionsResolver $resolver): void
9195
'method',
9296
]
9397
);
98+
$resolver->setDefaults(
99+
[
100+
'soap_call_options' => null,
101+
'soap_call_headers' => null,
102+
]
103+
);
94104
$resolver->setAllowedTypes('client', ['string']);
95105
$resolver->setAllowedTypes('method', ['string']);
106+
$resolver->setAllowedTypes('soap_call_options', ['array', 'null']);
107+
$resolver->setAllowedTypes('soap_call_headers', ['array', 'null']);
108+
109+
$resolver->setNormalizer('soap_call_headers', function (Options $options, $headers) {
110+
if ($headers === null) {
111+
return null;
112+
}
113+
114+
$headerResolver = new OptionsResolver();
115+
$this->configureSoapCallHeaderOption($headerResolver);
116+
117+
$resolvedHeaders = [];
118+
foreach ($headers as $name => $header) {
119+
$resolvedHeader = $headerResolver->resolve($header);
120+
$resolvedHeaders[] = new \SoapHeader($resolvedHeader['namespace'], $name, $resolvedHeader['data']);
121+
}
122+
123+
return $resolvedHeaders;
124+
});
125+
}
126+
127+
protected function configureSoapCallHeaderOption(OptionsResolver $resolver)
128+
{
129+
$resolver->setRequired('namespace');
130+
$resolver->setRequired('data');
96131
}
97132
}

0 commit comments

Comments
 (0)