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

AuditController::revertEntityHistoryAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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