diff --git a/core/Analytic/RangeAnalytic.php b/core/Analytic/RangeAnalytic.php index a4d5ef6..c4a0f92 100644 --- a/core/Analytic/RangeAnalytic.php +++ b/core/Analytic/RangeAnalytic.php @@ -2,9 +2,9 @@ namespace App\Core\Analytic; +use App\Core\Entity\Site\Node; use App\Core\Repository\Analytic\RefererRepositoryQuery; use App\Core\Repository\Analytic\ViewRepositoryQuery; -use App\Core\Entity\Site\Node; /** * class RangeAnalytic. @@ -15,6 +15,11 @@ class RangeAnalytic { protected ViewRepositoryQuery $viewQuery; protected RefererRepositoryQuery $refererQuery; + protected ?Node $node; + protected ?\DateTime $from; + protected ?\DateTime $to; + protected bool $reload = true; + protected array $cache = []; public function __construct(ViewRepositoryQuery $viewQuery, RefererRepositoryQuery $refererQuery) { @@ -22,27 +27,22 @@ class RangeAnalytic $this->refererQuery = $refererQuery; } - public function getViews(\DateTime $from, \DateTime $to, Node $node): array + public function getViews(): array { - $entities = $this->viewQuery->create() - ->andWhere('.date >= :from') - ->andWhere('.date <= :to') - ->andWhere('.node = :node') - ->orderBy('.date') - ->setParameters([ - ':from' => $from, - ':to' => $to, - ':node' => $node->getId(), - ]) - ->find() - ; + $entities = $this->getEntities('view'); + $this->reload = false; - $diff = $from->diff($to); + if ($entities) { + $first = $entities[0]; + $last = $entities[count($entities) - 1]; - if ($diff->days >= 365) { - $format = 'Y-m'; - } else { - $format = 'Y-m-d'; + $diff = $first->getDate()->diff($last->getDate()); + + if ($diff->days >= 90) { + $format = 'Y-m'; + } else { + $format = 'Y-m-d'; + } } $datas = []; @@ -60,20 +60,10 @@ class RangeAnalytic return $datas; } - public function getPathViews(\DateTime $from, \DateTime $to, Node $node): array + public function getPathViews(): array { - $entities = $this->viewQuery->create() - ->andWhere('.date >= :from') - ->andWhere('.date <= :to') - ->andWhere('.node = :node') - ->orderBy('.date') - ->setParameters([ - ':from' => $from, - ':to' => $to, - ':node' => $node->getId(), - ]) - ->find() - ; + $entities = $this->getEntities('view'); + $this->reload = false; $datas = []; @@ -90,20 +80,10 @@ class RangeAnalytic return $datas; } - public function getReferers(\DateTime $from, \DateTime $to, Node $node): array + public function getReferers(): array { - $entities = $this->refererQuery->create() - ->andWhere('.date >= :from') - ->andWhere('.date <= :to') - ->andWhere('.node = :node') - ->orderBy('.date') - ->setParameters([ - ':from' => $from, - ':to' => $to, - ':node' => $node->getId(), - ]) - ->find() - ; + $entities = $this->getEntities('referer'); + $this->reload = false; $datas = []; @@ -130,4 +110,61 @@ class RangeAnalytic return $datas; } + + public function setDateRange(?\DateTime $from, ?\DateTime $to): self + { + $this->from = $from; + $this->to = $to; + $this->reload = true; + + return $this; + } + + public function setNode(?Node $node): self + { + $this->node = $node; + $this->reload = true; + + return $this; + } + + protected function getEntities(string $type): array + { + if ('view' === $type) { + $query = $this->viewQuery->create(); + } elseif ('referer' === $type) { + $query = $this->refererQuery->create(); + } else { + throw new \InvalidArgumentException('Invalid type'); + } + + if (!$this->reload && isset($this->cache[$type])) { + return $this->cache[$type]; + } + + if (null !== $this->from) { + $query + ->andWhere('.date >= :from') + ->setParameter(':from', $this->from) + ; + } + + if (null !== $this->to) { + $query + ->andWhere('.date <= :to') + ->setParameter(':to', $this->to) + ; + } + + if (null !== $this->node) { + $query + ->andWhere('.node = :node') + ->setParameter(':node', $this->node->getId()) + ; + } + + $this->cache[$type] = $query->orderBy('.date')->find(); + + return $this->cache[$type]; + } } diff --git a/core/Controller/Analytic/AnalyticController.php b/core/Controller/Analytic/AnalyticController.php index 83b9640..8a0156b 100644 --- a/core/Controller/Analytic/AnalyticController.php +++ b/core/Controller/Analytic/AnalyticController.php @@ -2,11 +2,11 @@ namespace App\Core\Controller\Analytic; +use App\Core\Analytic\RangeAnalytic; +use App\Core\Entity\Site\Node; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -use App\Core\Entity\Site\Node; -use App\Core\Analytic\RangeAnalytic; /** * @Route("/admin/analytic") @@ -22,15 +22,16 @@ class AnalyticController extends AbstractController throw $this->createNotFoundException(); } - $views = $rangeAnalytic->getViews(new \DateTime('now - '.$range), new \DateTime(), $node); - $pathViews = $rangeAnalytic->getPathViews(new \DateTime('now - '.$range), new \DateTime(), $node); - $referers = $rangeAnalytic->getReferers(new \DateTime('now - '.$range), new \DateTime(), $node); + $rangeAnalytic + ->setDateRange(new \DateTime('now - '.$range), new \DateTime()) + ->setNode($node) + ; return $this->render('@Core/analytic/stats.html.twig', [ 'range' => $range, - 'views' => $views, - 'pathViews' => $pathViews, - 'referers' => $referers, + 'views' => $rangeAnalytic->getViews(), + 'pathViews' => $rangeAnalytic->getPathViews(), + 'referers' => $rangeAnalytic->getReferers(), 'node' => $node, ]); }