aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Twig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig')
-rw-r--r--src/Wallabag/CoreBundle/Twig/WallabagExtension.php63
1 files changed, 53 insertions, 10 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
index 974b86a9..3780b13e 100644
--- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
+++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
@@ -2,18 +2,24 @@
2 2
3namespace Wallabag\CoreBundle\Twig; 3namespace Wallabag\CoreBundle\Twig;
4 4
5use Doctrine\ORM\Query;
5use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 6use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
6use Wallabag\CoreBundle\Repository\EntryRepository; 7use Wallabag\CoreBundle\Repository\EntryRepository;
8use Wallabag\CoreBundle\Repository\TagRepository;
7 9
8class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface 10class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
9{ 11{
10 private $tokenStorage; 12 private $tokenStorage;
11 private $repository; 13 private $entryRepository;
14 private $tagRepository;
15 private $lifeTime;
12 16
13 public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null) 17 public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0)
14 { 18 {
15 $this->repository = $repository; 19 $this->entryRepository = $entryRepository;
20 $this->tagRepository = $tagRepository;
16 $this->tokenStorage = $tokenStorage; 21 $this->tokenStorage = $tokenStorage;
22 $this->lifeTime = $lifeTime;
17 } 23 }
18 24
19 public function getFilters() 25 public function getFilters()
@@ -27,6 +33,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
27 { 33 {
28 return array( 34 return array(
29 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), 35 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
36 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
30 ); 37 );
31 } 38 }
32 39
@@ -47,24 +54,24 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
47 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; 54 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
48 55
49 if (null === $user || !is_object($user)) { 56 if (null === $user || !is_object($user)) {
50 return []; 57 return 0;
51 } 58 }
52 59
53 switch ($type) { 60 switch ($type) {
54 case 'starred': 61 case 'starred':
55 $qb = $this->repository->getBuilderForStarredByUser($user->getId()); 62 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
56 break; 63 break;
57 64
58 case 'archive': 65 case 'archive':
59 $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); 66 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
60 break; 67 break;
61 68
62 case 'unread': 69 case 'unread':
63 $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); 70 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
64 break; 71 break;
65 72
66 case 'all': 73 case 'all':
67 $qb = $this->repository->getBuilderForAllByUser($user->getId()); 74 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
68 break; 75 break;
69 76
70 default: 77 default:
@@ -78,13 +85,49 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
78 ->groupBy('e.id') 85 ->groupBy('e.id')
79 ->getQuery(); 86 ->getQuery();
80 87
81 $data = $this->repository 88 $data = $this->enableCache($query)
82 ->enableCache($query)
83 ->getArrayResult(); 89 ->getArrayResult();
84 90
85 return count($data); 91 return count($data);
86 } 92 }
87 93
94 /**
95 * Return number of tags.
96 *
97 * @return int
98 */
99 public function countTags()
100 {
101 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
102
103 if (null === $user || !is_object($user)) {
104 return 0;
105 }
106
107 $qb = $this->tagRepository->findAllTags($user->getId());
108
109 $data = $this->enableCache($qb->getQuery())
110 ->getArrayResult();
111
112 return count($data);
113 }
114
115 /**
116 * Enable cache for a query.
117 *
118 * @param Query $query
119 *
120 * @return Query
121 */
122 private function enableCache(Query $query)
123 {
124 $query->useQueryCache(true);
125 $query->useResultCache(true);
126 $query->setResultCacheLifetime($this->lifeTime);
127
128 return $query;
129 }
130
88 public function getName() 131 public function getName()
89 { 132 {
90 return 'wallabag_extension'; 133 return 'wallabag_extension';