From 5fbcfb3e22e85593cffc8143753c2f621cb82bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Bechyn=C4=9B?= Date: Tue, 8 Nov 2016 18:31:28 +0100 Subject: [PATCH] AttachmentsHandlerInterface refactored --- ...er.php => AttachmentsHandlerInterface.php} | 9 ++++-- .../SoapOptions/SoapServerOptions.php | 11 +++++++ src/BeSimple/SoapServer/SoapServer.php | 30 +++++++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) rename src/BeSimple/SoapCommon/{AttachmentsHandler.php => AttachmentsHandlerInterface.php} (53%) diff --git a/src/BeSimple/SoapCommon/AttachmentsHandler.php b/src/BeSimple/SoapCommon/AttachmentsHandlerInterface.php similarity index 53% rename from src/BeSimple/SoapCommon/AttachmentsHandler.php rename to src/BeSimple/SoapCommon/AttachmentsHandlerInterface.php index 4de27b4..5ffd483 100644 --- a/src/BeSimple/SoapCommon/AttachmentsHandler.php +++ b/src/BeSimple/SoapCommon/AttachmentsHandlerInterface.php @@ -4,9 +4,12 @@ namespace BeSimple\SoapCommon; use BeSimple\SoapCommon\Storage\RequestHandlerAttachmentsStorage; -/** @todo: PBe - refactor this interface + usages -> inconsistent - adding storage, getting items - WTF APi? */ -interface AttachmentsHandler +interface AttachmentsHandlerInterface { public function addAttachmentStorage(RequestHandlerAttachmentsStorage $requestHandlerAttachmentsStorage); - public function getAttachmentsFromStorage(); + + /** + * @return RequestHandlerAttachmentsStorage + */ + public function getAttachmentStorage(); } diff --git a/src/BeSimple/SoapServer/SoapOptions/SoapServerOptions.php b/src/BeSimple/SoapServer/SoapOptions/SoapServerOptions.php index 08b39c1..805d306 100644 --- a/src/BeSimple/SoapServer/SoapOptions/SoapServerOptions.php +++ b/src/BeSimple/SoapServer/SoapOptions/SoapServerOptions.php @@ -61,6 +61,17 @@ class SoapServerOptions } } + public function getHandlerInstance() + { + if ($this->hasHandlerClass()) { + $handlerClassName = $this->handlerClass; + + return new $handlerClassName; + } + + return $this->getHandler(); + } + public function hasHandlerClass() { return $this->handlerClass !== null; diff --git a/src/BeSimple/SoapServer/SoapServer.php b/src/BeSimple/SoapServer/SoapServer.php index 452581c..652ff2c 100644 --- a/src/BeSimple/SoapServer/SoapServer.php +++ b/src/BeSimple/SoapServer/SoapServer.php @@ -13,7 +13,7 @@ namespace BeSimple\SoapServer; use BeSimple\SoapBundle\Soap\SoapAttachment; -use BeSimple\SoapCommon\AttachmentsHandler; +use BeSimple\SoapCommon\AttachmentsHandlerInterface; use BeSimple\SoapCommon\SoapKernel; use BeSimple\SoapCommon\SoapOptions\SoapOptions; use BeSimple\SoapCommon\SoapRequest; @@ -22,7 +22,7 @@ use BeSimple\SoapCommon\Storage\RequestHandlerAttachmentsStorage; use BeSimple\SoapServer\SoapOptions\SoapServerOptions; use BeSimple\SoapCommon\Converter\MtomTypeConverter; use BeSimple\SoapCommon\Converter\SwaTypeConverter; -use Exception; +use InvalidArgumentException; /** * Extended SoapServer that allows adding filters for SwA, MTOM, ... . @@ -47,6 +47,8 @@ class SoapServer extends \SoapServer */ public function __construct(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions) { + $this->validateSoapConfigs($soapServerOptions, $soapOptions); + $this->soapVersion = $soapOptions->getSoapVersion(); $this->soapServerOptions = $soapServerOptions; $this->soapOptions = $soapOptions; @@ -127,7 +129,7 @@ class SoapServer extends \SoapServer */ private function handleSoapRequest(SoapRequest $soapRequest) { - /** @var AttachmentsHandler $handler */ + /** @var AttachmentsHandlerInterface $handler */ $handler = $this->soapServerOptions->getHandler(); if ($this->soapOptions->hasAttachments()) { @@ -140,7 +142,7 @@ class SoapServer extends \SoapServer $attachments = []; if ($this->soapOptions->hasAttachments()) { - $attachments = $handler->getAttachmentsFromStorage(); + $attachments = $handler->getAttachmentStorage()->getAttachments(); } // Remove headers added by SoapServer::handle() method @@ -185,7 +187,7 @@ class SoapServer extends \SoapServer return $soapResponse; } - private function injectAttachmentStorage(AttachmentsHandler $handler, SoapRequest $soapRequest, $attachmentType) + private function injectAttachmentStorage(AttachmentsHandlerInterface $handler, SoapRequest $soapRequest, $attachmentType) { $attachments = []; if ($soapRequest->hasAttachments()) { @@ -216,7 +218,7 @@ class SoapServer extends \SoapServer } elseif ($soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) { $soapOptions->getTypeConverterCollection()->add(new MtomTypeConverter()); } else { - throw new Exception('Unresolved SOAP_ATTACHMENTS_TYPE: ' . $soapOptions->getAttachmentType()); + throw new InvalidArgumentException('Unresolved SOAP_ATTACHMENTS_TYPE: ' . $soapOptions->getAttachmentType()); } } @@ -235,4 +237,20 @@ class SoapServer extends \SoapServer return $filters; } + + private function validateSoapConfigs(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions) + { + if ($soapOptions->hasAttachments()) { + if (!$soapServerOptions->getHandlerInstance() instanceof AttachmentsHandlerInterface) { + throw new InvalidArgumentException( + sprintf( + '%s::handlerObject or handlerClass (instance of %s given) must implement %s in order to handle with attachments', + SoapServerOptions::class, + get_class($soapServerOptions->getHandlerInstance()), + AttachmentsHandlerInterface::class + ) + ); + } + } + } }