Passed
Branch master (7c3f6c)
by Javi
02:38
created

ContentController::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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