|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DMT\WebservicesNl\Client\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use DMT\Soap\Serializer\SoapDateHandler; |
|
6
|
|
|
use DMT\Soap\Serializer\SoapDeserializationVisitor; |
|
7
|
|
|
use DMT\Soap\Serializer\SoapHeaderEventSubscriber; |
|
8
|
|
|
use DMT\Soap\Serializer\SoapHeaderInterface; |
|
9
|
|
|
use DMT\Soap\Serializer\SoapSerializationVisitor; |
|
10
|
|
|
use DMT\WebservicesNl\Client\Command\Handler\Locator\CommandHandlerResolver; |
|
11
|
|
|
use DMT\WebservicesNl\Client\Soap\Authorization\HeaderAuthenticate; |
|
12
|
|
|
use DMT\WebservicesNl\Client\Soap\Authorization\HeaderLogin; |
|
13
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
14
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
|
15
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
|
16
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
17
|
|
|
use Metadata\Cache\FileCache; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ClientBuilder |
|
21
|
|
|
* |
|
22
|
|
|
* @package DMT\WebservicesNl\Client |
|
23
|
|
|
*/ |
|
24
|
|
|
class SoapClientBuilder extends AbstractClientBuilder |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SoapHeaderInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $authentication; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $credentials |
|
33
|
|
|
* |
|
34
|
|
|
* @return SoapClientBuilder |
|
35
|
|
|
* @throws \InvalidArgumentException |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function setAuthentication(array $credentials): AbstractClientBuilder |
|
38
|
|
|
{ |
|
39
|
6 |
|
if (array_key_exists('session_id', $credentials)) { |
|
40
|
1 |
|
$this->authentication = new HeaderAuthenticate($credentials['session_id']); |
|
41
|
5 |
|
} elseif (array_key_exists('username', $credentials) && array_key_exists('password', $credentials)) { |
|
42
|
3 |
|
$this->authentication = new HeaderLogin($credentials['username'], $credentials['password']); |
|
43
|
|
|
} else { |
|
44
|
2 |
|
throw new \InvalidArgumentException('No credentials given.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return CommandHandlerResolver |
|
52
|
|
|
*/ |
|
53
|
2 |
|
protected function getCommandResolver(): CommandHandlerResolver |
|
54
|
|
|
{ |
|
55
|
2 |
|
$httpClient = new HttpClient( |
|
56
|
|
|
[ |
|
57
|
2 |
|
'base_uri' => $this->endpoint, |
|
58
|
|
|
'http_errors' => false, |
|
59
|
|
|
'headers' => [ |
|
60
|
|
|
'Content-Type' => 'text/xml; charset=utf-8', |
|
61
|
|
|
] |
|
62
|
|
|
] |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
2 |
|
$serializer = SerializerBuilder::create() |
|
66
|
2 |
|
->configureListeners( |
|
67
|
|
|
function (EventDispatcher $dispatcher) { |
|
68
|
2 |
|
$dispatcher->addSubscriber(new SoapHeaderEventSubscriber($this->authentication)); |
|
69
|
2 |
|
} |
|
70
|
|
|
) |
|
71
|
2 |
|
->configureHandlers( |
|
72
|
|
|
function(HandlerRegistry $registry) { |
|
73
|
2 |
|
$registry->registerSubscribingHandler(new SoapDateHandler()); |
|
74
|
2 |
|
} |
|
75
|
|
|
) |
|
76
|
2 |
|
->setSerializationVisitor('soap', new SoapSerializationVisitor($this->namingStrategy)) |
|
77
|
2 |
|
->setDeserializationVisitor('soap', new SoapDeserializationVisitor($this->namingStrategy)) |
|
78
|
2 |
|
->setMetadataCache(new FileCache(dirname(__DIR__, 2) . '/cache/soap/')) |
|
79
|
2 |
|
->build(); |
|
80
|
|
|
|
|
81
|
2 |
|
return new CommandHandlerResolver($httpClient, $serializer, $this->serializerFormat); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|