Passed
Pull Request — master (#98)
by
unknown
02:59
created

AuditController::revertEntityHistoryAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 4
dl 0
loc 27
rs 9.7998
c 0
b 0
f 0
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
     * @param $id
31
     * @param $object
32
     * @param $entity
33
     * @param $field
34
     *
35
     * @return Symfony\Component\HttpFoundation\RedirectResponse
0 ignored issues
show
Bug introduced by
The type DH\DoctrineAuditBundle\C...dation\RedirectResponse was not found. Did you mean Symfony\Component\HttpFoundation\RedirectResponse? If so, make sure to prefix the type with \.
Loading history...
36
     */
37
    public function revertEntityHistoryAction($id, $object, $entity, $field)
38
    {
39
        // get audit reader service
40
        $reader = $this->container->get('dh_doctrine_audit.reader');
41
        $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...
42
        // get audit entity manager
43
        $em = $this->container->get('doctrine.orm.default_entity_manager');
44
45
        /** @var AuditEntry $entity_audit */
46
        $entity_audit = $reader->getAudit(AuditHelper::paramToNamespace($entity), $id);
47
        $audited_entity = AuditHelper::paramToNamespace($entity);
48
        $current_entity = $em->getRepository($audited_entity)->find($object);
49
50
        // Get all differences
51
        $diffs = $entity_audit[0]->getDiffs();
52
        // get field value to revert
53
        $field_value = $diffs[$field]['old'];
54
55
        $setMethod = "set{$field}";
56
57
        $current_entity->$setMethod($field_value);
58
59
        $em->persist($current_entity);
60
        $em->flush();
61
62
        return $this->redirectToRoute('dh_doctrine_audit_show_entity_history', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->redirectTo...y('entity' => $entity)) returns the type Symfony\Component\HttpFoundation\RedirectResponse which is incompatible with the documented return type DH\DoctrineAuditBundle\C...dation\RedirectResponse.
Loading history...
63
            'entity' => $entity
64
        ]);
65
    }
66
67
    /**
68
     * @Route("/audit/transaction/{hash}", name="dh_doctrine_audit_show_transaction", methods={"GET"})
69
     *
70
     * @param string $hash
71
     *
72
     * @throws \Doctrine\ORM\ORMException
73
     *
74
     * @return Response
75
     */
76
    public function showTransactionAction(string $hash): Response
77
    {
78
        $reader = $this->container->get('dh_doctrine_audit.reader');
79
        $audits = $reader->getAuditsByTransactionHash($hash);
80
81
        return $this->render('@DHDoctrineAudit/Audit/transaction.html.twig', [
82
            'hash' => $hash,
83
            'audits' => $audits,
84
        ]);
85
    }
86
87
    /**
88
     * @Route("/audit/{entity}/{id}", name="dh_doctrine_audit_show_entity_history", methods={"GET"})
89
     *
90
     * @param Request    $request
91
     * @param string     $entity
92
     * @param int|string $id
93
     *
94
     * @return Response
95
     */
96
    public function showEntityHistoryAction(Request $request, string $entity, $id = null): Response
97
    {
98
        $page = (int) $request->query->get('page', 1);
99
        $entity = AuditHelper::paramToNamespace($entity);
100
101
        $reader = $this->container->get('dh_doctrine_audit.reader');
102
103
        if (!$reader->getConfiguration()->isAuditable($entity)) {
104
            throw $this->createNotFoundException();
105
        }
106
107
        $entries = $reader->getAuditsPager($entity, $id, $page, AuditReader::PAGE_SIZE);
108
109
        return $this->render('@DHDoctrineAudit/Audit/entity_history.html.twig', [
110
            'id' => $id,
111
            'entity' => $entity,
112
            'entries' => $entries,
113
        ]);
114
    }
115
}
116