diff --git a/app/Enums/ExcludeTypeEnum.php b/app/Enums/ExcludeTypeEnum.php index 657083f..7d176b0 100644 --- a/app/Enums/ExcludeTypeEnum.php +++ b/app/Enums/ExcludeTypeEnum.php @@ -4,7 +4,13 @@ namespace App\Enums; enum ExcludeTypeEnum : Int { + /** + * Ig post defined by its id + */ case post = 1; + /** + * Ig user defined by its username + */ case user = 2; /** diff --git a/app/Jobs/InstaGetTopMediasJob.php b/app/Jobs/InstaGetTopMediasJob.php index 1b475b2..a3eee32 100644 --- a/app/Jobs/InstaGetTopMediasJob.php +++ b/app/Jobs/InstaGetTopMediasJob.php @@ -4,7 +4,7 @@ namespace App\Jobs; use App\Enums\ExcludeTypeEnum; use App\Models\InstaMedia; -use App\Models\InstaMediaExclude; +use App\Models\InstaMediaExclusion; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Facades\DB; @@ -48,10 +48,10 @@ class InstaGetTopMediasJob implements ShouldQueue */ \extract($this->fetchConfigParameters()); - /** @var \Illuminate\Database\Eloquent\Collection */ - $excludePosts = InstaMediaExclude::query()->where('type', ExcludeTypeEnum::post)->get(); - /** @var \Illuminate\Database\Eloquent\Collection */ - $excludeUsers = InstaMediaExclude::query()->where('type', ExcludeTypeEnum::user)->get(); + /** @var \Illuminate\Database\Eloquent\Collection */ + $excludePosts = InstaMediaExclusion::query()->where('type', ExcludeTypeEnum::post)->get(); + /** @var \Illuminate\Database\Eloquent\Collection */ + $excludeUsers = InstaMediaExclusion::query()->where('type', ExcludeTypeEnum::user)->get(); /** @var \App\Lib\Instagram\ApiFetcher */ $igFetcherClient = \app('Instagram'); diff --git a/app/Lib/Instagram/ApiFetcher.php b/app/Lib/Instagram/ApiFetcher.php index efe709a..34b2f03 100644 --- a/app/Lib/Instagram/ApiFetcher.php +++ b/app/Lib/Instagram/ApiFetcher.php @@ -52,12 +52,12 @@ class ApiFetcher * @phpcs:disable Generic.Files.LineLength.TooLong * @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint * - * @param integer $hashtag_id The IG hashtag Id we whould target. - * @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 integer $limit The limit of how many post should be fetched. - * @param integer $media_count How many media we want to fetch. + * @param integer $hashtag_id The IG hashtag Id we whould target. + * @param integer $user_id The IG user id we want to fetch. + * @param \Illuminate\Database\Eloquent\Collection<\App\Models\InstaMediaExclusion> $excludedPosts The IG user id we want to fetch. + * @param \Illuminate\Database\Eloquent\Collection<\App\Models\InstaMediaExclusion> $excludedUsers The IG user id we want to fetch. + * @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 * @throws \RuntimeException If a call error or a response parsing error occurs. */ diff --git a/app/Lib/Instagram/Objects/TopMedia.php b/app/Lib/Instagram/Objects/TopMedia.php index 59b1cee..1bda5a6 100644 --- a/app/Lib/Instagram/Objects/TopMedia.php +++ b/app/Lib/Instagram/Objects/TopMedia.php @@ -2,7 +2,7 @@ namespace App\Lib\Instagram\Objects; -use stdClass; +use Illuminate\Support\Carbon; /** * @property string $insta_id @@ -16,7 +16,7 @@ use stdClass; * * @package App\Lib\Instagram\Objects */ -class TopMedia extends stdClass +class TopMedia extends \stdClass { /** * Set this object data from Ig api data @@ -29,7 +29,7 @@ class TopMedia extends stdClass $this->insta_id = $ig_media->id; $this->type = $ig_media->media_type; $this->permalink = $ig_media->permalink; - $this->timestamp = $ig_media->timestamp; + $this->timestamp = Carbon::parse($ig_media->timestamp); $this->comments_count = \intval($ig_media->comments_count); $this->like_count = \optional($ig_media)->like_count ? \intval($ig_media->like_count) : null; $this->caption = $ig_media->caption; diff --git a/app/Models/InstaMedia.php b/app/Models/InstaMedia.php index 9190bf7..a05b281 100644 --- a/app/Models/InstaMedia.php +++ b/app/Models/InstaMedia.php @@ -5,17 +5,21 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +/** + * @property integer $insta_id A unique instagram id that can be used for api calls. + * @property string $type The instagram object type, commonly IMAGE or CAROUSEL_ALBUM. + * @property string $username The instagram user name, scrapped using OG description. + * @property string $caption The object text content. + * @property string $permalink The uri where is stored the Instagram object. + * @property \Carbon\Carbon $timestamp UTC timestamp from instagram api. + * @property integer $comments_count The number of comments. + * @property null|integer $like_count The number of likes. + * @package App\Models + */ class InstaMedia extends Model { use HasFactory; - /** - * $table - * - * @var string - */ - protected $table = "insta_medias"; - /** * $fillable * @@ -31,4 +35,13 @@ class InstaMedia extends Model 'username', 'caption' ]; + + /** + * The attributes that should be cast. + * + * @var string[] + */ + protected $casts = [ + 'timestamp' => 'immutable_datetime' + ]; } diff --git a/app/Models/InstaMediaExclude.php b/app/Models/InstaMediaExclusion.php similarity index 89% rename from app/Models/InstaMediaExclude.php rename to app/Models/InstaMediaExclusion.php index 6f9ac1c..4726c86 100644 --- a/app/Models/InstaMediaExclude.php +++ b/app/Models/InstaMediaExclusion.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use App\Enums\ExcludeTypeEnum; -class InstaMediaExclude extends Model +class InstaMediaExclusion extends Model { use HasFactory; diff --git a/database/migrations/2023_02_10_103952_create_insta_media_table.php b/database/migrations/2023_02_10_103952_create_insta_media_table.php new file mode 100644 index 0000000..627dd10 --- /dev/null +++ b/database/migrations/2023_02_10_103952_create_insta_media_table.php @@ -0,0 +1,40 @@ +id(); + $table->unsignedBigInteger("insta_id")->unique() + ->comment('A unique instagram id that can be used for api calls'); + $table->string("type")->comment('The instagram object type, commonly IMAGE or CAROUSEL_ALBUM'); + $table->string('username')->comment('The instagram user name, scrapped using OG description'); + $table->text('caption')->comment('The object text content'); + $table->string("permalink")->comment('The uri where is stored the Instagram object'); + $table->timestamp('timestamp')->comment('UTC timestamp from instagram api'); + $table->unsignedInteger("comments_count"); + $table->unsignedInteger("like_count")->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('insta_medias'); + } +}; diff --git a/database/migrations/2023_02_10_103952_create_insta_medias_table.php b/database/migrations/2023_02_10_103952_create_insta_medias_table.php deleted file mode 100644 index 2d6dd66..0000000 --- a/database/migrations/2023_02_10_103952_create_insta_medias_table.php +++ /dev/null @@ -1,39 +0,0 @@ -id(); - $table->string("insta_id")->unique(); - $table->string("type"); - $table->string("permalink"); - $table->string("timestamp"); - $table->bigInteger("comments_count"); - $table->bigInteger("like_count")->nullable(); - $table->string('username'); - $table->text('caption'); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('insta_medias'); - } -}; diff --git a/database/migrations/2023_02_10_122709_create_insta_media_excludes_table.php b/database/migrations/2023_02_10_122709_create_insta_media_exclusions_table.php similarity index 67% rename from database/migrations/2023_02_10_122709_create_insta_media_excludes_table.php rename to database/migrations/2023_02_10_122709_create_insta_media_exclusions_table.php index de78dac..09fad22 100644 --- a/database/migrations/2023_02_10_122709_create_insta_media_excludes_table.php +++ b/database/migrations/2023_02_10_122709_create_insta_media_exclusions_table.php @@ -13,10 +13,10 @@ return new class extends Migration */ public function up() { - Schema::create('insta_media_excludes', function (Blueprint $table) { + Schema::create('insta_media_exclusions', function (Blueprint $table) { $table->id(); - $table->unsignedTinyInteger('type')->comment("Enum 1 = post, 2 = user"); - $table->string('value'); + $table->unsignedTinyInteger('type')->comment("ExcludeTypeEnum 1 = post, 2 = user"); + $table->string('value')->comment('Post: ig id, User : ig username'); $table->timestamps(); }); }