Passed
Pull Request — master (#98)
by
unknown
03:57
created

AuditController::showEntityHistoryAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 21
rs 9.8333
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\AuditEntry;
9
use DH\DoctrineAuditBundle\Reader\AuditReader;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class AuditController extends AbstractController
17
{
18
    /**
19
     * @Route("/audit", name="dh_doctrine_audit_list_audits", methods={"GET"})
20
     */
21
    public function listAuditsAction(): Response
22
    {
23
        $reader = $this->container->get('dh_doctrine_audit.reader');
24
25
        return $this->render('@DHDoctrineAudit/Audit/audits.html.twig', [
26
            'audited' => $reader->getEntities(),
27
            'reader' => $reader,
28
        ]);
29
    }
30
31
    /**
32
     * @Route("/audit/revert/{hash}/{field}", name="dh_doctrine_audit_revert")
33
     *
34
     * @param $id
35
     * @param $object
36
     * @param $entity
37
     * @param $field
38
     *
39
     * @return RedirectResponse
40
     */
41
    public function revertEntityHistoryAction($hash, $field)
42
    {
43
        // get audit manager service
44
        $am = $this->container->get('dh_doctrine_audit.manager');
45
        // get audit reader service
46
        $reader = $this->container->get('dh_doctrine_audit.reader');
47
        // get audit entity manager
48
        $em = $this->container->get('doctrine.orm.default_entity_manager');
49
50
        $reverted_entity = $am->revert($reader, $em, $hash, $field);
51
        $entity_name = $em->getMetadataFactory()->getMetadataFor(get_class($reverted_entity))->getName();
52
53
        $em->persist($reverted_entity);
54
        $em->flush();
55
56
        return $this->redirectToRoute('dh_doctrine_audit_show_entity_history', [
57
            'entity' => $entity_name,
58
        ]);
59
    }
60
61
    /**
62
     * @Route("/audit/transaction/{hash}", name="dh_doctrine_audit_show_transaction", methods={"GET"})
63
     *
64
     * @param string $hash
65
     *
66
     * @return Response
67
     * @throws \Doctrine\ORM\ORMException
68
     *
69
     * @throws InvalidArgumentException
70
     */
71
    public function showTransactionAction(string $hash): Response
72
    {
73
        $reader = $this->container->get('dh_doctrine_audit.reader');
74
        $audits = $reader->getAuditsByTransactionHash($hash);
75
76
        return $this->render('@DHDoctrineAudit/Audit/transaction.html.twig', [
77
            'hash' => $hash,
78
            'audits' => $audits,
79
        ]);
80
    }
81
82
    /**
83
     * @Route("/audit/{entity}/{id}", name="dh_doctrine_audit_show_entity_history", methods={"GET"})
84
     *
85
     * @param Request    $request
86
     * @param string     $entity
87
     * @param int|string $id
88
     *
89
     * @return Response
90
     */
91
    public function showEntityHistoryAction(Request $request, string $entity, $id = null): Response
92
    {
93
        $page = (int)$request->query->get('page', 1);
94
        $entity = AuditHelper::paramToNamespace($entity);
95
96
        $reader = $this->container->get('dh_doctrine_audit.reader');
97
98
        if (!$reader->getConfiguration()->isAuditable($entity)) {
99
            throw $this->createNotFoundException();
100
        }
101
102
        try {
103
            $entries = $reader->getAuditsPager($entity, $id, $page, AuditReader::PAGE_SIZE);
104
        } catch (AccessDeniedException $e) {
105
            throw $this->createAccessDeniedException();
106
        }
107
108
        return $this->render('@DHDoctrineAudit/Audit/entity_history.html.twig', [
109
            'id' => $id,
110
            'entity' => $entity,
111
            'entries' => $entries,
112
        ]);
113
    }
114
}
115