Work on making country nullable. Presently the datamapper cant deal with it though...
authorCameron Ball <cameron@getapproved.com.au>
Fri, 2 Jan 2015 03:57:44 +0000 (11:57 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 2 Jan 2015 03:57:44 +0000 (11:57 +0800)
Controllers/UserAuthController.php
Controllers/UserController.php
DataAccess/DataMapper/Helpers/VOMapsHelper.php
Domain/Entities/IUser.php
Domain/Entities/IUserBuilder.php
Domain/Entities/User.php
Domain/Entities/UserBuilder.php
Domain/Entities/UserFactory.php
Domain/Entities/UserStepByStepBuilder.php

index 0f044d1..ec9e8d8 100644 (file)
@@ -78,22 +78,30 @@ class UserAuthController implements IDivineController
     {
         $userProfile = $this->_facebookRequest->getGraphObject(GraphUser::className());    
 
-        $homeTownPageId = $userProfile->getProperty('hometown')->getProperty('id');
-        $pageRequest = (new FacebookRequest($this->_facebookSession, 'GET', '/' . $homeTownPageId ))->execute();
-        $pageLocation = $pageRequest->getGraphObject(GraphLocation::className())->getProperty('location')->cast(GraphLocation::className());
+        $homeTown = $userProfile->getProperty('hometown');   
+        
+        if($homeTown)
+        {
+            $homeTownPageId = $homeTown ? $homeTown->getProperty('id') : null;
+
+            $pageRequest = (new FacebookRequest($this->_facebookSession, 'GET', '/' . $homeTownPageId ))->execute();
+            $pageLocation = $pageRequest->getGraphObject(GraphLocation::className())->getProperty('location')->cast(GraphLocation::className());
+
+            $country = Util::countryNameFromLatLong($pageLocation->getLatitude(), $pageLocation->getLongitude());
+        }
         
-        $country = Util::countryNameFromLatLong($pageLocation->getLatitude(), $pageLocation->getLongitude());
         $firstName = $userProfile->getFirstName();
         $lastName = $userProfile->getLastName();
         $facebookId = $userProfile->getId();
-
+        
         //TODO: Is insantiating the VO classes here a good idea?
-        $newUser = $this->_userStepByStepBuilder->With_Country(new \Domain\VOs\Country($country))
-                                                ->With_DisplayName($firstName)
+        $newUser = $this->_userStepByStepBuilder->With_DisplayName($firstName)
                                                 ->With_Name(new \Domain\VOs\Name($firstName, $lastName))
                                                 ->With_Tags(array())
                                                 ->With_FacebookId($facebookId)
                                                 ->With_Quota(100000000) //XXX: quota is in bytes
+                                                //XXX: Is this confusing? Maybe better to do a conditional and only call with_country when we have a country
+                                                ->With_Country(isset($country) ? new \Domain\VOs\Country($country) : null)
                                                 ->build();
                 
         $this->_userRepository->save($newUser);
index 1504735..f7c7a0b 100644 (file)
@@ -53,7 +53,7 @@ class UserController implements IDivineController
             'name' => $user->getName()->getFullName(),
             'displayName' => $user->getDisplayName(),
             'tags' => $user->getTags(),
-            'country' => $user->getCountry()->getCountryName(),
+            'country' => $user->getCountry() ? $user->getCountry()->getCountryName() : null,
         );
         
         if($this->_userSession->getCurrentUser())
@@ -75,8 +75,12 @@ class UserController implements IDivineController
         try
         {
             if(isset($userUpdateData->displayName)) $user->setDisplayName($userUpdateData->displayName);
-            //TODO: Direct instantiation bad?
-            if(isset($userUpdateData->country)) $user->setCountry(new Country($userUpdateData->country));
+            
+            if(property_exists($userUpdateData,'country'))
+            {
+                //TODO: Direct instantiation bad?
+                $user->setCountry(isset($userUpdateData->country) ? new Country($userUpdateData->country) : null);
+            }
             $this->_userRepository->save($user);
         } catch (Exception $e) {
             if(strpos($e->getMessage(), 'Duplicate entry') !== false)
index 9a1f2cc..575d95e 100644 (file)
@@ -2,6 +2,7 @@
 \r
 namespace DataAccess\DataMapper\Helpers;\r
 \r
+use Exception;\r
 use ReflectionClass;\r
 \r
 class VOMapsHelper\r
@@ -79,8 +80,12 @@ class VOMapsHelper
         {\r
             return new $className;\r
         } else {\r
-            $r = new ReflectionClass($className);\r
-            return $r->newInstanceArgs($constructors);\r
+            try {\r
+                $r = new ReflectionClass($className);\r
+                return $r->newInstanceArgs($constructors);\r
+            } catch (Exception $e) {\r
+                return null;\r
+            }\r
         }\r
     }\r
 }\r
