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

AuditController::revertEntityHistoryAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 27
rs 9.7998
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Controller;
4
5
use DH\DoctrineAuditBundle\Helper\AuditHelper;
6
use DH\DoctrineAuditBundle\Reader\AuditEntry;
7
use DH\DoctrineAuditBundle\Reader\AuditReader;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class AuditController extends AbstractController
14
{
15
    /**
16
     * @Route("/audit", name="dh_doctrine_audit_list_audits", methods={"GET"})
17
     */
18
    public function listAuditsAction(): Response
19
    {
20
        $reader = $this->container->get('dh_doctrine_audit.reader');
21
22
        return $this->render('@DHDoctrineAudit/Audit/audits.html.twig', [
23
            'audited' => $reader->getEntities(),
24
            'reader' => $reader,
25
        ]);
26
    }
27
28
    /**
29
     * @Route("/audit/revert/{id}/{object}/{entity}/{field}", name="dh_doctrine_audit_revert")
30
     *
31
     * @param $id
32
     * @param $object
33
     * @param $entity
34
     * @param $field
35
     *
36
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
37
     */
38
    public function revertEntityHistoryAction($id, $object, $entity, $field)
39
    {
40
        // get audit reader service
41
        $reader = $this->container->get('dh_doctrine_audit.reader');
42
        $am = $this->container->get('dh_doctrine_audit.manager');
0 ignored issues
show
Unused Code introduced by
The assignment to $am is dead and can be removed.
Loading history...
43
        // get audit entity manager
44
        $em = $this->container->get('doctrine.orm.default_entity_manager');
45
46
        /** @var AuditEntry $entity_audit */
47
        $entity_audit = $reader->getAudit(AuditHelper::paramToNamespace($entity), $id);
48
        $audited_entity = AuditHelper::paramToNamespace($entity);
49
        $current_entity = $em->getRepository($audited_entity)->find($object);
50
51
        // Get all differences
52
        $diffs = $entity_audit[0]->getDiffs();
53
        // get field value to revert
54
        $field_value = $diffs[$field]['old'];
55
56
        $setMethod = "set{$field}";
57
58
        $current_entity->{$setMethod}($field_value);
59
60
        $em->persist($current_entity);
61
        $em->flush();
62
63
        return $this->redirectToRoute('dh_doctrine_audit_show_entity_history', [
64
            'entity' => $entity,
65
        ]);
66
    }
67
68
    /**
69
     * @Route("/audit/transaction/{hash}", name="dh_doctrine_audit_show_transaction", methods={"GET"})
70
     *
71
     * @param string $hash
72
     *
73
     * @throws \Doctrine\ORM\ORMException
74
     *
75
     * @return Response
76
     */
77
    public function showTransactionAction(string $hash): Response
78
    {
79
        $reader = $this->container->get('dh_doctrine_audit.reader');
80
        $audits = $reader->getAuditsByTransactionHash($hash);
81
82
        return $this->render('@DHDoctrineAudit/Audit/transaction.html.twig', [
83
            'hash' => $hash,
84
            'audits' => $audits,
85
        ]);
86
    }
87
88
    /**
89
     * @Route("/audit/{entity}/{id}", name="dh_doctrine_audit_show_entity_history", methods={"GET"})
90
     *
91
     * @param Request    $request
92
     * @param string     $entity
93
     * @param int|string $id
94
     *
95
     * @return Response
96
     */
97
    public function showEntityHistoryAction(Request $request, string $entity, $id = null): Response
98
    {
99
        $page = (int) $request->query->get('page', 1);
100
        $entity = AuditHelper::paramToNamespace($entity);
101
102
        $reader = $this->container->get('dh_doctrine_audit.reader');
103
104
        if (!$reader->getConfiguration()->isAuditable($entity)) {
105
            throw $this->createNotFoundException();
106
        }
107
108
        $entries = $reader->getAuditsPager($entity, $id, $page, AuditReader::PAGE_SIZE);
109
110
        return $this->render('@DHDoctrineAudit/Audit/entity_history.html.twig', [
111
            'id' => $id,
112
            'entity' => $entity,
113
            'entries' => $entries,
114
        ]);
115
    }
116
}
117