setRdapQueries($this->pool->getItem('stats.rdap_queries.count')->get() ?? 0) ->setDomainPurchased($this->pool->getItem('stats.domain.purchased')->get() ?? 0) ->setDomainPurchaseFailed($this->pool->getItem('stats.domain.purchase.failed')->get() ?? 0) ->setAlertSent($this->pool->getItem('stats.alert.sent')->get() ?? 0) ->setWatchlistCount( $this->getCachedItem('stats.watchlist.count', fn () => $this->watchListRepository->count() )) ->setDomainCount( $this->getCachedItem('stats.domain.count', fn () => $this->domainRepository->createQueryBuilder('d') ->join('d.tld', 't') ->select('t.tld tld') ->addSelect('COUNT(d.ldhName) AS domain') ->addGroupBy('t.tld') ->orderBy('domain', 'DESC') ->setMaxResults(5) ->getQuery()->getArrayResult()) ) ->setDomainCountTotal( $this->getCachedItem('stats.domain.total', fn () => $this->domainRepository->count() )); return $stats; } /** * @throws InvalidArgumentException */ private function getCachedItem(string $key, callable $getItemFunction) { $item = $this->pool->getItem($key); if (!$item->isHit() || $this->kernel->isDebug()) { $value = $getItemFunction(); $item ->set($value) ->expiresAfter(24 * 60 * 60); $this->pool->save($item); return $value; } else { return $item->get(); } } }