<commit_before><?php

namespace Dark\RedisListBundle\Pagination;

use Predis\Client;
use Symfony\Component\HttpFoundation\Request;
use Dark\RedisListBundle\Collector\Collector;

class Pagination extends Iterator
{
    private $params;
    private $client;
    private $collector;
    private $templating;
    private $template;
    private $request;

    public function __construct(Client $client, Collector $collector, $templating, $request, $template)
    {
        $this->client = $client;
        $this->collector = $collector;
        $this->templating = $templating;
        $this->request = $request;
        $this->template = $template;
        $this->params = array();
    }

    public function paginate($hashName, $page = 1, $perPage = 15, $pageRange = 10, $route = null)
    {
        $count = $this->client->hlen($hashName);
        $from = $count - $page * $perPage;
        $to = $from + $pageRange;

        $range = range($from, $to);
        $elements = $this->client->hmget($hashName, $range);

        if (!is_null($route)) {
            $this->params['route'] = $route;
        } else {
            $this->params['route'] = $this->request->get("_route");
        }

        $this->params['current'] = $page;
        $this->params['pageRange'] = $pageRange;
        $this->params['pageCount'] = round($count / $perPage);
        $this->elements = $this->collector->process($elements);
    }

    public function render()
    {
        $params = $this->params;

        if ($params['current'] > 1) {
            $params['prev'] = $params['current'] - 1;
        }
        if ($params['current'] < $params['pageCount']) {
            $params['next'] = $params['current'] + 1;
        }

        $parts = round($params['pageRange'] / 2, 0, PHP_ROUND_HALF_EVEN);

        $firstPage = $params['current'] - $parts;
        $lastPage = $params['current'] + $parts;

        if ($firstPage <= 0) {
            $lastPage += abs($firstPage) + 1;
            $firstPage = 1;
        } elseif ($params['pageCount'] - $lastPage < $parts) {
            $firstPage -= $lastPage - $params['pageCount'];
        }

        for ($i = $firstPage; $i < $lastPage; $i++) {
            $params['pagesInRange'][] = $i;
        }

        return $this->templating->render($this->template, $params);
    }
}<commit_msg>Bad math again<commit_after><?php

namespace Dark\RedisListBundle\Pagination;

use Predis\Client;
use Symfony\Component\HttpFoundation\Request;
use Dark\RedisListBundle\Collector\Collector;

class Pagination extends Iterator
{
    private $params;
    private $client;
    private $collector;
    private $templating;
    private $template;
    private $request;

    public function __construct(Client $client, Collector $collector, $templating, $request, $template)
    {
        $this->client = $client;
        $this->collector = $collector;
        $this->templating = $templating;
        $this->request = $request;
        $this->template = $template;
        $this->params = array();
    }

    public function paginate($hashName, $page = 1, $perPage = 15, $pageRange = 10, $route = null)
    {
        $count = $this->client->hlen($hashName);
        $from = $count - $page * $perPage;
        $to = $from + $pageRange;

        $range = range($from, $to);
        $elements = $this->client->hmget($hashName, $range);

        if (!is_null($route)) {
            $this->params['route'] = $route;
        } else {
            $this->params['route'] = $this->request->get("_route");
        }

        $this->params['current'] = $page;
        $this->params['pageRange'] = $pageRange;
        $this->params['pageCount'] = round($count / $perPage);
        $this->params['perPage'] = $perPage;
        $this->elements = $this->collector->process($elements);
    }

    public function render()
    {
        $params = $this->params;

        if ($params['current'] > 1) {
            $params['prev'] = $params['current'] - 1;
        }
        if ($params['current'] < $params['pageCount']) {
            $params['next'] = $params['current'] + 1;
        }

        $parts = round($params['pageRange'] / 2, 0, PHP_ROUND_HALF_EVEN);

        $firstPage = $params['current'] - $parts;
        $lastPage = ($params['current'] + $parts) > $params['pageCount'] ? $params['pageCount'] : $params['current'] + $parts;

        if ($firstPage <= 0) {
            $lastPage += abs($firstPage);
            $firstPage = 1;
        } elseif ($params['pageCount'] - $lastPage < $parts) {
            $firstPage -= $params['current'] + $parts - $lastPage;
        }

        for ($i = $firstPage; $i <= $lastPage; $i++) {
            $params['pagesInRange'][] = $i;
        }

        return $this->templating->render($this->template, $params);
    }
}
{'fraction_non_alphanumeric': 0.17689075630252102, 'fraction_numerical': 0.005252100840336135, 'mean_word_length': 2.4425162689804774, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 55, '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': '739901', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1462, 'n_words': 292}