1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient; |
3
|
|
|
|
4
|
|
|
use GoetasWebservices\SoapServices\SoapCommon\Metadata\PhpMetadataGenerator; |
5
|
|
|
use GoetasWebservices\SoapServices\SoapCommon\Metadata\PhpMetadataGeneratorInterface; |
6
|
|
|
use GoetasWebservices\XML\WSDLReader\Exception\PortNotFoundException; |
7
|
|
|
use GoetasWebservices\XML\WSDLReader\Exception\ServiceNotFoundException; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Discovery\HttpClientDiscovery; |
10
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
11
|
|
|
use Http\Message\MessageFactory; |
12
|
|
|
use JMS\Serializer\SerializerInterface; |
13
|
|
|
|
14
|
|
|
class ClientFactory |
15
|
|
|
{ |
16
|
|
|
protected $namespaces = []; |
17
|
|
|
protected $metadata = []; |
18
|
|
|
/** |
19
|
|
|
* @var SerializerInterface |
20
|
|
|
*/ |
21
|
|
|
protected $serializer; |
22
|
|
|
/** |
23
|
|
|
* @var MessageFactory |
24
|
|
|
*/ |
25
|
|
|
protected $messageFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var HttpClient |
29
|
|
|
*/ |
30
|
|
|
protected $httpClient; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var PhpMetadataGeneratorInterface |
34
|
|
|
*/ |
35
|
|
|
private $generator; |
36
|
|
|
|
37
|
|
|
private $unwrap = false; |
38
|
|
|
|
39
|
14 |
|
public function __construct(array $namespaces, SerializerInterface $serializer) |
40
|
|
|
{ |
41
|
14 |
|
$this->setSerializer($serializer); |
42
|
|
|
|
43
|
14 |
|
foreach ($namespaces as $namespace => $phpNamespace) { |
44
|
14 |
|
$this->addNamespace($namespace, $phpNamespace); |
45
|
14 |
|
} |
46
|
14 |
|
} |
47
|
|
|
|
48
|
|
|
public function setUnwrapResponses($unwrap) |
49
|
|
|
{ |
50
|
|
|
$this->unwrap = !!$unwrap; |
51
|
|
|
} |
52
|
|
|
|
53
|
10 |
|
public function setHttpClient(HttpClient $client) |
54
|
|
|
{ |
55
|
10 |
|
$this->httpClient = $client; |
56
|
10 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param MessageFactory $messageFactory |
60
|
|
|
*/ |
61
|
|
|
public function setMessageFactory(MessageFactory $messageFactory) |
62
|
|
|
{ |
63
|
|
|
$this->messageFactory = $messageFactory; |
64
|
|
|
} |
65
|
|
|
|
66
|
14 |
|
public function setSerializer(SerializerInterface $serializer) |
67
|
|
|
{ |
68
|
14 |
|
$this->serializer = $serializer; |
69
|
14 |
|
} |
70
|
|
|
|
71
|
14 |
|
public function setMetadataGenerator(PhpMetadataGeneratorInterface $generator) |
72
|
|
|
{ |
73
|
14 |
|
$this->generator = $generator; |
74
|
14 |
|
} |
75
|
|
|
|
76
|
14 |
|
private function getSoapService($wsdl, $portName = null, $serviceName = null) |
77
|
|
|
{ |
78
|
14 |
|
$generator = $this->generator ?: new PhpMetadataGenerator(); |
79
|
|
|
|
80
|
14 |
|
foreach ($this->namespaces as $ns => $phpNs) { |
81
|
14 |
|
$generator->addNamespace($ns, $phpNs); |
82
|
14 |
|
} |
83
|
|
|
|
84
|
14 |
|
$services = $generator->generateServices($wsdl); |
85
|
14 |
|
$service = $this->getService($serviceName, $services); |
86
|
|
|
|
87
|
14 |
|
return $this->getPort($portName, $service); |
88
|
|
|
} |
89
|
|
|
|
90
|
14 |
|
public function addNamespace($uri, $phpNs) |
91
|
|
|
{ |
92
|
14 |
|
$this->namespaces[$uri] = $phpNs; |
93
|
14 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $wsdl |
97
|
|
|
* @param null|string $portName |
98
|
|
|
* @param null|string $serviceName |
99
|
|
|
* @param null|bool $unwrap |
100
|
|
|
* @return Client |
101
|
|
|
*/ |
102
|
14 |
|
public function getClient($wsdl, $portName = null, $serviceName = null, $unwrap = null) |
103
|
|
|
{ |
104
|
14 |
|
$service = $this->getSoapService($wsdl, $portName, $serviceName); |
105
|
|
|
|
106
|
12 |
|
$this->httpClient = $this->httpClient ?: HttpClientDiscovery::find(); |
107
|
12 |
|
$this->messageFactory = $this->messageFactory ?: MessageFactoryDiscovery::find(); |
108
|
12 |
|
$unwrap = is_null($unwrap) ? $this->unwrap : $unwrap; |
109
|
|
|
|
110
|
12 |
|
return new Client($service, $this->serializer, $this->messageFactory, $this->httpClient, $unwrap); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $serviceName |
115
|
|
|
* @param array $services |
116
|
|
|
* @return array |
117
|
|
|
* @throws ServiceNotFoundException |
118
|
|
|
*/ |
119
|
14 |
|
private function getService($serviceName, array $services) |
120
|
|
|
{ |
121
|
14 |
|
if ($serviceName && isset($services[$serviceName])) { |
122
|
1 |
|
return $services[$serviceName]; |
123
|
13 |
|
} elseif ($serviceName) { |
124
|
|
|
throw new ServiceNotFoundException("The service named $serviceName can not be found"); |
125
|
|
|
} else { |
126
|
13 |
|
return reset($services); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $portName |
132
|
|
|
* @param array $service |
133
|
|
|
* @return array |
134
|
|
|
* @throws PortNotFoundException |
135
|
|
|
*/ |
136
|
14 |
|
private function getPort($portName, array $service) |
137
|
|
|
{ |
138
|
14 |
|
if ($portName && isset($service[$portName])) { |
139
|
1 |
|
return $service[$portName]; |
140
|
13 |
|
} elseif ($portName) { |
141
|
2 |
|
throw new PortNotFoundException("The port named $portName can not be found"); |
142
|
|
|
} else { |
143
|
11 |
|
return reset($service); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.