Skip to content

Commit

Permalink
Merge pull request #29 from MustafaFares445/main
Browse files Browse the repository at this point in the history
Feat (Project): Get Facebook Login required data
  • Loading branch information
somarkn99 committed Feb 16, 2024
2 parents e96f744 + 0813add commit 9584e16
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/social-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/Platforms/FacebookPlatform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\src\Platforms;

use Exception;

/**
* @author by:Mustafa Fares.
* GitHub: https://github.com/MustafaFares445
* LinkedIn:https://www.linkedin.com/in/mustafa-fares/
*/
class Facebook extends AbstractPlatform
{
protected $scopes = ['id', 'name', 'first_name', 'last_name', 'email', 'gender', 'picture'];
protected $baseUrl = 'https://graph.facebook.com/me';

/**
* @throws Exception
*/
public function getUserInfo(): array
{
try {
// Send a request to the GitHub API endpoint and store the response.
$response = $this->sendRequest($this->baseUrl);

// Check if the response contains an 'id', indicating successful retrieval of user data.
if (isset($response['id'])) {
// Map the raw API response data to a structured format based on predefined scopes.
return $this->mapUserDataByScopes($this->scopes, $response);
}

// If no 'id' is present in the response, throw an exception indicating failure to fetch user info.
throw new \Exception('Failed to fetch user info');
} catch (\Exception $e) {
// Log the exception message for debugging purposes.
Log::error('Failed To Fetch User Information: ' . $e->getMessage());
// Rethrow the exception to be handled by the caller.
throw $e;
}
}


public function mapUserDataByScopes($scopes, $userData): array
{
$mappedData = [];
$requiredFields = [];

// Iterate through the requested scopes and add them to the list of required fields.
foreach ($scopes as $scope) {
$requiredFields[] = 'id'; // Ensure 'id' is always included.
if (in_array($scope, $this->scopes)) {
$requiredFields[] = $scope;
}
}

// Fallback to default scopes if no specific fields are required.
if (empty($requiredFields)) {
$requiredFields = $this->scopes;
}

// Map each required field from the raw API response to the structured mapped data array.
foreach ($requiredFields as $field) {
$mappedData[$field] = $userData[$field] ?? null;
}

return $mappedData;
}
}

0 comments on commit 9584e16

Please sign in to comment.