diff --git a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php index d0ddd50de..cfd6dabd8 100644 --- a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php +++ b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php @@ -152,6 +152,12 @@ class AnnotationRepository extends ServiceEntityRepository ->getResult(); } + public function getCountBuilderByUser($userId = null) + { + return $this->createQueryBuilder('e') + ->andWhere('e.user = :userId')->setParameter('userId', $userId); + } + /** * Return a query builder to used by other getBuilderFor* method. * diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 3e0b4effd..2520e0f87 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -226,21 +226,6 @@ class EntryRepository extends ServiceEntityRepository ; } - /** - * Retrieve entries with annotations count for a user. - * - * @param int $userId - * - * @return QueryBuilder - */ - public function getCountBuilderForAnnotationsByUser($userId) - { - return $this - ->getQueryBuilderByUser($userId) - ->innerJoin('e.annotations', 'a') - ; - } - /** * Retrieve untagged entries for a user. * diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index a31cacf0e..3d4a3a7b2 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -8,6 +8,7 @@ use Twig\Extension\AbstractExtension; use Twig\Extension\GlobalsInterface; use Twig\TwigFilter; use Twig\TwigFunction; +use Wallabag\AnnotationBundle\Repository\AnnotationRepository; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; use Wallabag\UserBundle\Entity\User; @@ -16,14 +17,16 @@ class WallabagExtension extends AbstractExtension implements GlobalsInterface { private $tokenStorage; private $entryRepository; + private $annotationRepository; private $tagRepository; private $lifeTime; private $translator; private $projectDir; - public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator, string $projectDir) + public function __construct(EntryRepository $entryRepository, AnnotationRepository $annotationRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator, string $projectDir) { $this->entryRepository = $entryRepository; + $this->annotationRepository = $annotationRepository; $this->tagRepository = $tagRepository; $this->tokenStorage = $tokenStorage; $this->lifeTime = $lifeTime; @@ -88,28 +91,25 @@ class WallabagExtension extends AbstractExtension implements GlobalsInterface switch ($type) { case 'starred': - $qb = $this->entryRepository->getCountBuilderForStarredByUser($user->getId()); + $qb = $this->entryRepository->getCountBuilderForStarredByUser($user->getId())->select('COUNT(e.id)'); break; case 'archive': - $qb = $this->entryRepository->getCountBuilderForArchiveByUser($user->getId()); + $qb = $this->entryRepository->getCountBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)'); break; case 'unread': - $qb = $this->entryRepository->getCountBuilderForUnreadByUser($user->getId()); + $qb = $this->entryRepository->getCountBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)'); break; case 'annotated': - $qb = $this->entryRepository->getCountBuilderForAnnotationsByUser($user->getId()); + $qb = $this->annotationRepository->getCountBuilderByUser($user->getId())->select('COUNT(DISTINCT e.entry)'); break; case 'all': - $qb = $this->entryRepository->getCountBuilderForAllByUser($user->getId()); + $qb = $this->entryRepository->getCountBuilderForAllByUser($user->getId())->select('COUNT(e.id)'); break; default: throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); } - $query = $qb - ->select('COUNT(e.id)') - ->getQuery(); - + $query = $qb->getQuery(); $query->useQueryCache(true); $query->enableResultCache($this->lifeTime); diff --git a/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php b/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php index 824f7a54c..482403f72 100644 --- a/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php +++ b/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php @@ -5,6 +5,7 @@ namespace Tests\Wallabag\CoreBundle\Twig; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Contracts\Translation\TranslatorInterface; +use Wallabag\AnnotationBundle\Repository\AnnotationRepository; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; use Wallabag\CoreBundle\Twig\WallabagExtension; @@ -17,6 +18,10 @@ class WallabagExtensionTest extends TestCase ->disableOriginalConstructor() ->getMock(); + $annotationRepository = $this->getMockBuilder(AnnotationRepository::class) + ->disableOriginalConstructor() + ->getMock(); + $tagRepository = $this->getMockBuilder(TagRepository::class) ->disableOriginalConstructor() ->getMock(); @@ -29,7 +34,7 @@ class WallabagExtensionTest extends TestCase ->disableOriginalConstructor() ->getMock(); - $extension = new WallabagExtension($entryRepository, $tagRepository, $tokenStorage, 0, $translator, ''); + $extension = new WallabagExtension($entryRepository, $annotationRepository, $tagRepository, $tokenStorage, 0, $translator, ''); $this->assertSame('lemonde.fr', $extension->removeWww('www.lemonde.fr')); $this->assertSame('lemonde.fr', $extension->removeWww('lemonde.fr')); @@ -42,6 +47,10 @@ class WallabagExtensionTest extends TestCase ->disableOriginalConstructor() ->getMock(); + $annotationRepository = $this->getMockBuilder(AnnotationRepository::class) + ->disableOriginalConstructor() + ->getMock(); + $tagRepository = $this->getMockBuilder(TagRepository::class) ->disableOriginalConstructor() ->getMock(); @@ -54,7 +63,7 @@ class WallabagExtensionTest extends TestCase ->disableOriginalConstructor() ->getMock(); - $extension = new WallabagExtension($entryRepository, $tagRepository, $tokenStorage, 0, $translator, ''); + $extension = new WallabagExtension($entryRepository, $annotationRepository, $tagRepository, $tokenStorage, 0, $translator, ''); $this->assertSame('lemonde.fr', $extension->removeScheme('lemonde.fr')); $this->assertSame('gist.github.com', $extension->removeScheme('gist.github.com')); @@ -67,6 +76,10 @@ class WallabagExtensionTest extends TestCase ->disableOriginalConstructor() ->getMock(); + $annotationRepository = $this->getMockBuilder(AnnotationRepository::class) + ->disableOriginalConstructor() + ->getMock(); + $tagRepository = $this->getMockBuilder(TagRepository::class) ->disableOriginalConstructor() ->getMock(); @@ -79,7 +92,7 @@ class WallabagExtensionTest extends TestCase ->disableOriginalConstructor() ->getMock(); - $extension = new WallabagExtension($entryRepository, $tagRepository, $tokenStorage, 0, $translator, ''); + $extension = new WallabagExtension($entryRepository, $annotationRepository, $tagRepository, $tokenStorage, 0, $translator, ''); $this->assertSame('lemonde.fr', $extension->removeSchemeAndWww('www.lemonde.fr')); $this->assertSame('lemonde.fr', $extension->removeSchemeAndWww('http://lemonde.fr'));