|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of the CleverAge/ProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2019 Clever-Age |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace CleverAge\SoapProcessBundle\Client; |
| 12 | + |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class Client |
| 17 | + * |
| 18 | + * @author Madeline Veyrenc <[email protected]> |
| 19 | + */ |
| 20 | +class Client implements ClientInterface |
| 21 | +{ |
| 22 | + /** @var string */ |
| 23 | + private $code; |
| 24 | + |
| 25 | + /** @var string|null */ |
| 26 | + private $wsdl; |
| 27 | + |
| 28 | + /** @var array */ |
| 29 | + private $options; |
| 30 | + |
| 31 | + /** @var LoggerInterface */ |
| 32 | + private $logger; |
| 33 | + |
| 34 | + /** @var \SoapClient */ |
| 35 | + private $soapClient; |
| 36 | + |
| 37 | + /** @var string */ |
| 38 | + private $lastRequest; |
| 39 | + |
| 40 | + /** @var string */ |
| 41 | + private $lastRequestHeaders; |
| 42 | + |
| 43 | + /** @var string */ |
| 44 | + private $lastResponse; |
| 45 | + |
| 46 | + /** @var string */ |
| 47 | + private $lastResponseHeaders; |
| 48 | + |
| 49 | + /** |
| 50 | + * Client constructor. |
| 51 | + * |
| 52 | + * @param LoggerInterface $logger |
| 53 | + * @param string $code |
| 54 | + * @param string|null $wsdl |
| 55 | + * @param array $options |
| 56 | + */ |
| 57 | + public function __construct(LoggerInterface $logger, string $code, ?string $wsdl, array $options) |
| 58 | + { |
| 59 | + $this->logger = $logger; |
| 60 | + $this->code = $code; |
| 61 | + $this->wsdl = $wsdl; |
| 62 | + $this->options = $options; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return LoggerInterface |
| 67 | + */ |
| 68 | + public function getLogger(): LoggerInterface |
| 69 | + { |
| 70 | + return $this->logger; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritdoc} |
| 75 | + * @throws \UnexpectedValueException |
| 76 | + */ |
| 77 | + public function getCode(): string |
| 78 | + { |
| 79 | + if (!$this->code) { |
| 80 | + throw new \UnexpectedValueException('Client code is not defined'); |
| 81 | + } |
| 82 | + |
| 83 | + return $this->code; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function getWsdl(): ?string |
| 90 | + { |
| 91 | + return $this->wsdl; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * {@inheritdoc} |
| 96 | + */ |
| 97 | + public function setWsdl(?string $wsdl): void |
| 98 | + { |
| 99 | + $this->wsdl = $wsdl; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * {@inheritdoc} |
| 104 | + */ |
| 105 | + public function getOptions(): array |
| 106 | + { |
| 107 | + return $this->options; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritdoc} |
| 112 | + */ |
| 113 | + public function setOptions(array $options): void |
| 114 | + { |
| 115 | + $this->options = $options; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return \SoapClient|null |
| 120 | + */ |
| 121 | + public function getSoapClient(): ?\SoapClient |
| 122 | + { |
| 123 | + return $this->soapClient; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param \SoapClient $soapClient |
| 128 | + */ |
| 129 | + public function setSoapClient(\SoapClient $soapClient): void |
| 130 | + { |
| 131 | + $this->soapClient = $soapClient; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @return string |
| 136 | + */ |
| 137 | + public function getLastRequest(): string |
| 138 | + { |
| 139 | + return $this->lastRequest; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param string $lastRequest |
| 144 | + */ |
| 145 | + public function setLastRequest(string $lastRequest): void |
| 146 | + { |
| 147 | + $this->lastRequest = $lastRequest; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @return string |
| 152 | + */ |
| 153 | + public function getLastRequestHeaders(): string |
| 154 | + { |
| 155 | + return $this->lastRequestHeaders; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param string $lastRequestHeaders |
| 160 | + */ |
| 161 | + public function setLastRequestHeaders(string $lastRequestHeaders): void |
| 162 | + { |
| 163 | + $this->lastRequestHeaders = $lastRequestHeaders; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @return string |
| 168 | + */ |
| 169 | + public function getLastResponse(): string |
| 170 | + { |
| 171 | + return $this->lastResponse; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @param string $lastResponse |
| 176 | + */ |
| 177 | + public function setLastResponse(string $lastResponse): void |
| 178 | + { |
| 179 | + $this->lastResponse = $lastResponse; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @return string |
| 184 | + */ |
| 185 | + public function getLastResponseHeaders(): string |
| 186 | + { |
| 187 | + return $this->lastResponseHeaders; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @param string $lastResponseHeaders |
| 192 | + */ |
| 193 | + public function setLastResponseHeaders(string $lastResponseHeaders): void |
| 194 | + { |
| 195 | + $this->lastResponseHeaders = $lastResponseHeaders; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * {@inheritdoc} |
| 200 | + */ |
| 201 | + public function call(string $method, array $input = []) |
| 202 | + { |
| 203 | + $this->initializeSoapClient(); |
| 204 | + |
| 205 | + $callMethod = sprintf('soapCall%s', ucfirst($method)); |
| 206 | + if (method_exists($this, $callMethod)) { |
| 207 | + return $this->$callMethod($input); |
| 208 | + } |
| 209 | + |
| 210 | + $this->getLogger()->notice( |
| 211 | + sprintf("Soap call '%s' on '%s'", $method, $this->getWsdl()) |
| 212 | + ); |
| 213 | + |
| 214 | + return $this->doSoapCall($method, $input); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @param string $method |
| 219 | + * @param array $input |
| 220 | + * |
| 221 | + * @return bool|mixed |
| 222 | + */ |
| 223 | + protected function doSoapCall(string $method, array $input = []) |
| 224 | + { |
| 225 | + if (!$this->getSoapClient()) { |
| 226 | + throw new \InvalidArgumentException('Soap client is not initialized'); |
| 227 | + } |
| 228 | + try { |
| 229 | + $result = $this->getSoapClient()->__soapCall($method, [$input]); |
| 230 | + } /** @noinspection PhpRedundantCatchClauseInspection */ catch (\SoapFault $e) { |
| 231 | + $this->getLastRequestTrace(); |
| 232 | + $this->getLogger()->alert( |
| 233 | + sprintf("Soap call '%s' on '%s' failed : %s", $method, $this->getWsdl(), $e->getMessage()), |
| 234 | + $this->getLastRequestTraceArray() |
| 235 | + ); |
| 236 | + |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + $this->getLastRequestTrace(); |
| 241 | + |
| 242 | + if (array_key_exists('trace', $this->getOptions()) && $this->getOptions()['trace']) { |
| 243 | + $this->getLogger()->debug( |
| 244 | + sprintf("Trace of soap call '%s' on '%s'", $method, $this->getWsdl()), |
| 245 | + $this->getLastRequestTraceArray() |
| 246 | + ); |
| 247 | + } |
| 248 | + |
| 249 | + return $result; |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Initialize \SoapClient object |
| 254 | + * |
| 255 | + * @return void |
| 256 | + */ |
| 257 | + protected function initializeSoapClient(): void |
| 258 | + { |
| 259 | + if (!$this->getSoapClient()) { |
| 260 | + $options = array_merge($this->getOptions(), ['trace' => true]); |
| 261 | + $this->setSoapClient(new \SoapClient($this->getWsdl(), $options)); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + */ |
| 267 | + protected function getLastRequestTrace(): void |
| 268 | + { |
| 269 | + if ($this->getSoapClient()) { |
| 270 | + $this->setLastRequest($this->getSoapClient()->__getLastRequest()); |
| 271 | + $this->setLastRequestHeaders($this->getSoapClient()->__getLastRequestHeaders()); |
| 272 | + $this->setLastResponse($this->getSoapClient()->__getLastResponse()); |
| 273 | + $this->setLastResponseHeaders($this->getSoapClient()->__getLastResponseHeaders()); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * @return array |
| 279 | + */ |
| 280 | + protected function getLastRequestTraceArray(): array |
| 281 | + { |
| 282 | + return [ |
| 283 | + 'LastRequest' => $this->getLastRequest(), |
| 284 | + 'LastRequestHeaders' => $this->getLastRequestHeaders(), |
| 285 | + 'LastResponse' => $this->getLastResponse(), |
| 286 | + 'LastResponseHeaders' => $this->getLastResponseHeaders(), |
| 287 | + ]; |
| 288 | + } |
| 289 | +} |
0 commit comments