DefaultController::getBaseLayout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Norsys\LogsBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
/**
10
 * Class DefaultController
11
 */
12
class DefaultController extends Controller
13
{
14
    /**
15
     * @param Request $request
16
     * @return \Symfony\Component\HttpFoundation\Response
17
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
18
     */
19
    public function indexAction(Request $request)
20
    {
21 1
        $query = $this->getLogRepository()->getLogsQueryBuilder();
22
23 1
        if ($this->clientHasAccess($request) === false) {
24 1
            throw $this->createNotFoundException();
25
        }
26
27 1
        $pagination = $this->get('knp_paginator')->paginate(
28 1
            $query,
29 1
            $request->query->get('page', 1),
30 1
            $this->container->getParameter('norsys_logs.logs_per_page')
31
        );
32
33 1
        return $this->render('NorsysLogsBundle:Default:index.html.twig', array(
34 1
            'pagination'  => $pagination,
35 1
            'base_layout' => $this->getBaseLayout(),
36
        ));
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @param integer $id
42
     * @return \Symfony\Component\HttpFoundation\Response
43
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
44
     */
45
    public function showAction(Request $request, int $id)
46
    {
47 1
        $log = $this->getLogRepository()->getLogById($id);
48
49 1
        if ($this->clientHasAccess($request) === false) {
50 1
            throw $this->createNotFoundException();
51
        }
52
53 1
        if (null === $log) {
54 1
            throw $this->createNotFoundException('The log entry does not exist');
55
        }
56
57 1
        return $this->render('NorsysLogsBundle:Default:show.html.twig', array(
58 1
            'log'          => $log,
59 1
            'base_layout'  => $this->getBaseLayout(),
60
        ));
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    protected function getBaseLayout()
67
    {
68 1
        return $this->container->getParameter('norsys_logs.base_layout');
69
    }
70
71
    /**
72
     * @return \Norsys\LogsBundle\Model\LogRepository
73
     */
74
    protected function getLogRepository()
75
    {
76 1
        return $this->get('norsys_logs.model.log_repository');
77
    }
78
79
    /**
80
     * @param Request $request
81
     *
82
     * @return boolean
83
     */
84
    protected function clientHasAccess(Request $request)
85
    {
86 1
        $securityEnabled    = $this->getParameter('norsys_logs.security.enabled');
87 1
        $securityAllowedIps = $this->getParameter('norsys_logs.security.allowed_ips');
88
89 1
        if ($securityEnabled === true
90 1
            && in_array($request->getClientIp(), $securityAllowedIps) === false
91
        ) {
92 1
            return false;
93
        }
94
95 1
        return true;
96
    }
97
}
98