Passed
Push — master ( 4c08a2...19d2d9 )
by Damien
03:42
created

AuditController::getSubscribedServices()   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\Controller;
4
5
use DH\DoctrineAuditBundle\Exception\AccessDeniedException;
6
use DH\DoctrineAuditBundle\Exception\InvalidArgumentException;
7
use DH\DoctrineAuditBundle\Helper\AuditHelper;
8
use DH\DoctrineAuditBundle\Reader\AuditReader;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
class AuditController extends AbstractController
15
{
16
    /**
17
     * @Route("/audit", name="dh_doctrine_audit_list_audits", methods={"GET"})
18
     */
19
    public function listAuditsAction(): Response
20
    {
21
        $reader = $this->container->get('dh_doctrine_audit.reader');
22
23
        return $this->render('@DHDoctrineAudit/Audit/audits.html.twig', [
24
            'audited' => $reader->getEntities(),
25
            'reader' => $reader,
26
        ]);
27
    }
28
29
    /**
30
     * @Route("/audit/transaction/{hash}", name="dh_doctrine_audit_show_transaction", methods={"GET"})
31
     *
32
     * @param string $hash
33
     *
34
     * @throws InvalidArgumentException
35
     * @throws \Doctrine\ORM\ORMException
36
     *
37
     * @return Response
38
     */
39
    public function showTransactionAction(string $hash): Response
40
    {
41
        $reader = $this->container->get('dh_doctrine_audit.reader');
42
        $audits = $reader->getAuditsByTransactionHash($hash);
43
44
        return $this->render('@DHDoctrineAudit/Audit/transaction.html.twig', [
45
            'hash' => $hash,
46
            'audits' => $audits,
47
        ]);
48
    }
49
50
    /**
51
     * @Route("/audit/{entity}/{id}", name="dh_doctrine_audit_show_entity_history", methods={"GET"})
52
     *
53
     * @param Request    $request
54
     * @param string     $entity
55
     * @param int|string $id
56
     *
57
     * @throws InvalidArgumentException
58
     *
59
     * @return Response
60
     */
61
    public function showEntityHistoryAction(Request $request, string $entity, $id = null): Response
62
    {
63
        $page = (int) $request->query->get('page', 1);
64
        $entity = AuditHelper::paramToNamespace($entity);
65
66
        $reader = $this->container->get('dh_doctrine_audit.reader');
67
68
        if (!$reader->getConfiguration()->isAuditable($entity)) {
69
            throw $this->createNotFoundException();
70
        }
71
72
        try {
73
            $entries = $reader->getAuditsPager($entity, $id, $page, AuditReader::PAGE_SIZE);
74
        } catch (AccessDeniedException $e) {
75
            throw $this->createAccessDeniedException();
76
        }
77
78
        return $this->render('@DHDoctrineAudit/Audit/entity_history.html.twig', [
79
            'id' => $id,
80
            'entity' => $entity,
81
            'paginator' => $entries,
82
        ]);
83
    }
84
85
    public static function getSubscribedServices(): array
86
    {
87
        return array_merge(parent::getSubscribedServices(), [
88
            'dh_doctrine_audit.reader' => AuditReader::class,
89
        ]);
90
    }
91
}
92