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

ContentController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 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