Skip to content

Commit

Permalink
handle SonataAdminBundle Pager::nbresults deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Jan 9, 2021
1 parent f20ba51 commit bc850f9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doctrine/mongodb-odm": "^2.1",
"doctrine/mongodb-odm-bundle": "^4.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
"sonata-project/admin-bundle": "^3.84",
"sonata-project/admin-bundle": "^3.86",
"sonata-project/form-extensions": "^0.1 || ^1.4",
"symfony/config": "^4.4",
"symfony/dependency-injection": "^4.4",
Expand Down
103 changes: 95 additions & 8 deletions src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,71 @@ class Pager extends BasePager
{
protected $queryBuilder = null;

public function computeNbResult()
/**
* @var int
*/
private $resultsCount = 0;

public function countResults(): int
{
$countQuery = clone $this->getQuery();
// NEXT_MAJOR: just return "$this->resultsCount" directly.
$deprecatedCount = $this->getNbResults('sonata_deprecation_mute');

if (\count($this->getParameters()) > 0) {
$countQuery->setParameters($this->getParameters());
if ($deprecatedCount === $this->resultsCount) {
return $this->resultsCount;
}

return $countQuery->count()->getQuery()->execute();
@trigger_error(sprintf(
'Relying on the protected property "%s::$nbResults" and its getter/setter is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will fail 4.0. Use "countResults()" and "setResultsCount()" instead.',
self::class,
), E_USER_DEPRECATED);

return $deprecatedCount;
}

public function setResultsCount(int $count): void
{
$this->resultsCount = $count;
// NEXT_MAJOR: Remove this line.
$this->setNbResults($count, 'sonata_deprecation_mute');
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @return int
*/
public function getNbResults()
{
if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0. Use "countResults()" instead.',
__METHOD__,
), E_USER_DEPRECATED);
}

return $this->nbResults;
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @return int
*/
public function computeNbResult()
{
if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0.',
__METHOD__,
), E_USER_DEPRECATED);
}

return $this->computeResultsCount();
}

public function getResults()
Expand All @@ -50,7 +106,7 @@ public function init()
{
$this->resetIterator();

$this->setNbResults($this->computeNbResult());
$this->setResultsCount($this->computeNbResult('sonata_deprecation_mute'));

$this->getQuery()->setFirstResult(0);
$this->getQuery()->setMaxResults(0);
Expand All @@ -59,15 +115,46 @@ public function init()
$this->getQuery()->setParameters($this->getParameters());
}

if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) {
if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->countResults()) {
$this->setLastPage(0);
} else {
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();

$this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage()));
$this->setLastPage((int) ceil($this->countResults() / $this->getMaxPerPage()));

$this->getQuery()->setFirstResult($offset);
$this->getQuery()->setMaxResults($this->getMaxPerPage());
}
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @param int $nb
*/
protected function setNbResults($nb)
{
if ('sonata_deprecation_mute' !== (\func_get_args()[1] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0. Use "setResultsCount()" instead.',
__METHOD__,
), E_USER_DEPRECATED);
}

$this->nbResults = $nb;
$this->resultsCount = (int) $nb;
}

private function computeResultsCount(): int
{
$countQuery = clone $this->getQuery();

if (\count($this->getParameters()) > 0) {
$countQuery->setParameters($this->getParameters());
}

return $countQuery->count()->getQuery()->execute();
}
}

0 comments on commit bc850f9

Please sign in to comment.