/var/www/vhosts/nabawater/common/helpers
Edit: /var/www/vhosts/nabawater/common/helpers/FBHelper.php (4112B)
*/
class FBHelper
{
/**
* @var self
*/
private static $instance;
private $accessToken;
private $error;
private $fb;
/**
* FBHelper constructor.
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public function __construct()
{
$appId = '335980027469645';
$appSecret = '2a55fdec289cbfbad6b7e17221af4524';
if ($appId !== null || $appSecret !== null) {
$this->fb = new Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.10',
]);
}
$this->setError(false);
}
/**
* @return FBHelper
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @return string
* @throws \yii\base\InvalidParamException
*/
public function getLoginUrl()
{
if ($this->fb === null || !$this->fb instanceof Facebook) {
return Url::to('/');
}
$permissions = ['email']; // Optional permissions
$loginURL = $this->fb
->getRedirectLoginHelper()
->getLoginUrl(Url::to('/fb-callback', true), $permissions);
return str_replace("#_=_", "", $loginURL);
}
/**
* @return $this
*/
public function setAccessToken()
{
$this->setError(false);
$this->accessToken = null;
$fb = $this->fb;
$_SESSION['FBRLH_state'] = $_GET['state'];
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (FacebookResponseException $e) {
$this->setError($e->getMessage());
return $this;
} catch (FacebookSDKException $e) {
$this->setError($e->getMessage());
return $this;
}
if ($accessToken === null) {
if ($helper->getError()) {
$this->setError($helper->getErrorDescription());
}
return $this;
}
$this->accessToken = $accessToken;
return $this;
}
/**
* @return FBUserData
* @throws FacebookSDKException
*/
public function getUserData()
{
if ($this->accessToken === null) {
return new FBUserData([]);
}
$oResponse = $this->fb->get('/me?fields=id,name,email', $this->accessToken);
return new FBUserData($oResponse->getGraphUser());
}
/**
* @param $error
* @return $this
*/
public function setError($error)
{
$this->error = $error;
return $this;
}
/**
* @return mixed
*/
public function getError()
{
return $this->error;
}
}
/**
* Class FBUserData
* @package common\helpers
*/
class FBUserData extends Component
{
private $id;
private $name;
private $email;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
}