<commit_before><?php

namespace Oro\Bundle\DataGridBundle\Extension\Pager;

use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
use Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource;
use Oro\Bundle\DataGridBundle\Datasource\DatasourceInterface;
use Oro\Bundle\DataGridBundle\Extension\Mode\ModeExtension;
use Oro\Bundle\DataGridBundle\Extension\Toolbar\ToolbarExtension;

/**
 * Responsibility of this extension is to apply pagination on query for ORM datasource
 */
class OrmPagerExtension extends AbstractPagerExtension
{
    /**
     * {@inheritDoc}
     */
    public function isApplicable(DatagridConfiguration $config)
    {
        return
            $config->getDatasourceType() === OrmDatasource::TYPE
            && !$this->getOr(PagerInterface::DISABLED_PARAM, false)
            && !$config->offsetGetByPath(ToolbarExtension::TOOLBAR_PAGINATION_HIDE_OPTION_PATH, false);
    }

    /**
     * {@inheritDoc}
     */
    public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
    {
        $defaultPerPage = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH, 10);

        /** @var $datasource OrmDatasource */
        if ($datasource->getCountQb()) {
            $this->pager->setCountQb($datasource->getCountQb());
        }
        $this->pager->setQueryBuilder($datasource->getQueryBuilder());
        $this->pager->setSkipAclCheck($config->isDatasourceSkipAclApply());
        $this->pager->setAclPermission($config->getDatasourceAclApplyPermission());
        $this->pager->setSkipCountWalker(
            $config->offsetGetByPath(DatagridConfiguration::DATASOURCE_SKIP_COUNT_WALKER_PATH)
        );

        $onePage = $config->offsetGetByPath(ToolbarExtension::PAGER_ONE_PAGE_OPTION_PATH, false);
        $mode = $config->offsetGetByPath(ModeExtension::MODE_OPTION_PATH);
        $perPageLimit = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH);
        $defaultPerPage = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH, 10);
        $perPageCount = $this->getOr(PagerInterface::PER_PAGE_PARAM, $defaultPerPage);

        if ((!$perPageLimit && $onePage) || $mode === ModeExtension::MODE_CLIENT) {
            // no restrictions applied
            $this->pager->setPage(0);
            $this->pager->setMaxPerPage(0);
        } elseif ($onePage && $perPageLimit) {
            // one page with limit
            $this->pager->setPage(0);
            if ($config->offsetGetByPath(ToolbarExtension::TOOLBAR_HIDE_OPTION_PATH)) {
                $this->pager->adjustTotalCount($perPageCount);
            }
            $this->pager->setMaxPerPage($perPageCount);
        } else {<commit_msg>BAP-11695: Slow queries after login - merge from master<commit_after><?php

namespace Oro\Bundle\DataGridBundle\Extension\Pager;

use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
use Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource;
use Oro\Bundle\DataGridBundle\Datasource\DatasourceInterface;
use Oro\Bundle\DataGridBundle\Extension\Mode\ModeExtension;
use Oro\Bundle\DataGridBundle\Extension\Toolbar\ToolbarExtension;

/**
 * Responsibility of this extension is to apply pagination on query for ORM datasource
 */
class OrmPagerExtension extends AbstractPagerExtension
{
    /**
     * {@inheritDoc}
     */
    public function isApplicable(DatagridConfiguration $config)
    {
        return
            $config->getDatasourceType() === OrmDatasource::TYPE
            && !$this->getOr(PagerInterface::DISABLED_PARAM, false)
            && !$config->offsetGetByPath(ToolbarExtension::TOOLBAR_PAGINATION_HIDE_OPTION_PATH, false);
    }

    /**
     * {@inheritDoc}
     */
    public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
    {
        /** @var $datasource OrmDatasource */
        if ($datasource->getCountQb()) {
            $this->pager->setCountQb($datasource->getCountQb());
        }
        $this->pager->setQueryBuilder($datasource->getQueryBuilder());
        $this->pager->setSkipAclCheck($config->isDatasourceSkipAclApply());
        $this->pager->setAclPermission($config->getDatasourceAclApplyPermission());
        $this->pager->setSkipCountWalker(
            $config->offsetGetByPath(DatagridConfiguration::DATASOURCE_SKIP_COUNT_WALKER_PATH)
        );

        $onePage = $config->offsetGetByPath(ToolbarExtension::PAGER_ONE_PAGE_OPTION_PATH, false);
        $mode = $config->offsetGetByPath(ModeExtension::MODE_OPTION_PATH);
        $perPageLimit = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH);
        $defaultPerPage = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH, 10);
        $perPageCount = $this->getOr(PagerInterface::PER_PAGE_PARAM, $defaultPerPage);

        if ((!$perPageLimit && $onePage) || $mode === ModeExtension::MODE_CLIENT) {
            // no restrictions applied
            $this->pager->setPage(0);
            $this->pager->setMaxPerPage(0);
        } elseif ($onePage && $perPageLimit) {
            // one page with limit
            $this->pager->setPage(0);
            if ($config->offsetGetByPath(ToolbarExtension::TOOLBAR_HIDE_OPTION_PATH)) {
                $this->pager->adjustTotalCount($perPageCount);
            }
            $this->pager->setMaxPerPage($perPageCount);
        } else {
{'fraction_non_alphanumeric': 0.12378328741965106, 'fraction_numerical': 0.0031221303948576677, 'mean_word_length': 3.650725875320239, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, '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': '2892968', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1661, 'n_words': 202}