|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace DMT\Insolvency\Soap\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use DMT\Insolvency\Client; |
|
6
|
|
|
use DMT\Insolvency\Config; |
|
7
|
|
|
use DMT\Insolvency\Soap\Authorization; |
|
8
|
|
|
use DMT\Insolvency\Soap\Request; |
|
9
|
|
|
use DMT\Insolvency\Soap\Response; |
|
10
|
|
|
use DMT\Soap\Serializer\SoapDateHandler; |
|
11
|
|
|
use DMT\Soap\Serializer\SoapDeserializationVisitorFactory; |
|
12
|
|
|
use DMT\Soap\Serializer\SoapHeaderEventSubscriber; |
|
13
|
|
|
use DMT\Soap\Serializer\SoapHeaderInterface; |
|
14
|
|
|
use DMT\Soap\Serializer\SoapMessageEventSubscriber; |
|
15
|
|
|
use DMT\Soap\Serializer\SoapNamespaceInterface; |
|
16
|
|
|
use DMT\Soap\Serializer\SoapSerializationVisitorFactory; |
|
17
|
|
|
use JMS\Serializer\DeserializationContext; |
|
18
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
|
19
|
|
|
use JMS\Serializer\Exception\Exception; |
|
20
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
|
21
|
|
|
use JMS\Serializer\SerializationContext; |
|
22
|
|
|
use JMS\Serializer\Serializer; |
|
23
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
24
|
|
|
use JMS\Serializer\SerializerInterface; |
|
25
|
|
|
use Metadata\Cache\CacheInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class SoapSerializer |
|
29
|
|
|
*/ |
|
|
|
|
|
|
30
|
|
|
class SoapSerializer implements SerializerInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var Serializer $serializer */ |
|
|
|
|
|
|
33
|
|
|
protected $serializer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* SoapSerializer constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Config $config |
|
|
|
|
|
|
39
|
|
|
* @param CacheInterface|null $metadataCache |
|
|
|
|
|
|
40
|
|
|
* @param array|null $metadataDirs |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
13 |
|
public function __construct(Client $client, Config $config, CacheInterface $metadataCache = null, array $metadataDirs = null) |
|
43
|
|
|
{ |
|
44
|
13 |
|
$builder = new SerializerBuilder(); |
|
45
|
|
|
|
|
46
|
13 |
|
if ($metadataCache) { |
|
47
|
|
|
$builder->setMetadataCache($metadataCache); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
13 |
|
if ($metadataDirs) { |
|
51
|
|
|
$builder->setMetadataDirs($metadataDirs); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
13 |
|
$this->serializer = $builder |
|
55
|
13 |
|
->setSerializationVisitor('soap', (new SoapSerializationVisitorFactory())->setSoapVersion(SoapNamespaceInterface::SOAP_1_2)) |
|
56
|
13 |
|
->setDeserializationVisitor('soap', new SoapDeserializationVisitorFactory()) |
|
57
|
13 |
|
->setObjectConstructor(new ClientModelInstantiator($client)) |
|
58
|
13 |
|
->configureHandlers( |
|
59
|
|
|
function(HandlerRegistry $registry) { |
|
|
|
|
|
|
60
|
13 |
|
$registry->registerSubscribingHandler(new SoapDateHandler()); |
|
61
|
13 |
|
} |
|
62
|
|
|
) |
|
63
|
13 |
|
->configureListeners( |
|
64
|
|
|
function (EventDispatcher $dispatcher) use ($config) { |
|
65
|
13 |
|
$dispatcher->addSubscriber(new SoapExceptionEventSubscriber()); |
|
66
|
13 |
|
$dispatcher->addSubscriber(new SoapDateTimeSanitizerEventSubscriber()); |
|
67
|
13 |
|
$dispatcher->addSubscriber(new SoapMessageEventSubscriber()); |
|
68
|
13 |
|
$dispatcher->addSubscriber(new SoapHeaderEventSubscriber($this->getSoapHeader($config))); |
|
69
|
13 |
|
$dispatcher->addSubscriber(new SoapAddressingEventSubscriber()); |
|
70
|
13 |
|
$dispatcher->addSubscriber(new AddIdentificationUriEventSubscriber($config)); |
|
71
|
13 |
|
} |
|
72
|
|
|
) |
|
73
|
13 |
|
->build(); |
|
74
|
13 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Serialize client request. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Request $data |
|
|
|
|
|
|
80
|
|
|
* @param string $format |
|
|
|
|
|
|
81
|
|
|
* @param SerializationContext|null $context |
|
|
|
|
|
|
82
|
|
|
* @param string|null $type |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @throws Exception |
|
86
|
|
|
*/ |
|
87
|
11 |
|
public function serialize($data, string $format, ?SerializationContext $context = null, ?string $type = null): string |
|
88
|
|
|
{ |
|
89
|
11 |
|
return $this->serializer->serialize($data, $format, $context, $type); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Deserialize client response. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $data |
|
|
|
|
|
|
96
|
|
|
* @param string $type |
|
|
|
|
|
|
97
|
|
|
* @param string $format |
|
|
|
|
|
|
98
|
|
|
* @param DeserializationContext|null $context |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return Response |
|
101
|
|
|
* @throws Exception |
|
102
|
|
|
*/ |
|
103
|
11 |
|
public function deserialize(string $data, string $type, string $format, ?DeserializationContext $context = null): Response |
|
104
|
|
|
{ |
|
105
|
11 |
|
return $this->serializer->deserialize($data, $type, $format, $context); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get SOAP security header. |
|
110
|
|
|
* |
|
111
|
|
|
* @param Config $config |
|
|
|
|
|
|
112
|
|
|
* |
|
113
|
|
|
* @return SoapHeaderInterface |
|
114
|
|
|
*/ |
|
115
|
13 |
|
protected function getSoapHeader(Config $config): SoapHeaderInterface |
|
116
|
|
|
{ |
|
117
|
13 |
|
$header = new Authorization\Security(); |
|
118
|
13 |
|
$header->mustUnderstand = 1; |
|
119
|
13 |
|
$header->usernameToken = new Authorization\UsernameToken(); |
|
120
|
13 |
|
$header->usernameToken->username = new Authorization\Username($config->user); |
|
121
|
13 |
|
$header->usernameToken->password = new Authorization\Password( |
|
122
|
13 |
|
$config->password, |
|
123
|
13 |
|
'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText' |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
13 |
|
return $header; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|