1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebservicesNl\Protocol\Soap\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use WebservicesNl\Common\Endpoint\Manager; |
7
|
|
|
use WebservicesNl\Common\Exception\Client\InputException; |
8
|
|
|
use WebservicesNl\Common\Exception\Server\NoServerAvailableException; |
9
|
|
|
use WebservicesNl\Connector\Client\AbstractClientFactory; |
10
|
|
|
use WebservicesNl\Platform\PlatformConfigInterface; |
11
|
|
|
use WebservicesNl\Protocol\Soap\Config\ConfigFactory as SoapConfigFactory; |
12
|
|
|
use WebservicesNl\Protocol\Soap\Helper\GuzzleClientFactory; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SoapFactory. |
16
|
|
|
* |
17
|
|
|
* Managing Factory for creating SoapClient for given platform (mainly WebservicesNl). |
18
|
|
|
*/ |
19
|
|
|
class SoapFactory extends AbstractClientFactory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var SoapConfig |
23
|
|
|
*/ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SoapBuilder constructor. |
28
|
|
|
* Converts a Platform config into a SoapConfig. |
29
|
|
|
* |
30
|
|
|
* @param PlatformConfigInterface $platformConfig |
31
|
|
|
* |
32
|
|
|
* @throws InputException |
33
|
|
|
*/ |
34
|
8 |
|
public function __construct(PlatformConfigInterface $platformConfig) |
35
|
|
|
{ |
36
|
8 |
|
$this->config = SoapConfigFactory::config($platformConfig); |
37
|
8 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Static function (LSB) for building this class. |
41
|
|
|
* |
42
|
|
|
* @param PlatformConfigInterface $platformConfig |
43
|
|
|
* |
44
|
|
|
* @throws InputException |
45
|
|
|
* @return static |
46
|
|
|
*/ |
47
|
8 |
|
public static function build(PlatformConfigInterface $platformConfig) |
48
|
|
|
{ |
49
|
8 |
|
return new static($platformConfig); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates a SoapClient with configured SoapConfig object and given additional settings. |
54
|
|
|
* |
55
|
|
|
* @param array $settings additional settings go here |
56
|
|
|
* |
57
|
|
|
* @return SoapClient |
58
|
|
|
* @throws InputException |
59
|
|
|
* @throws NoServerAvailableException |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
7 |
|
public function create(array $settings = []) |
63
|
1 |
|
{ |
64
|
|
|
// create soap settings, with given settings and platform settings |
65
|
7 |
|
$soapSettings = SoapSettings::loadFromArray($settings); |
66
|
|
|
|
67
|
|
|
// create a manager for endpoint management |
68
|
7 |
|
$manager = $this->createEndpointManager($settings); |
69
|
|
|
|
70
|
|
|
// replace the native soap client transport with a curl client |
71
|
7 |
|
$curlClient = $this->createCurlClient($soapSettings->toArray(), $manager); |
72
|
|
|
|
73
|
|
|
// build a SoapClient (extends the native soap client) |
74
|
7 |
|
$soapClient = new SoapClient($soapSettings, $manager, $curlClient); |
75
|
|
|
|
76
|
|
|
// set custom headers |
77
|
7 |
|
$soapHeaders = array_key_exists('soapHeaders', $settings) ? |
78
|
7 |
|
array_merge($settings['soapHeaders'], $this->config->getSoapHeaders()) : $this->config->getSoapHeaders(); |
79
|
|
|
|
80
|
7 |
|
$soapClient->__setSoapHeaders($soapHeaders); |
81
|
7 |
|
if ($this->config->hasConverter() === true) { |
82
|
7 |
|
$soapClient->setConverter($this->config->getConverter()); |
83
|
7 |
|
} |
84
|
|
|
|
85
|
7 |
|
if ($this->hasLogger() === true) { |
86
|
3 |
|
$soapClient->setLogger($this->logger); |
87
|
3 |
|
$this->logger->info('Created SoapClient for ' . $this->config->getPlatformConfig()->getPlatformName()); |
88
|
4 |
|
$this->logger->debug('Settings', ['settings' => (array) $settings]); |
89
|
7 |
|
$this->logger->debug('Created SoapClient', ['SoapClient' => print_r($soapClient, true)]); |
90
|
3 |
|
} |
91
|
|
|
|
92
|
7 |
|
return $soapClient; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Configure PSR-7 guzzle client for this soap factory. |
97
|
|
|
* |
98
|
|
|
* @param array $settings settings with extra guzzle settings |
99
|
|
|
* @param Manager $manager endpoint Manager |
100
|
|
|
* |
101
|
|
|
* @return Client |
102
|
|
|
* @throws NoServerAvailableException |
103
|
|
|
*/ |
104
|
7 |
|
private function createCurlClient(array $settings, Manager $manager) |
105
|
|
|
{ |
106
|
7 |
|
$settings['url'] = (string) $manager->getActiveEndpoint()->getUri(); |
107
|
|
|
|
108
|
7 |
|
$factory = GuzzleClientFactory::build($this->config->getPlatformConfig()); |
109
|
7 |
|
if ($this->hasLogger() === true) { |
110
|
3 |
|
$factory->setLogger($this->logger); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
7 |
|
return $factory->create($settings); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates and configures a EndpointManager. |
118
|
|
|
* |
119
|
|
|
* If url key is present in settings array, this url will be set to the active endpoint. |
120
|
|
|
* |
121
|
|
|
* @param array $settings optional settings |
122
|
|
|
* |
123
|
|
|
* @return Manager Endpoint manager |
124
|
|
|
* |
125
|
|
|
* @throws \InvalidArgumentException |
126
|
|
|
* @throws InputException |
127
|
|
|
*/ |
128
|
7 |
|
private function createEndpointManager(array $settings = []) |
129
|
|
|
{ |
130
|
|
|
// get the default end points from config object |
131
|
7 |
|
$endPoints = $this->getConfig()->getEndPoints(); |
132
|
|
|
|
133
|
|
|
// merge defaults urls with custom url if present, custom url is set to active |
134
|
7 |
|
if (array_key_exists('url', $settings) && filter_var($settings['url'], FILTER_VALIDATE_URL) !== false) { |
135
|
1 |
|
array_unshift($endPoints, $settings['url']); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
// Create EndPoint Manager |
139
|
7 |
|
$manager = new Manager(); |
140
|
7 |
|
foreach ($endPoints as $endPoint) { |
141
|
7 |
|
$manager->createEndpoint($endPoint); |
142
|
7 |
|
} |
143
|
|
|
|
144
|
7 |
|
if ($this->hasLogger() === true) { |
145
|
3 |
|
$this->logger->info('Created endpoint manager', ['endpoint count' => $manager->getEndpoints()->count()]); |
146
|
3 |
|
} |
147
|
|
|
|
148
|
7 |
|
return $manager; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return SoapConfig. |
153
|
|
|
* |
154
|
|
|
* @return SoapConfig |
155
|
|
|
*/ |
156
|
7 |
|
public function getConfig() |
157
|
|
|
{ |
158
|
7 |
|
return $this->config; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|