Bug fixes and UserController
authorCameron Ball <cameron@getapproved.com.au>
Wed, 22 Oct 2014 07:49:10 +0000 (15:49 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Wed, 22 Oct 2014 07:49:10 +0000 (15:49 +0800)
Controllers/UserAuthController.php
Controllers/UserController.php [new file with mode: 0644]
DataAccess/UserRepository.php
Domain/Entities/IUser.php
Domain/Entities/User.php
config/DataMaps.php
config/Routes.php
public_html/index.php

index f261547..1b66204 100644 (file)
@@ -53,8 +53,6 @@ class UserAuthController implements IDivineController
         
         // If the user is not in the DB, create them.
         $user = $this->_userRepository->findByFacebookId($id) ?: $this->registerUser();
-        // Update stored auth token if needed.
-        $this->updateAuthToken($user);
 
         $this->_response->setHeader('Content-Type', 'application/json')
                         ->setBody(json_encode(array('token' => $this->_facebookSession->getToken(), 'expires' => $this->getSessionExpiryTimestamp($this->_facebookSession), 'displayName' => $user->getDisplayName())))
@@ -104,15 +102,6 @@ class UserAuthController implements IDivineController
         return $newUser;
     }
     
-    private function updateAuthToken(IUser $user)
-    {
-        if($user->getAuthToken() != $this->_facebookSession->getToken())
-        {
-            $user->setAuthToken($this->_facebookSession->getToken());
-            $this->_userRepository->save($user);
-        }
-    }
-    
     private function isSessionLongLived(FacebookSession $session)
     {
         return $this->getSessionExpiryTimestamp($session) - time() >= 60;
diff --git a/Controllers/UserController.php b/Controllers/UserController.php
new file mode 100644 (file)
index 0000000..822dd13
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+namespace Controllers;
+
+use Controllers\IDivineController;
+use Services\Http\IHttpRequest;
+use Services\Http\IHttpResponse;
+use DataAccess\IUserRepository;
+
+class UserController implements IDivineController
+{
+    private $_userRepository;
+    private $_response;
+    private $_request;
+    
+    public function __construct(
+        IHttpRequest $request,
+        IHttpResponse $response,
+        IUserRepository $userRepository
+    ) {
+        $this->_request = $request;
+        $this->_response = $response;
+        $this->_userRepository = $userRepository;
+    }
+    
+    public function indexAction() {
+        ;
+    }
+    
+    // list simfiles
+    public function getUserAction($facebookId)
+    {
+        /* @var $user Domain\Entities\IUser */
+        $user = $this->_userRepository->findByFacebookId($facebookId);
+
+        $returnArray = array(
+            'id' => $user->getId(),
+            'name' => $user->getName()->getFullName(),
+            'displayName' => $user->getDisplayName(),
+            'tags' => $user->getTags(),
+            'country' => $user->getCountry()->getCountryName()
+        );
+
+        $this->_response->setHeader('Content-Type', 'application/json')
+                        ->setBody(json_encode($returnArray))
+                        ->sendResponse();
+    }
+}
index 70fe069..b98ddd2 100644 (file)
@@ -6,16 +6,19 @@ use DataAccess\IUserRepository;
 use DataAccess\DataMapper\IDataMapper;
 use Domain\Entities\IUser;
 use DataAccess\Queries\IQueryBuilderFactory;
+use Services\IFacebookSessionFactory;
 
 //TODO: Implement some sort of caching. Probably OK for now not to worry.
 class UserRepository implements IUserRepository
 {
     private $_dataMapper;
     private $_queryBuilderFactory;
+    private $_facebookSessionFactory;
     
-    public function __construct(IDataMapper $dataMapper, IQueryBuilderFactory $queryBuilderFactory) {
+    public function __construct(IDataMapper $dataMapper, IQueryBuilderFactory $queryBuilderFactory, IFacebookSessionFactory $faceBookSessionFactory) {
         $this->_dataMapper = $dataMapper;
         $this->_queryBuilderFactory = $queryBuilderFactory;
+        $this->_facebookSessionFactory = $faceBookSessionFactory;
     }
     
     public function findById($id) {
@@ -49,10 +52,10 @@ class UserRepository implements IUserRepository
     }
     
     public function findByAuthToken($token) {
-        return $this->_dataMapper->map(
-            'User',
-            'SELECT * FROM %s WHERE auth_token=' . $token
-        );
+        $facebookSession = $this->_facebookSessionFactory->createInstance($token);
+        $id = $facebookSession->getGraphObject(GraphUser::className())->getId();
+        
+        return $this->findByFacebookId($id);
     }
     
     public function save(IUser $entity) {
index 143afc8..aefd961 100644 (file)
@@ -13,6 +13,4 @@ interface IUser
     public function getYearsStepArtist();\r
     public function getFacebookId();\r
     public function setFacebookId($id);\r
-    public function getAuthToken();\r
-    public function setAuthToken($token);\r
 }\r
index 3b65ef1..dfa67e9 100644 (file)
@@ -9,29 +9,25 @@ use Domain\VOs\IName;
 \r
 class User extends AbstractEntity implements IUser\r
 {\r
-    private $_id;\r
     private $_country;\r
     private $_displayName;\r
     private $_name;\r
     private $_tags;\r
     private $_yearsStepArtist;\r
     private $_facebookId;\r
-    private $_authToken;\r
     \r
     public function __construct(\r
         ICountry $country,\r
         $displayName,\r
         IName $name,\r
         array $tags,\r
-        $facebookId,\r
-        $authToken = null\r
+        $facebookId\r
     ) {\r
         $this->_country = $country;\r
         $this->_displayName = $displayName;\r
         $this->_name = $name;\r
         $this->_tags = $tags;\r
         $this->_facebookId = $facebookId;\r
-        $this->_authToken = $authToken;\r
     }\r
         \r
     public function getCountry() {\r
@@ -60,16 +56,6 @@ class User extends AbstractEntity implements IUser
         $this->_facebookId = $id;\r
     }\r
     \r
-    public function getAuthToken()\r
-    {\r
-        return $this->_authToken;\r
-    }\r
-    \r
-    public function setAuthToken($token)\r
-    {\r
-        $this->_authToken = $token;\r
-    }\r
-    \r
     public function getYearsStepArtist() {\r
         return $this->_yearsStepArtist;\r
     }\r
index dbe5157..559e7ef 100644 (file)
@@ -41,8 +41,7 @@ return [
             'displayName' => DataAccess\Varchar('display_name'),\r
             'name' => DataAccess\VO('Name'),\r
             'tags' => DataAccess\VOArray('Tag', 'getTags'), // TODO: Make VarcharArray class\r
-            'facebookId' => DataAccess\Varchar('facebook_id'),\r
-            'authToken' => DataAccess\Varchar('auth_token')\r
+            'facebookId' => DataAccess\Varchar('facebook_id')\r
         ]\r
     ],\r
     \r
index e3188f6..73c3cae 100644 (file)
@@ -22,5 +22,11 @@ return [
     '/user/auth' => [\r
         'method' => ['GET'],\r
         'controller' => 'UserAuth'\r
+    ],\r
+    \r
+    '/user/:facebookId' => [\r
+        'method' => ['GET'],\r
+        'controller' => 'User',\r
+        'action' => 'getUser'\r
     ]\r
 ];\r
index 8ed4f0b..692cfd4 100644 (file)
@@ -23,5 +23,5 @@ $controllerActionArgs = $router->getActionArgs();
 $controller = $container->get('Controllers\\' . ucfirst($controllerName) . 'Controller' );\r
 \r
 // Last thing to do, call the action on the specified controller.\r
-call_user_func(array($controller, $controllerAction . 'Action'), $controllerActionArgs);\r
+call_user_func_array(array($controller, $controllerAction . 'Action'), $controllerActionArgs);\r
 \r