CSPLoggerController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A logAction() 0 21 4
A logsAction() 0 7 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