diff --git a/app/Jobs/InstaGetTopMediasJob.php b/app/Jobs/InstaGetTopMediasJob.php index fa662c9..dcb8c5f 100644 --- a/app/Jobs/InstaGetTopMediasJob.php +++ b/app/Jobs/InstaGetTopMediasJob.php @@ -33,66 +33,15 @@ class InstaGetTopMediasJob implements ShouldQueue public function handle(): void { DB::transaction(function () { - $fields = config('services.facebook.enpoints.ig_top_media.fields'); - $hashtag_id = config('services.facebook.enpoints.ig_top_media.hashtag_id'); - $user_id = config('services.facebook.enpoints.ig_top_media.user_id'); - $limit = config('services.facebook.enpoints.ig_top_media.limit'); - $media_count = config('services.facebook.enpoints.ig_top_media.media_count'); - if (\is_string($fields)) { - $fields = \explode(',', $fields); - } - if (!\is_array($fields)) { - throw new \RuntimeException( - 'Missing services.facebook.enpoints.ig_top_media.fields, - FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_FIELDS' - ); - } - // * Remove empty - $fields = \collect($fields)->filter()->map(function (string $field_name) { - return \trim($field_name); - })->all(); - - if (!empty($diff = \array_diff($fields, $this->defaultFields))) { - throw new \RuntimeException( - 'These media fields are not handled : ' . \implode($diff) - ); - } - - if (empty($fields)) { - throw new \RuntimeException( - 'Missing services.facebook.enpoints.ig_top_media.fields, - FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_FIELDS' - ); - } - if (empty($hashtag_id)) { - throw new \RuntimeException( - 'Missing services.facebook.enpoints.ig_top_media.hashtag_id, - FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_HASHTAGID' - ); - } - if (empty($user_id)) { - throw new \RuntimeException( - 'Missing services.facebook.enpoints.ig_top_media.user_id, - FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_USERID' - ); - } - if (empty($limit)) { - throw new \RuntimeException( - 'Missing services.facebook.enpoints.ig_top_media.limit, - FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_LIMIT' - ); - } - if (empty($media_count)) { - throw new \RuntimeException( - 'Missing services.facebook.enpoints.ig_top_media.media_count, - FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_MEDIACOUNT' - ); - } - $hashtag_id = \intval($hashtag_id); - $user_id = \intval($user_id); - $limit = \intval($limit); - $media_count = \intval($media_count); + /** + * @var integer $hashtag_id + * @var integer $user_id + * @var integer $limit + * @var integer $media_count + * @var array $fields + */ + \extract($this->fetchConfigParameters()); /** @var \Illuminate\Database\Eloquent\Collection */ $excludePosts = InstaMediaExclude::query()->where('type', ExcludeTypeEnum::post)->get(); @@ -106,12 +55,60 @@ class InstaGetTopMediasJob implements ShouldQueue $user_id, $excludePosts, $excludeUsers, - $fields, $limit, $media_count - )->map(function (array $ig_media) { - (new InstaMedia($ig_media))->saveOrFail(); + )->map(function (object $ig_media) { + (new InstaMedia((array)$ig_media))->saveOrFail(); }); }); } + + /** + * Fetch and check config elements + * + * @return array 'hashtag_id', 'user_id', 'limit', 'media_count', 'fields' + * @throws \RuntimeException If config is not valid. + */ + private function fetchConfigParameters(): array + { + $hashtag_id = config('services.facebook.enpoints.ig_top_media.hashtag_id'); + $user_id = config('services.facebook.enpoints.ig_top_media.user_id'); + $limit = config('services.facebook.enpoints.ig_top_media.limit'); + $media_count = config('services.facebook.enpoints.ig_top_media.media_count'); + + if (empty($hashtag_id)) { + throw new \RuntimeException( + 'Missing services.facebook.enpoints.ig_top_media.hashtag_id, + FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_HASHTAGID' + ); + } + if (empty($user_id)) { + throw new \RuntimeException( + 'Missing services.facebook.enpoints.ig_top_media.user_id, + FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_USERID' + ); + } + if (empty($limit)) { + throw new \RuntimeException( + 'Missing services.facebook.enpoints.ig_top_media.limit, + FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_LIMIT' + ); + } + if (empty($media_count)) { + throw new \RuntimeException( + 'Missing services.facebook.enpoints.ig_top_media.media_count, + FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_MEDIACOUNT' + ); + } + $hashtag_id = \intval($hashtag_id); + $user_id = \intval($user_id); + $limit = \intval($limit); + $media_count = \intval($media_count); + return \compact( + 'hashtag_id', + 'user_id', + 'limit', + 'media_count' + ); + } } diff --git a/app/Lib/InstagramApiFetcher.php b/app/Lib/InstagramApiFetcher.php index ea381c0..837c3fc 100644 --- a/app/Lib/InstagramApiFetcher.php +++ b/app/Lib/InstagramApiFetcher.php @@ -13,6 +13,17 @@ use Psr\Http\Message\UriInterface; */ class InstagramApiFetcher { + /** + * Top media fields to Fetch + * + * @var string[] + */ + private $topMediaFields = [ + 'id', 'media_type', 'like_count', + 'comments_count', 'permalink', + 'timestamp', 'caption' + ]; + /** * InstagramTopMediaList * @@ -41,17 +52,15 @@ class InstagramApiFetcher * @param integer $user_id The IG user id we want to fetch. * @param \Illuminate\Database\Eloquent\Collection<\App\Models\InstaMediaExclude> $excludedPosts The IG user id we want to fetch. * @param \Illuminate\Database\Eloquent\Collection<\App\Models\InstaMediaExclude> $excludedUsers The IG user id we want to fetch. - * @param array $fields The fields to retrieve for each posts. * @param integer $limit The limit of how many post should be fetched. * @param integer $media_count How many media we want to fetch. - * @return \Illuminate\Support\Collection + * @return \Illuminate\Support\Collection */ public function fetchHashtagTopMediaList( int $hashtag_id, int $user_id, Collection $excludedPosts, Collection $excludedUsers, - array $fields, int $limit = 50, int $media_count = 30 ): SupportCollection { @@ -60,7 +69,7 @@ class InstagramApiFetcher $path = \sprintf('%s/top_media', $hashtag_id); $parameters = [ 'user_id' => $user_id, - 'fields' => \implode(',', $fields), + 'fields' => \implode(',', $this->topMediaFields), 'limit' => $limit ]; @@ -103,7 +112,7 @@ class InstagramApiFetcher return null; } $count++; - return [ + return (object)[ 'insta_id' => $ig_media->id, 'type' => $ig_media->media_type, 'permalink' => $ig_media->permalink, diff --git a/config/services.php b/config/services.php index c3ce072..cef31a6 100644 --- a/config/services.php +++ b/config/services.php @@ -37,11 +37,6 @@ return [ 'graph_access_token' => env('FACEBOOK_GRAPH_ACCESS_TOKEN'), 'enpoints' => [ 'ig_top_media' => [ - 'fields' => env('FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_FIELDS', collect([ - 'id', 'media_type', 'like_count', - 'comments_count', 'permalink', - 'timestamp', 'caption' - ])->join(',')), 'hashtag_id' => env('FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_HASHTAGID'), 'user_id' => env('FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_USERID'), 'limit' => env('FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_LIMIT', 25),