ContentController::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 3
crap 3.072
1
<?php
2
3
namespace Flatdown\Controllers;
4
5
use Flatdown\Application;
6
use Flatdown\Exceptions\FileNotFoundException;
7
use Middlewares\HttpErrorException;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class ContentController
11
{
12 6
    public function __invoke(ServerRequestInterface $req, array $params, Application $app)
13
    {
14
        // do not allow dangerous path chars
15 6
        $document       = str_replace(['..', '*', '?', './'], '_', $params['document']);
16 6
        $index_filename = $app->getConfig('content_path') . '/' . $document . '/index.md';
17
18 6
        if (file_exists($index_filename)) {
19
            $filename = $index_filename;
20
        } else {
21 6
            $filename = $app->getConfig('content_path') . '/' . $document . '.md';
22
        }
23
24
        try {
25 6
            return $app->get('markdown.page', [$filename]);
26 3
        } catch (FileNotFoundException $e) {
27 3
            throw HttpErrorException::create(404);
28
        }
29
    }
30
}
31