This is more flexible so better take it.
authorRoland Haeder <roland@mxchange.org>
Sat, 18 Apr 2015 10:15:44 +0000 (12:15 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 18 Apr 2015 10:15:44 +0000 (12:15 +0200)
Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/classes/main/factories/database/class_DatabaseWrapperFactory.php
inc/classes/main/factories/login/class_LoginFactory.php
inc/classes/main/filter/verifier/class_UserUnconfirmedVerifierFilter.php
inc/classes/main/user/guest/class_Guest.php

index f774928..5ecdea4 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class DatabaseWrapperFactory extends ObjectFactory {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
        /**
         * Returns a singleton socket registry instance. If an instance is found in
         * the registry it will be returned, else a new instance is created and
@@ -48,16 +58,6 @@ class DatabaseWrapperFactory extends ObjectFactory {
                // Return the instance
                return $wrapperInstance;
        }
-
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
-       }
 }
 
 // [EOF]
index 1bc3869..01b042c 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class LoginFactory extends ObjectFactory {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
        /**
         * Returns a singleton login instance for given request instance.
         *
         * @param       $requestInstance        An instance of a Requestable class
         * @return      $wrapperInstance        A database wrapper instance
         */
-       public static final function createWrapperByConfiguredName (Requestable $requestInstance) {
-               // 
-               die('requestInstance=<pre>'.print_r($requestInstance, TRUE).'</pre>');
-
+       public static final function createLoginObjectByRequest (Requestable $requestInstance) {
                // Get registry instance
                $registryInstance = Registry::getRegistry();
 
                // Do we have an instance in the registry?
-               if ($registryInstance->instanceExists($wrapperName)) {
+               if ($registryInstance->instanceExists('login_helper')) {
                        // Then use this instance
-                       $wrapperInstance = $registryInstance->getInstance($wrapperName);
+                       $userInstance = $registryInstance->getInstance('login_helper');
                } else {
-                       // Get the registry instance
-                       $wrapperInstance = self::createObjectByConfiguredName($wrapperName);
+                       // Probe on member instance
+                       try {
+                               // Get class name
+                               $className = FrameworkConfiguration::getSelfInstance()->getConfigEntry('user_class');
+
+                               // Try to instance it
+                               $userInstance = call_user_func_array(array($className, 'createMemberByRequest'), array($requestInstance));
+                       } catch (UnexpectedGuestAccountException $e) {
+                               // Then try it with guest account
+                               $className = FrameworkConfiguration::getSelfInstance()->getConfigEntry('guest_class');
+
+                               // Try to instance it
+                               $userInstance = call_user_func_array(array($className, 'createGuestByRequest'), array($requestInstance));
+                       }
 
                        // Set the instance in registry for further use
-                       $registryInstance->addInstance($wrapperName, $wrapperInstance);
+                       $registryInstance->addInstance('login_helper', $userInstance);
                }
 
                // Return the instance
-               return $wrapperInstance;
-       }
-
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
+               return $userInstance;
        }
 }
 
index 2bdaf07..c8e6f42 100644 (file)
@@ -53,11 +53,8 @@ class UserUnconfirmedVerifierFilter extends BaseFilter implements Filterable {
         * @return      void
         */
        public function execute (Requestable $requestInstance, Responseable $responseInstance) {
-               // Get member class
-               $userClass = $this->getConfigInstance()->getConfigEntry('user_class');
-
                // Get a user instance for comparison
-               $userInstance = call_user_func_array(array($userClass, 'createMemberByRequest'), array($requestInstance));
+               $userInstance = LoginFactory::createLoginObjectByRequest($requestInstance);
 
                // Is the email address valid?
                if ($userInstance->ifEmailAddressExists() === FALSE) {
index e2911cf..6fdefc7 100644 (file)
@@ -38,6 +38,32 @@ class Guest extends BaseUser implements ManageableGuest, Registerable {
                parent::__construct(__CLASS__);
        }
 
+       /**
+        * Creates a user by a given request instance
+        *
+        * @param       $requestInstance        An instance of a Requestable class
+        * @return      $userInstance           An instance of this user class
+        * @todo        Add more ways over creating user classes
+        */
+       public static final function createGuestByRequest (Requestable $requestInstance) {
+               // Determine if by email or username
+               if (!is_null($requestInstance->getRequestElement('username'))) {
+                       // Username supplied
+                       $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
+               } elseif (!is_null($requestInstance->getRequestElement('email'))) {
+                       // Email supplied
+                       $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
+               } else {
+                       // Unsupported mode
+                       $userInstance = new Guest();
+                       $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
+                       exit();
+               }
+
+               // Return the prepared instance
+               return $userInstance;
+       }
+
        /**
         * Creates an instance of this user class by a provided username. This
         * factory method will check if username is already taken and if not so it