]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Twig / WallabagExtension.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Twig;
4
5 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
6 use Symfony\Component\Translation\TranslatorInterface;
7 use Wallabag\CoreBundle\Repository\EntryRepository;
8 use Wallabag\CoreBundle\Repository\TagRepository;
9
10 class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
11 {
12 private $tokenStorage;
13 private $entryRepository;
14 private $tagRepository;
15 private $lifeTime;
16 private $translator;
17
18 public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator)
19 {
20 $this->entryRepository = $entryRepository;
21 $this->tagRepository = $tagRepository;
22 $this->tokenStorage = $tokenStorage;
23 $this->lifeTime = $lifeTime;
24 $this->translator = $translator;
25 }
26
27 public function getFilters()
28 {
29 return [
30 new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
31 ];
32 }
33
34 public function getFunctions()
35 {
36 return [
37 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
38 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
39 new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
40 ];
41 }
42
43 public function removeWww($url)
44 {
45 return preg_replace('/^www\./i', '', $url);
46 }
47
48 /**
49 * Return number of entries depending of the type (unread, archive, starred or all).
50 *
51 * @param string $type Type of entries to count
52 *
53 * @return int
54 */
55 public function countEntries($type)
56 {
57 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
58
59 if (null === $user || !is_object($user)) {
60 return 0;
61 }
62
63 switch ($type) {
64 case 'starred':
65 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
66 break;
67 case 'archive':
68 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
69 break;
70 case 'unread':
71 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
72 break;
73 case 'all':
74 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
75 break;
76 default:
77 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
78 }
79
80 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
81 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
82 $query = $qb
83 ->select('e.id')
84 ->groupBy('e.id')
85 ->getQuery();
86
87 $query->useQueryCache(true);
88 $query->useResultCache(true);
89 $query->setResultCacheLifetime($this->lifeTime);
90
91 return count($query->getArrayResult());
92 }
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 return $this->tagRepository->countAllTags($user->getId());
108 }
109
110 /**
111 * Display a single line about reading stats.
112 *
113 * @return string
114 */
115 public function displayStats()
116 {
117 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
118
119 if (null === $user || !is_object($user)) {
120 return 0;
121 }
122
123 $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
124 ->select('e.id')
125 ->groupBy('e.id')
126 ->getQuery();
127
128 $query->useQueryCache(true);
129 $query->useResultCache(true);
130 $query->setResultCacheLifetime($this->lifeTime);
131
132 $nbArchives = count($query->getArrayResult());
133
134 $interval = $user->getCreatedAt()->diff(new \DateTime('now'));
135 $nbDays = (int) $interval->format('%a') ?: 1;
136
137 // force setlocale for date translation
138 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
139
140 return $this->translator->trans('footer.stats', [
141 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
142 '%nb_archives%' => $nbArchives,
143 '%per_day%' => round($nbArchives / $nbDays, 2),
144 ]);
145 }
146
147 public function getName()
148 {
149 return 'wallabag_extension';
150 }
151 }