99 lines
2.9 KiB
PHP
99 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\OpInsta\InstaMedia;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Illuminate\View\View;
|
|
|
|
class OpInstaController extends Controller
|
|
{
|
|
/**
|
|
* Show OpInsta page
|
|
*
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function index(): View
|
|
{
|
|
$this->assertConfigIsValid();
|
|
$has_started = $this->hasStarted();
|
|
|
|
$instaMediaList = InstaMedia::query()->orderBy('id')->get()
|
|
->map(function (InstaMedia $media) {
|
|
$path = \explode('/', \rtrim(\parse_url(
|
|
$media->permalink,
|
|
\PHP_URL_PATH
|
|
), '/'));
|
|
|
|
$media->pictures = $this->fetchPicture(\collect($path)->last());
|
|
return $media;
|
|
});
|
|
|
|
return \view('front.opinsta.index', \compact('instaMediaList', 'has_started'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Assert config is valid
|
|
*
|
|
* @return void
|
|
* @throws \ErrorException If config is invalid.
|
|
*/
|
|
private function assertConfigIsValid(): void
|
|
{
|
|
try {
|
|
$start = config('opinsta.start');
|
|
$end = config('opinsta.end');
|
|
|
|
Validator::validate([
|
|
'start' => $start,
|
|
'end' => $end,
|
|
], [
|
|
'start' => 'date_format:d-m-Y|before:end',
|
|
'end' => 'date_format:d-m-Y|after:start',
|
|
]);
|
|
} catch (ValidationException $e) {
|
|
$message = \collect($e->errors())->map(function ($el) {
|
|
return \collect($el)->implode(', ');
|
|
})->implode(', ');
|
|
throw new \ErrorException("Op install config is invalid, {$message}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Le jeu a démaré ?
|
|
*
|
|
* Démarre à la date de départ (0h00) et s'arrête à la date de fin (23h59)
|
|
*
|
|
* @return boolean
|
|
*/
|
|
private function hasStarted(): bool
|
|
{
|
|
$start = Carbon::createFromFormat('d-m-Y', config('opinsta.start'), 'Europe/Paris')->setTime(0, 0);
|
|
$end = Carbon::createFromFormat('d-m-Y', config('opinsta.end'), 'Europe/Paris')->setTime(0, 0);
|
|
$now = Carbon::now('Europe/Paris')->setTime(0, 0);
|
|
return $now->isBetween($start, $end);
|
|
}
|
|
|
|
/**
|
|
* Fetch Media Picture Object
|
|
*
|
|
* @param string $ig_media_hash
|
|
* @return array<integer,string[]>
|
|
*/
|
|
private function fetchPicture(string $ig_media_hash): array
|
|
{
|
|
return Cache::remember(
|
|
"media_picture_ajax_fetch{$ig_media_hash}",
|
|
// * 3H cache
|
|
3 * 60 * 60,
|
|
function () use ($ig_media_hash) {
|
|
return InstaMedia::fetchInstagramMediaPictures($ig_media_hash);
|
|
}
|
|
);
|
|
}
|
|
}
|