Skip to content

Commit

Permalink
Apply fixes from StyleCI (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaibilyas authored Jul 29, 2022
1 parent 8e7c60f commit 71cb411
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
54 changes: 33 additions & 21 deletions src/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class Facebook

public function __construct(array $config)
{
if (!isset($config['app_id']) || $config['app_id'] == '') {
if (! isset($config['app_id']) || $config['app_id'] == '') {
throw new Exception('app_id not set in config array');
}

if (!isset($config['app_secret']) || $config['app_secret'] == '') {
if (! isset($config['app_secret']) || $config['app_secret'] == '') {
throw new Exception('app_secret not set in config array');
}

if (!isset($config['redirect_url']) || $config['redirect_url'] == '') {
if (! isset($config['redirect_url']) || $config['redirect_url'] == '') {
throw new Exception('redirect_url not set in config array');
}

Expand Down Expand Up @@ -63,21 +63,25 @@ public function setResponseType(string $type = 'json')
public function handleRedirect(callable $callable)
{
if (isset($_GET['code'], $_GET['state'])) {
$this->accessToken = json_decode($this->client->get($this->apiVersion . '/oauth/access_token?client_id=' . $this->config['app_id'] . '&client_secret=' . $this->config['app_secret'] . '&redirect_uri=' . $this->config['redirect_url'] . '&code=' . $_GET['code'])->getBody()->getContents())->access_token;
$this->accessToken = json_decode($this->client->get($this->apiVersion.'/oauth/access_token?client_id='.$this->config['app_id'].'&client_secret='.$this->config['app_secret'].'&redirect_uri='.$this->config['redirect_url'].'&code='.$_GET['code'])->getBody()->getContents())->access_token;

$user = json_decode($this->client->get($this->apiVersion . '/me?fields=id,first_name,last_name,name,picture.height(150).width(150)&access_token=' . $this->accessToken)->getBody()->getContents());
$user = json_decode($this->client->get($this->apiVersion.'/me?fields=id,first_name,last_name,name,picture.height(150).width(150)&access_token='.$this->accessToken)->getBody()->getContents());

$response = [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'full_name' => $user->name,
'profile_picture_url' => $user->picture->data->url,
'access_token' => $this->accessToken
'access_token' => $this->accessToken,
];

if ($this->responseType == 'json') $response = json_encode($response);
if ($this->responseType == 'object') $response = json_decode(json_encode($response));
if ($this->responseType == 'json') {
$response = json_encode($response);
}
if ($this->responseType == 'object') {
$response = json_decode(json_encode($response));
}

$callable($response);
exit;
Expand All @@ -89,12 +93,12 @@ public function getLoginUrl(array $permissions = ['public_profile', 'email'])
$permissions = implode(',', $permissions);
$this->state = bin2hex(random_bytes(20));

return 'https://www.facebook.com/' . $this->apiVersion . '/dialog/oauth?client_id=' . $this->config['app_id'] . '&redirect_uri=' . $this->config['redirect_url'] . '&scope=' . $permissions . '&state=' . $this->state;
return 'https://www.facebook.com/'.$this->apiVersion.'/dialog/oauth?client_id='.$this->config['app_id'].'&redirect_uri='.$this->config['redirect_url'].'&scope='.$permissions.'&state='.$this->state;
}

public function post(string $path, array $params, string $accessToken = null)
{
$path = $path[0] == '/' ? $path : '/' . $path;
$path = $path[0] == '/' ? $path : '/'.$path;

$this->accessToken = $accessToken ? $accessToken : $this->accessToken;

Expand All @@ -104,7 +108,7 @@ public function post(string $path, array $params, string $accessToken = null)
}

try {
$this->response = $this->client->post($this->apiVersion . $path . $separator . 'access_token=' . $this->accessToken, [
$this->response = $this->client->post($this->apiVersion.$path.$separator.'access_token='.$this->accessToken, [
'json' => $params,
]);

Expand All @@ -116,7 +120,7 @@ public function post(string $path, array $params, string $accessToken = null)

public function get(string $path, string $accessToken = null)
{
$path = $path[0] == '/' ? $path : '/' . $path;
$path = $path[0] == '/' ? $path : '/'.$path;

$this->accessToken = $accessToken ? $accessToken : $this->accessToken;

Expand All @@ -126,7 +130,8 @@ public function get(string $path, string $accessToken = null)
}

try {
$this->response = $this->client->get($this->apiVersion . $path . $separator . 'access_token=' . $this->accessToken);
$this->response = $this->client->get($this->apiVersion.$path.$separator.'access_token='.$this->accessToken);

return $this->getResponse();
} catch (\Exception $e) {
return $e->getResponse()->getBody()->getContents();
Expand All @@ -135,7 +140,7 @@ public function get(string $path, string $accessToken = null)

public function delete(string $path, string $accessToken = null)
{
$path = $path[0] == '/' ? $path : '/' . $path;
$path = $path[0] == '/' ? $path : '/'.$path;

$this->accessToken = $accessToken ? $accessToken : $this->accessToken;

Expand All @@ -145,7 +150,8 @@ public function delete(string $path, string $accessToken = null)
}

try {
$this->response = $this->client->delete($this->apiVersion . $path . $separator . 'access_token=' . $this->accessToken);
$this->response = $this->client->delete($this->apiVersion.$path.$separator.'access_token='.$this->accessToken);

return $this->getResponse();
} catch (\Exception $e) {
return $e->getResponse()->getBody()->getContents();
Expand All @@ -154,24 +160,30 @@ public function delete(string $path, string $accessToken = null)

public function getBusinesses(string $userId = 'me', array $fields = ['id', 'name'], int $limit = 100, string $accessToken = null)
{
return $this->get($userId . '/businesses?fields=' . implode(',', $fields) . '&limit=' . $limit, $accessToken);
return $this->get($userId.'/businesses?fields='.implode(',', $fields).'&limit='.$limit, $accessToken);
}

public function getPages(string $userId = 'me', array $fields = ['id', 'name'], int $limit = 100, string $accessToken = null)
{
return $this->get($userId . '/accounts?fields=' . implode(',', $fields) . '&limit=' . $limit, $accessToken);
return $this->get($userId.'/accounts?fields='.implode(',', $fields).'&limit='.$limit, $accessToken);
}

public function getAdAccounts(string $userId = 'me', array $fields = ['id', 'name'], int $limit = 100, string $accessToken = null)
{
return $this->get($userId . '/adaccounts?fields=' . implode(',', $fields) . '&limit=' . $limit, $accessToken);
return $this->get($userId.'/adaccounts?fields='.implode(',', $fields).'&limit='.$limit, $accessToken);
}

private function getResponse()
{
if ($this->responseType == 'json') return $this->toJson();
if ($this->responseType == 'object') return $this->toObject();
if ($this->responseType == 'array') return $this->toArray();
if ($this->responseType == 'json') {
return $this->toJson();
}
if ($this->responseType == 'object') {
return $this->toObject();
}
if ($this->responseType == 'array') {
return $this->toArray();
}
}

public function toObject(): stdClass
Expand Down
1 change: 0 additions & 1 deletion tests/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SohaibIlyas\FacebookPhpSdk\Tests;

use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\TestCase;
use SohaibIlyas\FacebookPhpSdk\Facebook;

Expand Down

0 comments on commit 71cb411

Please sign in to comment.