From: Roland Häder Date: Mon, 26 Oct 2020 15:24:30 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8e6ecb77791bd20960985f9571b2828c5b3afea6;p=hub.git Continued: - throwed more InvalidArgumentException when some method arguments are not valid Signed-off-by: Roland Häder --- 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