From 797774b9428e73f8ed5c48f220fb5c7e4483af6d Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 2 Jan 2015 11:57:44 +0800 Subject: [PATCH] Work on making country nullable. Presently the datamapper cant deal with it though, so work on that --- Controllers/UserAuthController.php | 22 +++++++++++++++------- Controllers/UserController.php | 10 +++++++--- DataAccess/DataMapper/Helpers/VOMapsHelper.php | 9 +++++++-- Domain/Entities/IUser.php | 2 +- Domain/Entities/IUserBuilder.php | 2 +- Domain/Entities/User.php | 4 ++-- Domain/Entities/UserBuilder.php | 2 +- Domain/Entities/UserFactory.php | 2 +- Domain/Entities/UserStepByStepBuilder.php | 19 ++++++------------- 9 files changed, 41 insertions(+), 31 deletions(-) diff --git a/Controllers/UserAuthController.php b/Controllers/UserAuthController.php index 0f044d1..ec9e8d8 100644 --- a/Controllers/UserAuthController.php +++ b/Controllers/UserAuthController.php @@ -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); diff --git a/Controllers/UserController.php b/Controllers/UserController.php index 1504735..f7c7a0b 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -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) diff --git a/DataAccess/DataMapper/Helpers/VOMapsHelper.php b/DataAccess/DataMapper/Helpers/VOMapsHelper.php index 9a1f2cc..575d95e 100644 --- a/DataAccess/DataMapper/Helpers/VOMapsHelper.php +++ b/DataAccess/DataMapper/Helpers/VOMapsHelper.php @@ -2,6 +2,7 @@ namespace DataAccess\DataMapper\Helpers; +use Exception; use ReflectionClass; class VOMapsHelper @@ -79,8 +80,12 @@ class VOMapsHelper { return new $className; } else { - $r = new ReflectionClass($className); - return $r->newInstanceArgs($constructors); + try { + $r = new ReflectionClass($className); + return $r->newInstanceArgs($constructors); + } catch (Exception $e) { + return null; + } } } } diff --git a/Domain/Entities/IUser.php b/Domain/Entities/IUser.php index 74d0c58..e26d697 100644 --- a/Domain/Entities/IUser.php +++ b/Domain/Entities/IUser.php @@ -18,5 +18,5 @@ interface IUser public function getQuota(); public function setDisplayName($displayName); - public function setCountry(ICountry $country); + public function setCountry(ICountry $country = null); } diff --git a/Domain/Entities/IUserBuilder.php b/Domain/Entities/IUserBuilder.php index bb5ca03..38e67aa 100644 --- a/Domain/Entities/IUserBuilder.php +++ b/Domain/Entities/IUserBuilder.php @@ -7,7 +7,7 @@ use Domain\VOs\IName; interface IUserBuilder { - public function With_Country(ICountry $country); + public function With_Country(ICountry $country = null); public function With_DisplayName($name); public function With_Name(IName $name); public function With_Tags(array $tags); diff --git a/Domain/Entities/User.php b/Domain/Entities/User.php index 167404d..e78c8c1 100644 --- a/Domain/Entities/User.php +++ b/Domain/Entities/User.php @@ -18,7 +18,7 @@ class User extends AbstractEntity implements IUser private $_quota; public function __construct( - ICountry $country, + ICountry $country = null, $displayName, IName $name, array $tags, @@ -74,7 +74,7 @@ class User extends AbstractEntity implements IUser $this->_displayName = $displayName; } - public function setCountry(ICountry $country) + public function setCountry(ICountry $country = null) { $this->_country = $country; } diff --git a/Domain/Entities/UserBuilder.php b/Domain/Entities/UserBuilder.php index 73eeb80..ad01c35 100644 --- a/Domain/Entities/UserBuilder.php +++ b/Domain/Entities/UserBuilder.php @@ -23,7 +23,7 @@ class UserBuilder implements IUserBuilder $this->_userFactory = $userFactory; } - public function With_Country(ICountry $country) { + public function With_Country(ICountry $country = null) { $this->_country = $country; return $this; } diff --git a/Domain/Entities/UserFactory.php b/Domain/Entities/UserFactory.php index f2e650d..0830209 100644 --- a/Domain/Entities/UserFactory.php +++ b/Domain/Entities/UserFactory.php @@ -21,7 +21,7 @@ interface IUserFactory class UserFactory implements IUserFactory { public function createInstance( - ICountry $country, + ICountry $country = null, $displayName, IName $name, array $tags, diff --git a/Domain/Entities/UserStepByStepBuilder.php b/Domain/Entities/UserStepByStepBuilder.php index 6f8d55c..e94c05d 100644 --- a/Domain/Entities/UserStepByStepBuilder.php +++ b/Domain/Entities/UserStepByStepBuilder.php @@ -7,11 +7,6 @@ use Domain\VOs\IName; interface IUserStepByStepBuilder { - public function With_Country(ICountry $country); -} - -interface IUserStepByStepBuilder_With_Country -{ public function With_DisplayName($name); } @@ -38,6 +33,7 @@ interface IUserStepByStepBuilder_With_FacebookId interface IUserStepByStepBuilder_With_Quota { public function With_YearsStepArtist($years); //not going to make this mandatory as it is kind of a joke + public function With_Country(ICountry $country = null); public function build(); } @@ -53,14 +49,6 @@ abstract class AbstractUserStepByStepBuilder class UserStepByStepBuilder extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder { - public function With_Country(ICountry $country) { - $this->_userBuilder->With_Country($country); - return new UserStepByStepBuilder_With_Country($this->_userBuilder); - } -} - -class UserStepByStepBuilder_With_Country extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_Country -{ public function With_DisplayName($name) { $this->_userBuilder->With_DisplayName($name); return new UserStepByStepBuilder_With_DisplayName($this->_userBuilder); @@ -107,6 +95,11 @@ class UserStepByStepBuilder_With_Quota extends AbstractUserStepByStepBuilder imp return $this; } + public function With_Country(ICountry $country = null) { + $this->_userBuilder->With_Country($country); + return $this; + } + public function build() { return $this->_userBuilder ->build(); -- 2.11.0