<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class NsfwScope implements Scope
{

    private function canNsfw(): bool
    {
        if( \Auth::guest() )
            return false;

        $user = \Auth::user();
        return $user->nsfw_content === 1;
    }

    /**
     * Apply the scope to a given Eloquent query builder.
     */
    public function apply(Builder $builder, Model $model): void
    {
        if( !$this->canNsfw() )
            $builder->where('nsfw', false );
    }
}
