-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTask.php
More file actions
125 lines (104 loc) · 4.21 KB
/
RequestTask.php
File metadata and controls
125 lines (104 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/SoapProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\SoapProcessBundle\Task;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\SoapProcessBundle\Registry\ClientRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @phpstan-type RequestOptions array{
* 'client': string,
* 'method': string,
* 'soap_call_options': array<mixed>|null,
* 'soap_call_headers': array<\SoapHeader>|null,
* }
*/
class RequestTask extends AbstractConfigurableTask
{
public function __construct(protected LoggerInterface $logger, protected ClientRegistry $registry)
{
}
public function execute(ProcessState $state): void
{
/** @var RequestOptions $options */
$options = $this->getOptions($state);
$client = $this->registry->getClient($options['client']);
/** @var array<mixed> $input */
$input = $state->getInput() ?: [];
/** @var array<mixed>|null $soapCallOptions */
$soapCallOptions = $this->getOption($state, 'soap_call_options');
$client->setSoapOptions($soapCallOptions);
/** @var array<\SoapHeader>|null $soapCallHeaders */
$soapCallHeaders = $this->getOption($state, 'soap_call_headers');
$client->setSoapHeaders($soapCallHeaders);
$result = $client->call($options['method'], $input);
// Handle empty results
if (false === $result) {
$logContext = [
'options' => $options,
'last_request' => $client->getLastRequest(),
'last_request_headers' => $client->getLastRequestHeaders(),
'last_response' => $client->getLastResponse(),
'last_response_headers' => $client->getLastResponseHeaders(),
];
$state->setErrorOutput($result);
$this->logger->error('Empty resultset for query', $logContext);
if (TaskConfiguration::STRATEGY_SKIP === $state->getTaskConfiguration()->getErrorStrategy()) {
$state->setSkipped(true);
} elseif (TaskConfiguration::STRATEGY_STOP === $state->getTaskConfiguration()->getErrorStrategy()) {
$state->setStopped(true);
}
}
$state->setOutput($result);
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(
[
'client',
'method',
]
);
$resolver->setDefaults(
[
'soap_call_options' => null,
'soap_call_headers' => null,
]
);
$resolver->setAllowedTypes('client', ['string']);
$resolver->setAllowedTypes('method', ['string']);
$resolver->setAllowedTypes('soap_call_options', ['array', 'null']);
$resolver->setAllowedTypes('soap_call_headers', ['array', 'null']);
$resolver->setNormalizer('soap_call_headers', function (Options $options, $headers) {
if (null === $headers) {
return null;
}
$headerResolver = new OptionsResolver();
$this->configureSoapCallHeaderOption($headerResolver);
$resolvedHeaders = [];
/** @var array<string, array<mixed>> $headers */
foreach ($headers as $name => $header) {
/** @var array{'namespace': string, 'data': array<mixed>} $resolvedHeader */
$resolvedHeader = $headerResolver->resolve($header);
$resolvedHeaders[] = new \SoapHeader($resolvedHeader['namespace'], $name, $resolvedHeader['data']);
}
return $resolvedHeaders;
});
}
protected function configureSoapCallHeaderOption(OptionsResolver $resolver): void
{
$resolver->setRequired('namespace');
$resolver->setRequired('data');
}
}