AuditController::diffAction()   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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AdminBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Doctrine\ORM\QueryBuilder;
11
use DataDog\PagerBundle\Pagination;
12
use DataDog\AuditBundle\Entity\AuditLog;
13
use AppBundle\Controller\DoctrineController;
14
15
class AuditController extends Controller
16
{
17
    use DoctrineController;
18
19
    public function filters(QueryBuilder $qb, $key, $val)
20
    {
21
        switch ($key) {
22
        case 'history':
23
            if ($val) {
24
                $orx = $qb->expr()->orX();
25
                $orx->add('s.fk = :fk');
26
                $orx->add('t.fk = :fk');
27
28
                $qb->andWhere($orx);
29
                $qb->setParameter('fk', intval($val));
30
            }
31
            break;
32
        case 'class':
33
            $orx = $qb->expr()->orX();
34
            $orx->add('s.class = :class');
35
            $orx->add('t.class = :class');
36
37
            $qb->andWhere($orx);
38
            $qb->setParameter('class', $val);
39
            break;
40
        case 'blamed':
41
            if ($val === 'null') {
42
                $qb->andWhere($qb->expr()->isNull('a.blame'));
43
            } else {
44
                // this allows us to safely ignore empty values
45
                // otherwise if $qb is not changed, it would add where the string is empty statement.
46
                $qb->andWhere($qb->expr()->eq('b.fk', ':blame'));
47
                $qb->setParameter('blame', $val);
48
            }
49
            break;
50
        default:
51
            // if user attemps to filter by other fields, we restrict it
52
            throw new \Exception("filter not allowed");
53
        }
54
    }
55
56
    /**
57
     * @Method("GET")
58
     * @Template
59
     * @Route("/audit")
60
     */
61
    public function indexAction(Request $request)
62
    {
63
        Pagination::$defaults = array_merge(Pagination::$defaults, ['limit' => 10]);
64
65
        $qb = $this->repo("DataDogAuditBundle:AuditLog")
66
            ->createQueryBuilder('a')
67
            ->addSelect('s', 't', 'b')
68
            ->innerJoin('a.source', 's')
69
            ->leftJoin('a.target', 't')
70
            ->leftJoin('a.blame', 'b');
71
72
        $options = [
73
            'sorters' => ['a.loggedAt' => 'DESC'],
74
            'applyFilter' => [$this, 'filters'],
75
        ];
76
77
        $sourceClasses = [
78
            Pagination::$filterAny => $this->get('translator')->trans('audit.pagination.any_source'),
79
        ];
80
81
        foreach ($this->getDoctrine()->getManager()->getMetadataFactory()->getAllMetadata() as $meta) {
82
            if ($meta->isMappedSuperclass || strpos($meta->name, 'DataDog\AuditBundle') === 0) {
83
                continue;
84
            }
85
            $parts = explode('\\', $meta->name);
86
            $sourceClasses[$meta->name] = end($parts);
87
        }
88
89
        $users = [
90
            Pagination::$filterAny => $this->get('translator')->trans('audit.pagination.any_user'),
91
            'null' => $this->get('translator')->trans('audit.pagination.unknown'),
92
        ];
93
        foreach ($this->repo('AppBundle:User')->findAll() as $user) {
94
            $users[$user->getId()] = (string) $user;
95
        }
96
97
        $logs = new Pagination($qb, $request, $options);
98
        return compact('logs', 'sourceClasses', 'users');
99
    }
100
101
    /**
102
     * @Route("/audit/diff/{id}")
103
     * @Method("GET")
104
     * @Template
105
     */
106
    public function diffAction(AuditLog $log)
107
    {
108
        return compact('log');
109
    }
110
}
111