diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d8d1220 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..a85fe78 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/social-api.iml b/.idea/social-api.iml new file mode 100644 index 0000000..6d39eca --- /dev/null +++ b/.idea/social-api.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Platforms/FacebookPlatform.php b/src/Platforms/FacebookPlatform.php new file mode 100644 index 0000000..8fc3a5f --- /dev/null +++ b/src/Platforms/FacebookPlatform.php @@ -0,0 +1,68 @@ +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; + } +}