AuditExtension::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AdminBundle\Twig;
4
5
use DataDog\AuditBundle\Entity\AuditLog;
6
use DataDog\AuditBundle\Entity\Association;
7
8
class AuditExtension extends \Twig_Extension
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function getFunctions()
14
    {
15
        $defaults = [
16
            'is_safe' => ['html'],
17
            'needs_environment' => true,
18
        ];
19
20
        return [
21
            new \Twig_SimpleFunction('audit', [$this, 'audit'], $defaults),
22
            new \Twig_SimpleFunction('audit_value', [$this, 'value'], $defaults),
23
            new \Twig_SimpleFunction('audit_assoc', [$this, 'assoc'], $defaults),
24
            new \Twig_SimpleFunction('audit_blame', [$this, 'blame'], $defaults),
25
        ];
26
    }
27
28
    public function audit(\Twig_Environment $twig, AuditLog $log)
29
    {
30
        return $twig->render("AdminBundle:Audit:{$log->getAction()}.html.twig", compact('log'));
31
    }
32
33
    public function assoc(\Twig_Environment $twig, $assoc)
34
    {
35
        return $twig->render("AdminBundle:Audit:assoc.html.twig", compact('assoc'));
36
    }
37
38
    public function blame(\Twig_Environment $twig, $blame)
39
    {
40
        return $twig->render("AdminBundle:Audit:blame.html.twig", compact('blame'));
41
    }
42
43
    public function value(\Twig_Environment $twig, $val)
44
    {
45
        switch (true) {
46
        case is_bool($val):
47
            return $val ? 'true' : 'false';
48
        case is_array($val) && isset($val['fk']):
49
            return $this->assoc($twig, $val);
50
        case is_array($val):
51
            return json_encode($val);
52
        case is_string($val):
53
            return strlen($val) > 60 ? substr($val, 0, 60) . '...' : $val;
54
        case is_null($val):
55
            return 'NULL';
56
        default:
57
            return $val;
58
        }
59
    }
60
61
    public function getName()
62
    {
63
        return 'admin_audit_extension';
64
    }
65
}
66