Skip to content

Commit

Permalink
refactor: internal class refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yizack committed Dec 3, 2024
1 parent e7594c4 commit 4a6e3a1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/InstagramFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
require_once "Config.php";

class InstagramFeed extends Config {
protected const TOKEN_REFRESH_INTERVAL = 86400; // 24 hours in seconds
private const BASE_URL = "https://graph.instagram.com";
private const TOKEN_REFRESH_INTERVAL = 86400; // 24 hours in seconds

protected function fetch($path) {
return json_decode(file_get_contents($path), true);
private function fetch($path) {
return json_decode(file_get_contents(self::BASE_URL . $path), true);
}

protected function refreshToken() {
private function refreshToken() {
$path = $this->getPath();
$filename = $this->getFilename();
$filePath = "$path/$filename";
Expand All @@ -29,7 +30,7 @@ protected function refreshToken() {
$updatedDate = $updatedJson["updated"] ?? $date;

if (strtotime($date) - strtotime($updatedDate) > self::TOKEN_REFRESH_INTERVAL) {
$refresh = $this->fetch("https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=" . $this->getToken());
$refresh = $this->fetch("/refresh_access_token?grant_type=ig_refresh_token&access_token=" . $this->getToken());
if (!$refresh) {
error_log("Warning: Failed to refresh token, please check your configuration or generate a new token.");
}
Expand All @@ -45,10 +46,10 @@ protected function refreshToken() {
* @param string|null $fields Comma-separated list of fields to retrieve. Defaults to 'username,permalink,timestamp,caption'.
* @return array The Instagram feed data.
*/
function getFeed($fields = null) {
public function getFeed($fields = null) {
$fields = $fields ?? 'username,permalink,timestamp,caption';
$this->refreshToken();
$feed = $this->fetch("https://graph.instagram.com/me/media?fields=" . $fields . "&access_token=" . $this->getToken());
$feed = $this->fetch("/me/media?fields=" . $fields . "&access_token=" . $this->getToken());
return $feed["data"] ?? [];
}
}
Expand Down

0 comments on commit 4a6e3a1

Please sign in to comment.