<commit_before><?php

namespace Canvas\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ViewThrottle
{
    /**
     * Handle the incoming request.
     *
     * @param $request
     * @param $next
     * @return Response
     */
    public function handle(Request $request, Closure $next)
    {
        $posts = $this->getViewedPosts();

        if (! is_null($posts)) {
            $posts = $this->cleanExpiredViews($posts);
            $this->storeInSession($posts);
        }

        return $next($request);
    }

    /**
     * Get the viewed posts stored in the current session.
     *
     * @return array|null
     */
    private function getViewedPosts()
    {
        return session()->get('viewed_posts', null);
    }

    /**
     * Clean out the expired posts from the session.
     *
     * @param array $posts
     * @return array
     */
    private function cleanExpiredViews(array $posts): array
    {
        $time = time();

        $throttleTime = 3600;

        return array_filter($posts, function ($timestamp) use ($time, $throttleTime) {
            return ($timestamp + $throttleTime) > $time;
        });
    }

    /**
     * Store posts in the current session.
     *
     * @param $posts<commit_msg>Set a constant expiration<commit_after><?php

namespace Canvas\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ViewThrottle
{
    /**
     * Set the view to expire in 1 hour.
     *
     * @const int
     */
    const VIEW_EXPIRES = 3600;

    /**
     * Handle the incoming request.
     *
     * @param $request
     * @param $next
     * @return Response
     */
    public function handle(Request $request, Closure $next)
    {
        $posts = $this->getViewedPosts();

        if (! is_null($posts)) {
            $posts = $this->cleanExpiredViews($posts);
            $this->storeInSession($posts);
        }

        return $next($request);
    }

    /**
     * Get the viewed posts stored in the current session.
     *
     * @return array|null
     */
    private function getViewedPosts()
    {
        return session()->get('viewed_posts', null);
    }

    /**
     * Clean out the expired posts from the session.
     *
     * @param array $posts
     * @return array
     */
    private function cleanExpiredViews(array $posts): array
    {
        $time = time();

        $throttleTime = self::VIEW_EXPIRES;

        return array_filter($posts, function ($timestamp) use ($time, $throttleTime) {
            return ($timestamp + $throttleTime) > $time;
        });
    }

    /**
     * Store posts in the current session.
     *
     * @param $posts
{'fraction_non_alphanumeric': 0.12388724035608309, 'fraction_numerical': 0.0033382789317507417, 'mean_word_length': 2.05436013590034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4423637', 'n_tokens_mistral': 817, 'n_tokens_neox': 796, 'n_words': 203}