From: Roland Häder <roland@mxchange.org>
Date: Thu, 3 Dec 2020 08:28:01 +0000 (+0100)
Subject: Continued:
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f80f98497184715c479cd9103db7c72e3c046e9b;p=hub.git

Continued:
- added some type-hints for primitive variables
- added some checks on parameters
- some debug messages improved (sprintf() is the norm when parameters are logged)

Signed-off-by: Roland Häder <roland@mxchange.org>
---

diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php
index 34a499fb1..27e3c9197 100644
--- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php
+++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php
@@ -110,10 +110,8 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements
 	 * @return	$isNewPeer			Whether 'sender' is a new peer to this peer
 	 */
 	public function isSenderNewPeer (DeliverablePackage $packageInstance, StoreableCriteria $dataSetInstance = NULL) {
-		// Debug message
-		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-FRONTEND: packageInstance=' . $packageInstance . ' - CALLED!');
-
 		// Get a search criteria instance from package data
+		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-FRONTEND: packageInstance=' . $packageInstance . ' - CALLED!');
 		$searchInstance = $this->getSenderSearchInstanceFromPackageInstance($packageInstance);
 
 		// Is the dataset instance set?
@@ -183,7 +181,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements
 	/**
 	 * Registers the given peer state and An instance of a DeliverablePackage class
 	 *
-	 * @param	$stateInstance		A PeerStateable class instance
+	 * @param	$stateInstance		An instance of a PeerStateable class
 	 * @param	$packageInstance	An instance of a DeliverablePackage class
 	 * @return	void
 	 * @throws	PeerAlreadyRegisteredException	If a peer is already registered
@@ -244,10 +242,8 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements
 	 * @throws	UnexpectedValueException	If $resultInstance->next() returns FALSE
 	 */
 	public function isSamePeerState (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance) {
-		// Debug message
-		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-FRONTEND: State ' . $helperInstance->getPrintableState() . ' needs to be checked it has changed ...');
-
 		// Now get the search instance from given package data
+		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-FRONTEND: State ' . $helperInstance->getPrintableState() . ' needs to be checked it has changed ...');
 		$searchInstance = $this->getSenderSearchInstanceFromPackageInstance($packageInstance);
 
 		// With this search instance query the database for the peer and get a result instance
diff --git a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php
index b50c2f698..57672dbfe 100644
--- a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php
+++ b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php
@@ -77,15 +77,18 @@ class PeerStateFactory extends ObjectFactory {
 	 * @param	$helperInstance		An instance of a ConnectionHelper class
 	 * @param	$packageInstance	An instance of a DeliverablePackage class
 	 * @param	$socketInstance		An instance of a StorableSocket class
-	 * @param	$errorCode			The last error code
+	 * @param	$errorName			Error code in text form
 	 * @return	$stateInstance		A Stateable class instance
 	 */
-	public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance, $errorCode) {
+	public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance, string $errorName) {
 		// Error code and helper's state name should not be the same
-		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: helperInstance=%s,packageInstance=%s,socketInstance=%s,errorCode=%s - CALLED!', $helperInstance->__toString(), $packageInstance->__toString(), $socketInstance->__toString(), $errorCode));
-		if ($errorCode == $helperInstance->getPrintableState()) {
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: helperInstance=%s,packageInstance=%s,socketInstance=%s,errorName=%s - CALLED!', $helperInstance->__toString(), $packageInstance->__toString(), $socketInstance->__toString(), $errorName));
+		if (empty($errorName)) {
+			// Empty parameter
+			throw new InvalidArgumentException('Parameter "errorName" is empty');
+		} elseif ($errorName == $helperInstance->getPrintableState()) {
 			// Not valid!
-			throw new InvalidArgumentException(sprintf('Current state and errorCode=%s are the same.', $errorCode));
+			throw new InvalidArgumentException(sprintf('Current state and errorName=%s are the same.', $errorName));
 		}
 
 		// Init state instance, this is better coding practice
@@ -108,50 +111,43 @@ class PeerStateFactory extends ObjectFactory {
 
 		// Do we have an entry?
 		if ($tableInstance->isSenderNewPeer($packageInstance)) {
-			// Debug output
-			/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: errorCode=' . $errorCode);
-
 			// Register the new peer with its session id
+			/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: errorName=' . $errorName);
 			$tableInstance->registerPeerByPackageInstance($packageInstance, $socketInstance);
 
 			/*
 			 * It is a new peer so create the state instance based on error
 			 * code and get an instance from it.
 			 */
-			$stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
+			$stateInstance = self::createObjectByConfiguredName('peer_' . $errorName . '_state_class');
 
 			// And register it with the lookup table
 			$tableInstance->registerPeerState($stateInstance, $packageInstance);
 		} elseif ($tableInstance->isSamePeerState($helperInstance, $packageInstance)) {
-			// Debug output
-			/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: Peer state unchanged, re-generating old state ...');
-
 			/*
 			 * The peer's state has noot changed, still we have to return a
 			 * state instance, so generate it here.
 			 */
+			/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: Peer state unchanged, re-generating old state ...');
 			$stateInstance = self::createPeerStateInstanceByName($helperInstance->getPrintableState(), $helperInstance);
 		} else {
-			// Debug output
-			/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: Updating peer state ...');
-
 			/*
 			 * It is an already known peer but with a changed state. So first
 			 * get an instance of the state.
 			 */
-			$stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
+			/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: Updating peer state ...');
+			$stateInstance = self::createObjectByConfiguredName(sprintf('peer_%s_state_class', $errorName));
 
 			// The peer's state has changed, update database now
 			$tableInstance->registerPeerState($stateInstance, $packageInstance);
 		}
 
-		// Debug message
-		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
-
 		// Set the state in the helper
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: Peer state has changed from %s to %s (%s).', $helperInstance->getPrintableState(), $stateInstance->getStateName(), $stateInstance->__toString()));
 		$helperInstance->setStateInstance($stateInstance);
 
 		// For any purposes, return the state instance
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: stateInstance=%s - EXIT!', $stateInstance->__toString()));
 		return $stateInstance;
 	}
 
@@ -163,17 +159,23 @@ class PeerStateFactory extends ObjectFactory {
 	 * @param	$helperInstance		A ConnectionHelper class instance
 	 * @return	$stateInstance		A Stateable class instance
 	 */
-	public static final function createPeerStateInstanceByName ($stateName, ConnectionHelper $helperInstance) {
-		// Get a class from a configuration entry
-		$stateInstance = self::createObjectByConfiguredName('peer_' . $stateName . '_state_class', array($helperInstance));
+	public static final function createPeerStateInstanceByName (string $stateName, ConnectionHelper $helperInstance) {
+		// Validate parameter
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: stateName=%s,helperInstance=%s - CALLED!', $stateName, $helperInstance->__toString()));
+		if (empty($stateName)) {
+			// Empty parameter
+			throw new InvalidArgumentException('Parameter "stateName" is empty');
+		}
 
-		// Debug message
-		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY: Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
+		// Get a class from a configuration entry
+		$stateInstance = self::createObjectByConfiguredName(sprintf('peer_%s_state_class', $stateName), array($helperInstance));
 
 		// Once we have that state, set it in the peer instance
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: Peer state has changed from %s to %s (%s).', $helperInstance->getPrintableState(), $stateInstance->getStateName(), $stateInstance->__toString()));
 		$helperInstance->setStateInstance($stateInstance);
 
 		// For any purposes, return the state instance
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: stateInstance=%s - EXIT!', $stateInstance->__toString()));
 		return $stateInstance;
 	}
 
diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
index 96a7e3371..0e5a93b6b 100644
--- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
+++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
@@ -93,10 +93,10 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
 		} // END - if
 
 		// Translate the error code to an own name
-		$errorCode = $socketInstance->translateLastSocketErrorCodeToName();
+		$errorName = $socketInstance->translateLastSocketErrorCodeToName();
 
-		// Create a state instance based on $errorCode. This factory does the hard work for us
-		$stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageInstance, $socketInstance, $errorCode);
+		// Create a state instance based on $errorName. This factory does the hard work for us
+		$stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageInstance, $socketInstance, $errorName);
 
 		// Return the prepared instance
 		return $stateInstance;
diff --git a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php
index 9305d77b1..05bbd55df 100644
--- a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php
+++ b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php
@@ -55,7 +55,7 @@ interface LookupablePeerState extends Lookupable {
 	/**
 	 * Registers the given peer state and An instance of a DeliverablePackage class
 	 *
-	 * @param	$stateInstance	A PeerStateable class instance
+	 * @param	$stateInstance	An instance of a PeerStateable class
 	 * @param	$packageInstance	An instance of a DeliverablePackage class
 	 * @return	void
 	 */