FlatFileContentController::render()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10.3999

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 12
nop 0
dl 0
loc 34
ccs 8
cts 20
cp 0.4
crap 10.3999
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Alvo\Page;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
8
/**
9
 * A default page rendering class.
10
 */
11
class FlatFileContentController implements InjectionAwareInterface
12
{
13
    use InjectionAwareTrait;
14
15
16
17
    /**
18
     * Render a page using flat file content.
19
     *
20
     * @return void
21
     */
22 1
    public function render()
23
    {
24
        // Get the current route and see if it matches a content/file
25 1
        $path = $this->di->get("request")->getRoute();
26 1
        $file1 = ANAX_INSTALL_PATH . "/content/$path.md";
27 1
        $file2 = ANAX_INSTALL_PATH . "/content/$path/index.md";
28
29 1
        $file = is_file($file1) ? $file1 : null;
30 1
        $file = is_file($file2) ? $file2 : $file;
31
32 1
        if (!$file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
33 1
            return;
34
        }
35
36
        // Check that file is really in the right place
37
        $real = realpath($file);
38
        $base = realpath(ANAX_INSTALL_PATH . "/content/");
39
        if (strncmp($base, $real, strlen($base))) {
40
            return;
41
        }
42
43
        // Get content from markdown file
44
        $content = file_get_contents($file);
45
        $content = $this->di->get("textfilter")->parse(
46
            $content,
47
            ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"]
48
        );
49
50
        // Render a standard page using layout
51
        $this->di->get("view")->add("article/article", [
52
            "content" => $content->text,
53
            "frontmatter" => $content->frontmatter,
54
        ]);
55
        $this->di->get("pageRender")->renderPage($content->frontmatter);
56
    }
57
}
58