FlatFileContentController::catchAll()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 36
ccs 0
cts 21
cp 0
rs 9.2888
c 0
b 0
f 0
cc 5
nc 12
nop 1
crap 30
1
<?php
2
3
namespace Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * A controller for flat file markdown content.
10
 */
11
class FlatFileContentController implements ContainerInjectableInterface
12
{
13
    use ContainerInjectableTrait;
14
15
16
17
    /**
18
     * Render a page using flat file content.
19
     *
20
     * @param array $args as a variadic to catch all arguments.
21
     *
22
     * @return mixed as null when flat file is not found and otherwise a
23
     *               complete response object with content to render.
24
     *
25
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
26
     */
27
    public function catchAll(...$args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

27
    public function catchAll(/** @scrutinizer ignore-unused */ ...$args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        // Get the current route and see if it matches a content/file
30
        $path = $this->di->get("request")->getRoute();
31
        $file1 = ANAX_INSTALL_PATH . "/content/{$path}.md";
32
        $file2 = ANAX_INSTALL_PATH . "/content/{$path}/index.md";
33
34
        $file = is_file($file1) ? $file1 : null;
35
        $file = is_file($file2) ? $file2 : $file;
36
        
37
        if (!$file) {
38
            return;
39
        }
40
41
        // Check that file is really in the right place
42
        $real = realpath($file);
43
        $base = realpath(ANAX_INSTALL_PATH . "/content/");
44
        if (strncmp($base, $real, strlen($base))) {
45
            return;
46
        }
47
48
        // Get content from markdown file
49
        $content = file_get_contents($file);
50
        $content = $this->di->get("textfilter")->parse(
51
            $content,
52
            ["frontmatter", "variable", "shortcode", "markdown", "titlefromheader"]
53
        );
54
55
        // Add content as a view and then render the page
56
        $page = $this->di->get("page");
57
        $page->add("anax/v2/article/default", [
58
            "content" => $content->text,
59
            "frontmatter" => $content->frontmatter,
60
        ]);
61
62
        return $page->render($content->frontmatter);
63
    }
64
}
65