SoapSerializer::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 32
ccs 20
cts 22
cp 0.9091
rs 9.584
cc 3
nc 4
nop 4
crap 3.0067
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
30
class SoapSerializer implements SerializerInterface
31
{
32
    /** @var Serializer $serializer */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
33
    protected $serializer;
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $client should have a doc-comment as per coding-style.
Loading history...
36
     * SoapSerializer constructor.
37
     *
38
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Doc comment for parameter $config does not match actual variable name $client
Loading history...
39
     * @param CacheInterface|null $metadataCache
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $metadataCache does not match actual variable name $config
Loading history...
40
     * @param array|null $metadataDirs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Doc comment for parameter $metadataDirs does not match actual variable name $metadataCache
Loading history...
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
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
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
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
80
     * @param string $format
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 20 spaces after parameter type; 1 found
Loading history...
81
     * @param SerializationContext|null $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     * @param string|null $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
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
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
96
     * @param string $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
97
     * @param string $format
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 22 spaces after parameter type; 1 found
Loading history...
98
     * @param DeserializationContext|null $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
Coding Style introduced by
Missing parameter comment
Loading history...
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