]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Thu, 3 Dec 2020 08:28:01 +0000 (09:28 +0100)
committerRoland Häder <roland@mxchange.org>
Thu, 3 Dec 2020 08:28:01 +0000 (09:28 +0100)
- 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>
application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php
application/hub/classes/factories/states/peer/class_PeerStateFactory.php
application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php

index 34a499fb1681a7794e982d430f70050e5a71bc14..27e3c919742e7c9facb0ac233ed09f93085529c7 100644 (file)
@@ -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
index b50c2f698956a3a44acfe5a1a68da7a49dcd89c1..57672dbfe78f50de8b8854cc85dc12bec0532a09 100644 (file)
@@ -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;
        }
 
index 96a7e33713806ed0ecc91be34af6ca580d34b3c3..0e5a93b6b464448198d9e559589723d7f5904909 100644 (file)
@@ -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;
index 9305d77b18e5c15917308df1bae7ddd4df579a2c..05bbd55df1ffcb3b2b0cc8e8bb5402007219ea14 100644 (file)
@@ -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
         */