Browse files

added fetching userData from deezer via accessToken

  • Loading branch information...
1 parent 21d3429 commit 2c46c3ff579bf3e4c00b05c8323c55dbc2c48a10 @fightbulc committed Jul 7, 2012
View
54 app/php/JsonRpc/V1/Web/Auth.php
@@ -1,54 +0,0 @@
-<?php
-
- namespace App\JsonRpc\V1\Web;
-
- class Auth extends \Simplon\Abstracts\AbstractClass
- {
- public function __construct(array $request)
- {
- // init facebook
- $this->initAccessToken();
-
- // get facebookUserId
- $facebookUserId = \Simplon\Lib\Facebook\FacebookLib::getInstance()
- ->getUserId();
-
- // try to get cached user data
- $facebookUserManager = new \App\Manager\FacebookUsers\FacebookUserManager();
- $facebookUserVo = $facebookUserManager->getByFacebookId($facebookUserId);
-
- // create new user
- if (!$facebookUserVo->getId())
- {
- // get user data from facebook
- $userData = \Simplon\Lib\Facebook\FacebookLib::getInstance()
- ->getUserProfile('/me');
-
- // set in VO
- $facebookUserVo->setData($userData);
-
- // write in sql
- $facebookUserManager->registerUser($facebookUserVo);
- }
-
- return TRUE;
- }
-
- // ##########################################
-
- public function initAccessToken()
- {
-
- }
- $token_url = "http://connect.deezer.com/oauth/access_token.php?app_id=" . $app_id . "&secret=" . $app_secret . "&code=" . $code;
-
- $response = file_get_contents($token_url);
-
- $params = NULL;
- parse_str($response, $params);
- $api_url = "http://api.deezer.com/2.0/user/me?access_token=" . $params['access_token'];
-
- $user = json_decode(file_get_contents($api_url));
-
- echo("Hello " . $user->name);
- }
View
40 app/php/Manager/DeezerUsers/UserManager.php
@@ -0,0 +1,40 @@
+<?php
+
+ namespace App\Manager\DeezerUsers;
+
+ class UserManager extends \Simplon\Abstracts\AbstractCacheQueryManager
+ {
+ /**
+ * @param $accessToken
+ * @return \App\Vo\UserVo
+ */
+ public function getUserVo($accessToken)
+ {
+ if (!array_key_exists('userDetails', $_SESSION))
+ {
+ $_SESSION['userDetails'] = $this->setUserDetails($accessToken);
+ }
+
+ $userVo = new \App\Vo\UserVo();
+ $userVo->setData($_SESSION['userDetails']);
+
+ return $userVo;
+ }
+
+ // ##########################################
+
+ /**
+ * @param $accessToken
+ * @return array
+ */
+ public function setUserDetails($accessToken)
+ {
+ $userApiUrl = 'http://api.deezer.com/2.0/user/me?access_token=' . $accessToken;
+
+ $rawResponse = \Simplon\Vendor\CURL\CURL::init($userApiUrl)
+ ->setReturnTransfer(TRUE)
+ ->execute();
+
+ return \Simplon\Lib\Helper\FormatHelper::jsonDecode($rawResponse);
+ }
+ }
View
83 app/php/Manager/FacebookUsers/FacebookUserManager.php
@@ -1,83 +0,0 @@
-<?php
-
- namespace App\Manager\FacebookUsers;
-
- class FacebookUserManager extends \Simplon\Abstracts\AbstractCacheQueryManager
- {
- /**
- * @return string
- */
- public function getUserQuery()
- {
- return "
- SELECT
- fb_id,
- id,
- first_name,
- last_name
- FROM
- fbusers
- WHERE
- fb_id = :fbId
- ";
- }
-
- // ##########################################
-
- /**
- * @param $facebookUserId
- * @return \App\Vo\FacebookUserVo
- */
- public function getByFacebookId($facebookUserId)
- {
- $dbQuery = new \Simplon\Lib\Db\DbCacheQuery();
-
- $sqlQuery = $this->getUserQuery();
-
- $sqlConditions = array(
- 'fbId' => $facebookUserId,
- );
-
- $dbQuery
- ->setSqlQuery($sqlQuery)
- ->setSqlConditions($sqlConditions);
-
- $userData = $this->fetchRow($dbQuery);
-
- if(empty($userData))
- {
- return new \App\Vo\FacebookUserVo();
- }
-
- return \App\Factory\VoFactory::singleFactory($userData, new \App\Vo\FacebookUserVo());
- }
-
- // ##########################################
-
- /**
- * @param \App\Vo\FacebookUserVo $facebookUserVo
- * @return bool
- */
- public function registerUser(\App\Vo\FacebookUserVo $facebookUserVo)
- {
- $dbQuery = new \Simplon\Lib\Db\DbCacheQuery();
-
- $data = array(
- 'id' => NULL,
- 'fb_id' => $facebookUserVo->getId(),
- 'username' => $facebookUserVo->getUserName(),
- 'first_name' => $facebookUserVo->getFirstName(),
- 'last_name' => $facebookUserVo->getLastName(),
- 'locale' => $facebookUserVo->getLocale(),
- 'gender' => $facebookUserVo->getGender(),
- 'created' => \Simplon\Lib\Helper\FormatHelper::getUnixTime(),
- 'updated' => \Simplon\Lib\Helper\FormatHelper::getUnixTime(),
- );
-
- $dbQuery
- ->setSqlTable('fbusers')
- ->setData($data);
-
- return $this->insert($dbQuery);
- }
- }
View
43 app/php/Manager/Memories/MemoriesManager.php
@@ -4,6 +4,23 @@
class MemoriesManager extends \Simplon\Abstracts\AbstractCacheQueryManager
{
+ private $_userManager;
+
+ /**
+ * @return \App\Manager\DeezerUsers\UserManager
+ */
+ protected function _getUserManager()
+ {
+ if (!$this->_userManager)
+ {
+ $this->_userManager = new \App\Manager\DeezerUsers\UserManager();
+ }
+
+ return $this->_userManager;
+ }
+
+ // ##########################################
+
/**
* @return string
*/
@@ -55,9 +72,13 @@ public function getByTrackId(\App\Request\Memories\rInterface\iGetByTrackId $req
*/
public function createMemory(\App\Request\Memories\rInterface\iCreateMemory $requestVo)
{
+ $userVo = $this
+ ->_getUserManager()
+ ->getUserVo($requestVo->getDeezerAccessToken());
+
$data = array(
'id' => NULL,
- 'user_id' => $requestVo->getUserId(),
+ 'user_id' => $userVo->getId(),
'track_id' => $requestVo->getTrackId(),
'mood_tag' => $requestVo->getMoodTag(),
'memory' => $requestVo->getMemory(),
@@ -162,9 +183,13 @@ protected function _updateMemoryUserVote($data)
*/
public function setUpVote(\App\Request\Memories\rInterface\iSetUpVote $requestVo)
{
+ $userVo = $this
+ ->_getUserManager()
+ ->getUserVo($requestVo->getDeezerAccessToken());
+
$data = array(
'memory_id' => $requestVo->getId(),
- 'user_id' => $requestVo->getUserId(),
+ 'user_id' => $userVo->getId(),
'vote' => 1
);
@@ -179,13 +204,17 @@ public function setUpVote(\App\Request\Memories\rInterface\iSetUpVote $requestVo
*/
public function setDownVote(\App\Request\Memories\rInterface\iSetDownVote $requestVo)
{
+ $userVo = $this
+ ->_getUserManager()
+ ->getUserVo($requestVo->getDeezerAccessToken());
+
$data = array(
'memory_id' => $requestVo->getId(),
- 'user_id' => $requestVo->getUserId(),
+ 'user_id' => $userVo->getId(),
'vote' => -1
);
- return $this->_InsertUpdateMemoryUserVote($requestVo->getId(), $requestVo->getUserId(), $data);
+ return $this->_InsertUpdateMemoryUserVote($requestVo->getId(), $userVo->getId(), $data);
}
// ##########################################
@@ -196,11 +225,15 @@ public function setDownVote(\App\Request\Memories\rInterface\iSetDownVote $reque
*/
public function removeVote(\App\Request\Memories\rRemoveVote $requestVo)
{
+ $userVo = $this
+ ->_getUserManager()
+ ->getUserVo($requestVo->getDeezerAccessToken());
+
$dbCacheQuery = new \Simplon\Lib\Db\DbCacheQuery();
$sqlConditions = array(
'memory_id' => $requestVo->getId(),
- 'user_id' => $requestVo->getUserId(),
+ 'user_id' => $userVo->getId(),
);
$dbCacheQuery
View
8 app/php/Request/Memories/rCreateMemory.php
@@ -15,14 +15,6 @@ public function getTrackId()
/**
* @return string
*/
- public function getUserId()
- {
- return $this->getByKey('userId');
- }
-
- /**
- * @return string
- */
public function getMoodTag()
{
return $this->getByKey('moodTag');
View
1 app/php/Request/Memories/rInterface/iCreateMemory.php
@@ -5,7 +5,6 @@
interface iCreateMemory extends \Simplon\Abstracts\iAbstractRequest
{
public function getTrackId();
- public function getUserId();
public function getMoodTag();
public function getMemory();
}
View
1 app/php/Request/Memories/rInterface/iRemoveVote.php
@@ -5,5 +5,4 @@
interface iRemoveVote extends \Simplon\Abstracts\iAbstractRequest
{
public function getId();
- public function getUserId();
}
View
1 app/php/Request/Memories/rInterface/iSetDownVote.php
@@ -5,5 +5,4 @@
interface iSetDownVote extends \Simplon\Abstracts\iAbstractRequest
{
public function getId();
- public function getUserId();
}
View
1 app/php/Request/Memories/rInterface/iSetUpVote.php
@@ -5,5 +5,4 @@
interface iSetUpVote extends \Simplon\Abstracts\iAbstractRequest
{
public function getId();
- public function getUserId();
}
View
48 app/php/Vo/UserVo.php
@@ -11,15 +11,57 @@ public function getId()
// ##########################################
- public function getAmount()
+ public function getFirstName()
{
- return $this->getByKey('amount');
+ return $this->getByKey('firstname');
}
// ##########################################
- public function getName()
+ public function getLastName()
+ {
+ return $this->getByKey('lastname');
+ }
+
+ // ##########################################
+
+ public function getFullName()
{
return $this->getByKey('name');
}
+
+ // ##########################################
+
+ public function getGender()
+ {
+ return $this->getByKey('gender');
+ }
+
+ // ##########################################
+
+ public function getCountry()
+ {
+ return $this->getByKey('country');
+ }
+
+ // ##########################################
+
+ public function getLanguage()
+ {
+ return $this->getByKey('lang');
+ }
+
+ // ##########################################
+
+ public function getProfileUrl()
+ {
+ return $this->getByKey('link');
+ }
+
+ // ##########################################
+
+ public function getAvatar()
+ {
+ return $this->getByKey('picture');
+ }
}
View
10 library/Simplon/Abstracts/AbstractVo.php
@@ -116,4 +116,14 @@ public function getCurrentUsersFacebookId()
return \Simplon\Lib\Facebook\FacebookLib::getInstance()
->getUserId();
}
+
+ // ##########################################
+
+ /**
+ * @return string
+ */
+ public function getDeezerAccessToken()
+ {
+ return $this->getByKey('accessToken');
+ }
}
View
1 library/Simplon/Abstracts/iAbstractRequest.php
@@ -5,4 +5,5 @@
interface iAbstractRequest
{
public function getCurrentUsersFacebookId();
+ public function getDeezerAccessToken();
}
View
2 public/api/v1/web/index.php
@@ -1,5 +1,7 @@
<?php
+ session_start();
+
require(__DIR__ . '/../../../../library/Simplon/Bootstrap.php');
require('api.php');
View
2 public/api/v1/web/schema/Web.Memories.createMemory.php
@@ -3,8 +3,8 @@
return array(
'params' => array(
+ 'accessToken' => 'string',
'trackId' => 'integer',
- 'userId' => 'integer',
'moodTag' => 'string',
'memory' => 'string',
View
2 public/api/v1/web/schema/Web.Memories.removeVote.php
@@ -3,8 +3,8 @@
return array(
'params' => array(
+ 'accessToken' => 'string',
'memoryId' => 'integer',
- 'userId' => 'integer',
),
View
2 public/api/v1/web/schema/Web.Memories.setDownVote.php
@@ -3,8 +3,8 @@
return array(
'params' => array(
+ 'accessToken' => 'string',
'memoryId' => 'integer',
- 'userId' => 'integer',
),
View
2 public/api/v1/web/schema/Web.Memories.setUpVote.php
@@ -3,8 +3,8 @@
return array(
'params' => array(
+ 'accessToken' => 'string',
'memoryId' => 'integer',
- 'userId' => 'integer',
),

0 comments on commit 2c46c3f

Please sign in to comment.