ldc-legaulois/app/Models/OpInsta/InstaMedia.php
Jérémy Munsch 859b1f3625 Op Insta
2023-04-25 10:10:08 +00:00

91 lines
2.7 KiB
PHP

<?php
namespace App\Models\OpInsta;
use Illuminate\Database\Eloquent\Casts\AsStringable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @mixin \App\Lib\Instagram\Objects\TopMedia
*
* @package App\Models\OpInsta
*/
class InstaMedia extends Model
{
use HasFactory;
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'opinsta';
/**
* The attributes that should be cast.
*
* @var string[]
*/
protected $casts = [
'caption' => AsStringable::class,
'permalink' => AsStringable::class,
'timestamp' => 'immutable_datetime'
];
// * METHODS
/**
* Fetch Media picture using ig media hash
*
* @param string $ig_media_hash
* @return array
* @throws \RuntimeException If a call error or a response parsing error occurs.
*/
public static function fetchInstagramMediaPictures(string $ig_media_hash): array
{
/** @var \App\Lib\Instagram\ApiFetcher */
$igFetcherClient = \app('Instagram');
return $igFetcherClient->fetchMediaPicturesFromPostPermalink("https://www.instagram.com/p/{$ig_media_hash}/");
}
/**
* Fetch and check config elements
*
* @return array 'hashtag_id', 'user_id', 'media_count', 'fields'
* @throws \RuntimeException If config is not valid.
*/
private static 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');
$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($media_count)) {
throw new \RuntimeException(
'Missing services.facebook.enpoints.ig_top_media.media_count,
FACEBOOK_GRAPH_ENDPOINT_IG_TOPMEDIA_NBMEDIA'
);
}
$hashtag_id = \intval($hashtag_id);
$user_id = \intval($user_id);
$media_count = \intval($media_count);
return \compact(
'hashtag_id',
'user_id',
'media_count'
);
}
}