1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
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 | */ |
||
0 ignored issues
–
show
|
|||
30 | class SoapSerializer implements SerializerInterface |
||
31 | { |
||
32 | /** @var Serializer $serializer */ |
||
0 ignored issues
–
show
|
|||
33 | protected $serializer; |
||
34 | |||
35 | /** |
||
0 ignored issues
–
show
|
|||
36 | * SoapSerializer constructor. |
||
37 | * |
||
38 | * @param Config $config |
||
0 ignored issues
–
show
|
|||
39 | * @param CacheInterface|null $metadataCache |
||
0 ignored issues
–
show
|
|||
40 | * @param array|null $metadataDirs |
||
0 ignored issues
–
show
|
|||
41 | */ |
||
42 | 12 | public function __construct(Client $client, Config $config, CacheInterface $metadataCache = null, array $metadataDirs = null) |
|
43 | { |
||
44 | 12 | $builder = new SerializerBuilder(); |
|
45 | |||
46 | 12 | if ($metadataCache) { |
|
47 | $builder->setMetadataCache($metadataCache); |
||
48 | } |
||
49 | |||
50 | 12 | if ($metadataDirs) { |
|
51 | $builder->setMetadataDirs($metadataDirs); |
||
52 | } |
||
53 | |||
54 | 12 | $this->serializer = $builder |
|
55 | 12 | ->setSerializationVisitor('soap', (new SoapSerializationVisitorFactory())->setSoapVersion(SoapNamespaceInterface::SOAP_1_2)) |
|
56 | 12 | ->setDeserializationVisitor('soap', new SoapDeserializationVisitorFactory()) |
|
57 | 12 | ->setObjectConstructor(new ClientModelInstantiator($client)) |
|
58 | 12 | ->configureHandlers( |
|
59 | function(HandlerRegistry $registry) { |
||
0 ignored issues
–
show
|
|||
60 | 12 | $registry->registerSubscribingHandler(new SoapDateHandler()); |
|
61 | 12 | } |
|
62 | ) |
||
63 | 12 | ->configureListeners( |
|
64 | function (EventDispatcher $dispatcher) use ($config) { |
||
65 | 12 | $dispatcher->addSubscriber(new SoapExceptionEventSubscriber()); |
|
66 | 12 | $dispatcher->addSubscriber(new SoapDateTimeSanitizerEventSubscriber()); |
|
67 | 12 | $dispatcher->addSubscriber(new SoapMessageEventSubscriber()); |
|
68 | 12 | $dispatcher->addSubscriber(new SoapHeaderEventSubscriber($this->getSoapHeader($config))); |
|
69 | 12 | $dispatcher->addSubscriber(new SoapAddressingEventSubscriber()); |
|
70 | 12 | $dispatcher->addSubscriber(new AddIdentificationUriEventSubscriber($config->documentUri)); |
|
71 | 12 | } |
|
72 | ) |
||
73 | 12 | ->build(); |
|
74 | 12 | } |
|
75 | |||
76 | /** |
||
77 | * Serialize client request. |
||
78 | * |
||
79 | * @param Request $data |
||
0 ignored issues
–
show
|
|||
80 | * @param string $format |
||
0 ignored issues
–
show
|
|||
81 | * @param SerializationContext|null $context |
||
0 ignored issues
–
show
|
|||
82 | * @param string|null $type |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
96 | * @param string $type |
||
0 ignored issues
–
show
|
|||
97 | * @param string $format |
||
0 ignored issues
–
show
|
|||
98 | * @param DeserializationContext|null $context |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
112 | * |
||
113 | * @return SoapHeaderInterface |
||
114 | */ |
||
115 | 12 | protected function getSoapHeader(Config $config): SoapHeaderInterface |
|
116 | { |
||
117 | 12 | $header = new Authorization\Security(); |
|
118 | 12 | $header->mustUnderstand = 1; |
|
119 | 12 | $header->usernameToken = new Authorization\UsernameToken(); |
|
120 | 12 | $header->usernameToken->username = new Authorization\Username($config->user); |
|
121 | 12 | $header->usernameToken->password = new Authorization\Password( |
|
122 | 12 | $config->password, |
|
123 | 12 | 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText' |
|
124 | ); |
||
125 | |||
126 | 12 | return $header; |
|
127 | } |
||
128 | } |
||
129 |