FlatFileContentController::render()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
ccs 0
cts 22
cp 0
rs 8.439
cc 5
eloc 20
nc 12
nop 0
crap 30
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 FlatFileContentController implements InjectionAwareInterface
13
{
14
    use InjectionAwareTrait;
15
16
17
18
    /**
19
     * Render a page using flat file content.
20
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
21
     *
22
     * @return void
23
     */
24
    public function render()
25
    {
26
        // Get the current route and see if it matches a content/file
27
        $path = $this->di->get("request")->getRoute();
28
        $file1 = ANAX_INSTALL_PATH . "/content/${path}.md";
29
        $file2 = ANAX_INSTALL_PATH . "/content/${path}/index.md";
30
31
        $file = is_file($file1) ? $file1 : null;
32
        $file = is_file($file2) ? $file2 : $file;
33
34
        if (!$file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type string|null 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...
35
            return;
36
        }
37
38
        // Check that file is really in the right place
39
        $real = realpath($file);
40
        $base = realpath(ANAX_INSTALL_PATH . "/content/");
41
        if (strncmp($base, $real, strlen($base))) {
42
            return;
43
        }
44
45
        // Get content from markdown file
46
        $content = file_get_contents($file);
47
        $content = $this->di->get("textfilter")->parse(
48
            $content,
49
            ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"]
50
        );
51
52
        // Render a standard page using layout
53
        $this->di->get("view")->add("default1/article", [
54
            "content" => $content->text,
55
            "frontmatter" => $content->frontmatter,
56
        ]);
57
        $this->di->get("pageRender")->renderPage($content->frontmatter);
58
    }
59
}
60