ClientBuilder::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace DMT\VatServiceEu;
4
5
use DMT\CommandBus\Validator\ValidationMiddleware;
6
use DMT\Http\Client\RequestHandler;
7
use DMT\Soap\Serializer\SoapDateHandler;
8
use DMT\Soap\Serializer\SoapDeserializationVisitorFactory;
9
use DMT\Soap\Serializer\SoapMessageEventSubscriber;
10
use DMT\Soap\Serializer\SoapSerializationVisitorFactory;
11
use DMT\VatServiceEu\Handler\CheckVatHandler;
12
use JMS\Serializer\EventDispatcher\EventDispatcher;
13
use JMS\Serializer\Handler\HandlerRegistry;
14
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
15
use JMS\Serializer\SerializerBuilder;
16
use JMS\Serializer\SerializerInterface;
17
use League\Tactician\CommandBus;
18
use League\Tactician\Handler\CommandHandlerMiddleware;
19
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
20
use League\Tactician\Handler\Locator\CallableLocator;
21
use League\Tactician\Handler\MethodNameInflector\HandleClassNameWithoutSuffixInflector;
22
use League\Tactician\Plugins\LockingMiddleware;
23
use Psr\Http\Client\ClientInterface;
24
use Psr\Http\Message\RequestFactoryInterface;
25
26
class ClientBuilder
27
{
28
    private ClientInterface $client;
29
    private RequestFactoryInterface $requestFactory;
30
31
    /**
32
     * Create the client builder
33
     *
34
     * @param ClientInterface $client
35
     * @param RequestFactoryInterface $requestFactory
36
     * @return ClientBuilder
37
     */
38 27
    public static function create(ClientInterface $client, RequestFactoryInterface $requestFactory): ClientBuilder
39
    {
40 27
        return (new static())
41 27
            ->setClient($client)
42 27
            ->setRequestFactory($requestFactory);
43
    }
44
45
    /**
46
     * @param ClientInterface $client
47
     * @return ClientBuilder
48
     */
49 27
    public function setClient(ClientInterface $client): self
50
    {
51 27
        $this->client = $client;
52
53 27
        return $this;
54
    }
55
56
    /**
57
     * @param RequestFactoryInterface $requestFactory
58
     * @return ClientBuilder
59
     */
60 27
    public function setRequestFactory(RequestFactoryInterface $requestFactory): self
61
    {
62 27
        $this->requestFactory = $requestFactory;
63
64 27
        return $this;
65
    }
66
67
    /**
68
     * Build the client.
69
     *
70
     * @return Client
71
     */
72 1
    public function build(): Client
73
    {
74 1
        return new Client(
75 1
            new CommandBus([
76 1
                new LockingMiddleware(),
77 1
                new ValidationMiddleware(),
78 1
                new CommandHandlerMiddleware(
79 1
                    new ClassNameExtractor(),
80 1
                    new CallableLocator([$this, 'getCheckVatHandler']),
81 1
                    new HandleClassNameWithoutSuffixInflector('Request')
82 1
                )
83 1
            ])
84 1
        );
85
    }
86
87
    /**
88
     * Get the handler that handles the service requests.
89
     *
90
     * @return CheckVatHandler
91
     */
92 1
    public function getCheckVatHandler(): CheckVatHandler
93
    {
94 1
        return new CheckVatHandler(new RequestHandler($this->client), $this->getSerializer(), $this->requestFactory);
95
    }
96
97
    /**
98
     * Get the soap serializer.
99
     *
100
     * @return SerializerInterface
101
     */
102 26
    public function getSerializer(): SerializerInterface
103
    {
104 26
        return
105 26
            SerializerBuilder::create()
106 26
                ->setSerializationVisitor('soap', new SoapSerializationVisitorFactory())
107 26
                ->setDeserializationVisitor('soap', new SoapDeserializationVisitorFactory())
108 26
                ->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())
109 26
                ->configureListeners(
110 26
                    function (EventDispatcher $dispatcher) {
111 26
                        $dispatcher->addSubscriber(
112 26
                            new SoapMessageEventSubscriber()
113 26
                        );
114 26
                    }
115 26
                )
116 26
                ->configureHandlers(function (HandlerRegistry $registry) {
117 26
                    $registry->registerSubscribingHandler(new SoapDateHandler());
118 26
                })
119 26
                ->build()
120 26
            ;
121
    }
122
}
123