Passed
Pull Request — master (#60)
by Damien
02:16
created

TwigExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Twig\Extension;
4
5
use Symfony\Bridge\Doctrine\RegistryInterface;
6
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFilter;
8
use Twig\TwigFunction;
9
10
class TwigExtension extends AbstractExtension
11
{
12
    protected $doctrine;
13
14
    public function getFunctions(): array
15
    {
16
        return [
17
            new TwigFunction('findUser', [$this, 'findUser']),
18
            new TwigFunction('class', [$this, 'getClass']),
19
            new TwigFunction('tablename', [$this, 'getTablename']),
20
        ];
21
    }
22
23
    public function getFilters(): array
24
    {
25
        return [
26
            new TwigFilter('json_decode', 'json_decode'),
27
        ];
28
    }
29
30
    public function __construct(RegistryInterface $doctrine)
31
    {
32
        $this->doctrine = $doctrine;
33
    }
34
35
    public function findUser($id, $repository)
36
    {
37
        if (null === $id) {
38
            return null;
39
        }
40
41
        $em = $this->doctrine->getManager();
42
        $repo = $em->getRepository($repository);
43
44
        return $repo->find($id);
45
    }
46
47
    public function getClass($entity): string
48
    {
49
        return \get_class($entity);
50
    }
51
52
    public function getTablename($entity): string
53
    {
54
        return $this
55
            ->doctrine
56
            ->getManager()
57
            ->getClassMetadata(\get_class($entity))
58
            ->getTableName()
59
        ;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getName(): string
66
    {
67
        return 'twig_extensions';
68
    }
69
}
70