aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
blob: 6e46c7018cab9df687aa7e9be754913e74bac817 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

namespace Wallabag\CoreBundle\Twig;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Wallabag\CoreBundle\Repository\EntryRepository;

class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
    private $tokenStorage;
    private $repository;

    public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
    {
        $this->repository = $repository;
        $this->tokenStorage = $tokenStorage;
    }

    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
        ];
    }

    public function removeWww($url)
    {
        return preg_replace('/^www\./i', '', $url);
    }

    public function getGlobals()
    {
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;

        if (null === $user || !is_object($user)) {
            return array();
        }

        $unreadEntries = $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()->getResult();
        $starredEntries = $this->repository->getBuilderForStarredByUser($user->getId())->getQuery()->getResult();
        $archivedEntries = $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()->getResult();
        $allEntries = $this->repository->getBuilderForAllByUser($user->getId())->getQuery()->getResult();

        return array(
            'unreadEntries' => count($unreadEntries),
            'starredEntries' => count($starredEntries),
            'archivedEntries' => count($archivedEntries),
            'allEntries' => count($allEntries),
        );
    }

    public function getName()
    {
        return 'wallabag_extension';
    }
}