index 74d0c58..e26d697 100644 (file)
@@ -18,5 +18,5 @@ interface IUser
     public function getQuota();\r
     \r
     public function setDisplayName($displayName);\r
-    public function setCountry(ICountry $country);\r
+    public function setCountry(ICountry $country = null);\r
 }\r
index bb5ca03..38e67aa 100644 (file)
@@ -7,7 +7,7 @@ use Domain\VOs\IName;
 \r
 interface IUserBuilder\r
 {\r
-    public function With_Country(ICountry $country);\r
+    public function With_Country(ICountry $country = null);\r
     public function With_DisplayName($name);\r
     public function With_Name(IName $name);\r
     public function With_Tags(array $tags);\r
index 167404d..e78c8c1 100644 (file)
@@ -18,7 +18,7 @@ class User extends AbstractEntity implements IUser
     private $_quota;\r
     \r
     public function __construct(\r
-        ICountry $country,\r
+        ICountry $country = null,\r
         $displayName,\r
         IName $name,\r
         array $tags,\r
@@ -74,7 +74,7 @@ class User extends AbstractEntity implements IUser
         $this->_displayName = $displayName;\r
     }\r
     \r
-    public function setCountry(ICountry $country)\r
+    public function setCountry(ICountry $country = null)\r
     {\r
         $this->_country = $country;\r
     }\r
index 73eeb80..ad01c35 100644 (file)
@@ -23,7 +23,7 @@ class UserBuilder implements IUserBuilder
         $this->_userFactory = $userFactory;\r
     }\r
     \r
-    public function With_Country(ICountry $country) {\r
+    public function With_Country(ICountry $country = null) {\r
         $this->_country = $country;\r
         return $this;\r
     }\r
index f2e650d..0830209 100644 (file)
@@ -21,7 +21,7 @@ interface IUserFactory
 class UserFactory implements IUserFactory\r
 {\r
     public function createInstance(\r
-        ICountry $country,\r
+        ICountry $country = null,\r
         $displayName,\r
         IName $name,\r
         array $tags,\r
index 6f8d55c..e94c05d 100644 (file)
@@ -7,11 +7,6 @@ use Domain\VOs\IName;
 \r
 interface IUserStepByStepBuilder\r
 {\r
-    public function With_Country(ICountry $country);\r
-}\r
-\r
-interface IUserStepByStepBuilder_With_Country\r
-{\r
     public function With_DisplayName($name);\r
 }\r
 \r
@@ -38,6 +33,7 @@ interface IUserStepByStepBuilder_With_FacebookId
 interface IUserStepByStepBuilder_With_Quota\r
 {\r
     public function With_YearsStepArtist($years); //not going to make this mandatory as it is kind of a joke\r
+    public function With_Country(ICountry $country = null);\r
     public function build();\r
 }\r
     \r
@@ -53,14 +49,6 @@ abstract class AbstractUserStepByStepBuilder
 \r
 class UserStepByStepBuilder extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder\r
 {\r
-    public function With_Country(ICountry $country) {\r
-        $this->_userBuilder->With_Country($country);\r
-        return new UserStepByStepBuilder_With_Country($this->_userBuilder);\r
-    }\r
-}\r
-\r
-class UserStepByStepBuilder_With_Country extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_Country\r
-{\r
     public function With_DisplayName($name) {\r
         $this->_userBuilder->With_DisplayName($name);\r
         return new UserStepByStepBuilder_With_DisplayName($this->_userBuilder);\r
@@ -107,6 +95,11 @@ class UserStepByStepBuilder_With_Quota extends AbstractUserStepByStepBuilder imp
         return $this;\r
     }\r
     \r
+    public function With_Country(ICountry $country = null) {\r
+        $this->_userBuilder->With_Country($country);\r
+        return $this;\r
+    }\r
+\r
     public function build() {\r
         return $this->_userBuilder\r
                     ->build();\r