Failed Conditions
Branch issue#666 (91903a)
by Guilherme
08:25
created

ImpersonationReportController::editAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 17
ccs 0
cts 11
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Controller\Admin;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
21
use LoginCidadao\CoreBundle\Form\ImpersonationReportType;
22
use LoginCidadao\CoreBundle\Entity\ImpersonationReport;
23
use LoginCidadao\APIBundle\Entity\ActionLog;
24
25
/**
26
 * @Route("/admin/impersonation/reports")
27
 * @Security("has_role('FEATURE_IMPERSONATION_REPORTS')")
28
 */
29
class ImpersonationReportController extends Controller
30
{
31
32
    /**
33
     * @Route("/", name="lc_admin_impersonation_report_index")
34
     * @Template()
35
     */
36
    public function indexAction()
37
    {
38
        $logRepo    = $this->getDoctrine()
39
            ->getRepository('LoginCidadaoAPIBundle:ActionLog');
40
        $reportRepo = $this->getDoctrine()
41
            ->getRepository('LoginCidadaoCoreBundle:ImpersonationReport');
42
43
        $pending = $logRepo->findImpersonatonsWithoutReports(null,
44
            $this->getUser(), true);
45
        $reports = $reportRepo->findBy(array(
46
            'impersonator' => $this->getUser()
47
        ));
48
49
        return compact('pending', 'reports');
50
    }
51
52
    /**
53
     * @Route("/new/{logId}", name="lc_admin_impersonation_report_new")
54
     * @Template()
55
     */
56
    public function newAction(Request $request, $logId)
57
    {
58
        $log = $this->getActionLogOr404($logId);
59
60
        $report = $this->getNewReport($log);
61
        if ($report instanceof RedirectResponse) {
62
            return $report;
63
        }
64
65
        $report->setImpersonator($this->getUser());
66
67
        $form = $this->createForm('LoginCidadao\CoreBundle\Form\ImpersonationReportType',
68
            $report);
69
        $form->handleRequest($request);
70
71
        if ($form->isValid()) {
72
            $em = $this->getDoctrine()->getManager();
73
            $em->persist($report);
74
            $em->flush();
75
76
            return $this->redirectToRoute('lc_admin_impersonation_report_index');
77
        }
78
79
        return array('form' => $form->createView(), 'report' => $report);
80
    }
81
82
    /**
83
     * @Route("/{id}/edit", name="lc_admin_impersonation_report_edit", requirements={"id" = "\d+"})
84
     * @Template()
85
     * @Security("has_role('ROLE_IMPERSONATION_REPORTS_EDIT')")
86
     */
87
    public function editAction(Request $request, $id)
88
    {
89
        $report = $this->getOr404($id);
90
91
        $form = $this->createForm('LoginCidadao\CoreBundle\Form\ImpersonationReportType',
92
            $report);
93
        $form->handleRequest($request);
94
95
        if ($form->isValid()) {
96
            $em = $this->getDoctrine()->getManager();
97
            $em->persist($report);
98
            $em->flush();
99
100
            return $this->redirectToRoute('lc_admin_impersonation_report_index');
101
        }
102
103
        return array('form' => $form->createView(), 'report' => $report);
104
    }
105
106
    /**
107
     *
108
     * @param integer $id
109
     * @return ActionLog
110
     * @throws NotFoundHttpException
111
     */
112
    private function getActionLogOr404($id)
113
    {
114
        $logRepo = $this->getDoctrine()
115
            ->getRepository('LoginCidadaoAPIBundle:ActionLog');
116
117
        $log = $logRepo->find($id);
118
119
        if ($log instanceof ActionLog) {
120
            return $log;
121
        }
122
123
        throw $this->createNotFoundException();
124
    }
125
126
    /**
127
     *
128
     * @param integer $id
129
     * @return ActionLog
130
     * @throws NotFoundHttpException
131
     */
132
    private function getOr404($id)
133
    {
134
        $reportRepo = $this->getDoctrine()
135
            ->getRepository('LoginCidadaoCoreBundle:ImpersonationReport');
136
137
        $report = $reportRepo->find($id);
138
139
        if ($report instanceof ImpersonationReport) {
140
            return $report;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $report returns the type LoginCidadao\CoreBundle\Entity\ImpersonationReport which is incompatible with the documented return type LoginCidadao\APIBundle\Entity\ActionLog.
Loading history...
141
        }
142
143
        throw $this->createNotFoundException();
144
    }
145
146
    /**
147
     *
148
     * @param ActionLog $log
149
     * @return ImpersonationReport | RedirectResponse
150
     * @throws AccessDeniedException
151
     */
152
    private function getNewReport(ActionLog $log)
153
    {
154
        $reportRepo = $this->getDoctrine()
155
            ->getRepository('LoginCidadaoCoreBundle:ImpersonationReport');
156
        $personRepo = $this->getDoctrine()
157
            ->getRepository('LoginCidadaoCoreBundle:Person');
158
159
        $report = new ImpersonationReport();
160
161
        $existingReport = $reportRepo->findOneBy(array('actionLog' => $log));
162
        if ($existingReport instanceof ImpersonationReport) {
163
            $this->addFlash('error', "This action was already reported.");
164
            return $this->redirectToRoute('lc_admin_impersonation_report_index');
165
        }
166
167
        $impersonatorId = $log->getClientId();
168
169
        if ($impersonatorId !== $this->getUser()->getId()) {
170
            throw $this->createAccessDeniedException("You cannot fill other person's report!");
171
        }
172
173
        $targetUser = $personRepo->find($log->getUserId());
174
        $report->setTarget($targetUser)
175
            ->setActionLog($log);
176
177
        return $report;
178
    }
179
}
180