CSPLoggerController::logsAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Sockam\CSPLoggerBundle\Controller;
4
5
use Sockam\CSPLoggerBundle\Entity\CSPReport;
6
use Sockam\CSPLoggerBundle\Form\Type\CSPReportType;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class CSPLoggerController extends Controller
12
{
13
    public function logAction(Request $request)
14
    {
15
        $csp_report = new CSPReport();
16
        $form = $this->createForm(CSPReportType::class, $csp_report);
17
        if ($request->isMethod('POST')) {
18
            $data = array_values(json_decode($request->getContent(), true));
19
            $form->submit($data[0]);
20
            if ($form->isSubmitted() && $form->isValid()) {
21
                $csp_report->setDateReceived(new \DateTime());
22
                $csp_report->setSenderIp($request->getClientIp());
23
                $csp_report->setUserAgent($request->headers->get('User-Agent'));
24
                $em = $this->get('doctrine.orm.default_entity_manager');
25
                $em->persist($csp_report);
26
                $em->flush($csp_report);
27
28
                return new Response($csp_report->getId());
29
            } else {
30
                return new Response('OOPS');
31
            }
32
        }
33
    }
34
35
    public function logsAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $em = $this->get('doctrine.orm.default_entity_manager');
38
        $logs = $em->getRepository('SockamCSPLoggerBundle:CSPReport')->findBy([], ['dateReceived' => 'desc']);
39
40
        return $this->render('@SockamCSPLogger/CSPLogs/logs.html.twig', ['logs' => $logs]);
41
    }
42
}
43