Files
animethemes-server/app/Http/Middleware/Models/RecordView.php
T
paranarimasuandGitHub 088ea287c5 feat: initial migration to pennant features for feature flags and global site config [incremental] (#573)
* feat: initial commit for feature management

* style: fix StyleCI findings

* fix(api): prohibit features of nonnull scope from API & fix(admin): wrong translate key

* style: fix Static Analysis error
2023-04-23 18:54:34 -05:00

42 lines
977 B
PHP

<?php
declare(strict_types=1);
namespace App\Http\Middleware\Models;
use App\Constants\FeatureConstants;
use Closure;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use Illuminate\Http\Request;
use Laravel\Pennant\Feature;
use RuntimeException;
/**
* Class RecordView.
*/
class RecordView
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure(Request): mixed $next
* @param string $modelKey
* @return mixed
*/
public function handle(Request $request, Closure $next, string $modelKey): mixed
{
$model = $request->route($modelKey);
if (! $model instanceof Viewable) {
throw new RuntimeException('record_view should only be configured for viewable models');
}
if (Feature::active(FeatureConstants::ALLOW_VIEW_RECORDING)) {
views($model)->cooldown(now()->addMinutes(5))->record();
}
return $next($request);
}
}