PageRender::renderPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 52
c 0
b 0
f 0
ccs 0
cts 32
cp 0
rs 9.4929
cc 1
eloc 32
nc 1
nop 2
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// namespace Anax\Page;
4
namespace Marcusgsta\Page;
5
6
use \Anax\DI\InjectionAwareInterface;
7
use \Anax\DI\InjectionAwareTrait;
8
9
/**
10
 * A default page rendering class.
11
 */
12
class PageRender implements PageRenderInterface, InjectionAwareInterface
13
{
14
    use InjectionAwareTrait;
15
16
17
18
    /**
19
     * Render a standard web page using a specific layout.
20
     *
21
     * @param array   $data   variables to expose to layout view.
22
     * @param integer $status code to use when delivering the result.
23
     * @SuppressWarnings(PHPMD.ExitExpression)
24
     * @return void
25
     */
26
    public function renderPage($data, $status = 200)
27
    {
28
        $data["stylesheets"] = ["css/bootstrap.min.css", "css/style.css", "css/remserver.css"];
29
        $data["javascripts"] = [
30
            "js/jquery-3.2.1.slim.min.js",
31
            "js/tether.min.js",
32
            "js/popper.min.js",
33
            "js/bootstrap.min.js"
34
        ];
35
        $data["commentableRoutes"] = ["index", "about", "report", "remserver", "book"];
36
37
38
        // create navbar from class
39
        $navbar = new \Marcusgsta\Navbar\Navbar();
40
        $navbar->configure();
41
        $navbarconfig = $navbar->config;
42
43
        // check if a user is logged in
44
        // $acronym = $this->di->session->get("user");
45
        $acronym = $this->di->get("loginController")->isLoggedIn();
46
47
        $role = $this->di->get("commentController")->getRole($acronym);
48
49
        // Add common header, navbar and footer
50
        // Add layout, render it, add to response and send.
51
        $view = $this->di->get("view");
52
        $view->add("take1/header", [], "header");
53
        $view->add("take1/navbar", $navbarconfig, "navbar");
54
55
        $comments = $this->di->get("commentController")->getComments();
56
        $comments = $comments['items'];
57
58
        $commentableRoutes = $data['commentableRoutes'];
59
        // $view->add("take1/comment", ["user" => $acronym], "commentsection");
60
        $view->add(
61
            "take1/comment",
62
            [
63
            "comments" => $comments,
64
            "user" => $acronym,
65
            "role" => $role,
66
            "commentableRoutes" => $commentableRoutes
67
            ],
68
            "commentsection"
69
        );
70
71
        $view->add("take1/footer", ["user" => $acronym], "footer");
72
        $view->add("default1/layout", $data, "layout");
73
        $body = $view->renderBuffered("layout");
74
        $this->di->get("response")->setBody($body)
75
                                  ->send($status);
76
        exit;
77
    }
78
}
79