From 8e6ecb77791bd20960985f9571b2828c5b3afea6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 26 Oct 2020 16:24:30 +0100 Subject: [PATCH] Continued: - throwed more InvalidArgumentException when some method arguments are not valid MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../discovery/protocol/class_ProtocolDiscovery.php | 12 +++++++++--- .../handler/class_ProtocolHandlerFactory.php | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php index d4288d02c..ffa39519b 100644 --- a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php +++ b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php @@ -10,6 +10,9 @@ use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; +// Import SPL stuff +use \InvalidArgumentException; + /** * A Protocol discovery class * @@ -85,18 +88,21 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco * * @param $packageInstance An instance of a DeliverablePackage class * @return $protocolType Type of protocol, e.g. 'tcp' for TCP/IPv4 connections + * @throws InvalidArgumentException If $packageInstance has no valid recipient UNL set */ public static final function discoverProtocolByPackageInstance (DeliverablePackage $packageInstance) { - //* DEBUG: */ die(__METHOD__ . ':packageInstance=' . print_r($packageInstance, TRUE)); - /* * "Explode" the 'recipient' array element into a new one, giving at * least two entries: protocol://address */ + //* DEBUG: */ die(__METHOD__ . ':packageInstance=' . print_r($packageInstance, TRUE)); $recipient = explode(':', $packageInstance->getRecipientUnl()); // At least 2 entries must be found - assert(count($recipient) >= 2); + if (count($recipient) < 2) { + // Not valid + throw new InvalidArgumentException(sprintf('packageInstance with recipientType=%s has no valid UNL=%s set.', $packageInstance->getRecipientType(), $packageInstance->getRecipientUnl())); + } // Now get the first part (protocol type) and make all lower-case $protocolType = strtolower($recipient[0]); diff --git a/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php b/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php index 34cb97932..ddef8fc06 100644 --- a/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php +++ b/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php @@ -11,6 +11,9 @@ use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; +// Import SPL stuff +use \InvalidArgumentException; + /** * A factory class for protocol handlers * @@ -50,8 +53,15 @@ class ProtocolHandlerFactory extends ObjectFactory { * * @param $protocolType Protocol type to create/return a protocol handler instance for * @return $handlerInstance A protocol handler instance + * @throws InvalidArgumentException If protocolType is not valid */ private static function createProtocolHandlerByType ($protocolType) { + // Is it valid? + if (empty($protocolType)) { + // Throw excption + throw new InvalidArgumentException('protocolType is empty'); + } + // Do we have an instance in the registry? if (GenericRegistry::getRegistry()->instanceExists($protocolType . '_protocol_handler')) { // Then use this instance -- 2.39.5