Passed
Pull Request — master (#11)
by Joao
01:52
created

ServerStaticMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 77.42%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 81
ccs 24
cts 31
cp 0.7742
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mimeContentType() 0 29 4
A beforeProcess() 0 34 5
1
<?php
2
3
namespace ByJG\RestServer\Middleware;
4
5
use ByJG\RestServer\Exception\Error415Exception;
6
use ByJG\RestServer\Exception\Error500Exception;
7
use ByJG\RestServer\HttpRequest;
8
use ByJG\RestServer\HttpResponse;
9
use ByJG\RestServer\OutputProcessor\HtmlOutputProcessor;
10
use ByJG\RestServer\ResponseBag;
11
use ByJG\Util\Uri;
12
use FastRoute\Dispatcher;
13
14
class ServerStaticMiddleware implements BeforeMiddlewareInterface
15
{
16
    /**
17
     * Undocumented function
18
     *
19
     * @param mixed $dispatcherStatus
20
     * @param HttpResponse $response
21
     * @param HttpRequest $request
22
     * @return MiddlewareResult
23
     */
24 2
    public function beforeProcess(
25
        $dispatcherStatus,
26
        HttpResponse $response,
27
        HttpRequest $request
28
    )
29
    {
30 2
        if ($dispatcherStatus != Dispatcher::NOT_FOUND) {
31
            return MiddlewareResult::continue();
32
        }
33
34 2
        $requestUri = new Uri($_SERVER['REQUEST_URI']);
35 2
        if ($requestUri->getScheme() === "file") {
36 2
            $file = $requestUri->getPath();
37
        } else {
38
            $script = explode('/', $_SERVER['SCRIPT_FILENAME']);
39
            $script[count($script)-1] = ltrim($requestUri->getPath(), '/');
40
            $file = implode('/', $script);
41
        }
42
43 2
        if (file_exists($file)) {
44 2
            $mime = $this->mimeContentType($file);
45
46 1
            if (empty($mime)) {
47
                return MiddlewareResult::continue();
48
            }
49
50 1
            $response->addHeader("Content-Type", $mime);
51 1
            $response->emptyResponse();
52 1
            $response->getResponseBag()->setSerializationRule(ResponseBag::RAW);
53 1
            $response->write(file_get_contents($file));
54 1
            return MiddlewareResult::stopProcessingOthers(HtmlOutputProcessor::class);
0 ignored issues
show
Unused Code introduced by
The call to ByJG\RestServer\Middlewa...:stopProcessingOthers() has too many arguments starting with ByJG\RestServer\OutputPr...lOutputProcessor::class. ( Ignorable by Annotation )

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

54
            return MiddlewareResult::/** @scrutinizer ignore-call */ stopProcessingOthers(HtmlOutputProcessor::class);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
55
        }
56
57
        return MiddlewareResult::continue();
58
    }
59
60
    /**
61
     * Get the Mime Type based on the filename
62
     *
63
     * @param string $filename
64
     * @return string
65
     */
66 6
    public function mimeContentType($filename)
67
    {
68 6
        if (!file_exists($filename)) {
69 1
            return null;
70
        }
71
72
        $prohibitedTypes = [
73
            "php",
74
            "vb",
75
            "cs",
76
            "rb",
77
            "py",
78
            "py3",
79
            "lua"
80
        ];
81
82 5
        $ext = substr(strrchr($filename, "."), 1);
83 5
        if (in_array($ext, $prohibitedTypes)) {
84 1
            throw new Error415Exception("File type not supported");
85
        }
86
87 4
        if (!function_exists('finfo_open')) {
88
            throw new Error500Exception("ServerStaticMiddleware requires finfo extension");
89
        }
90
91 4
        $finfo = finfo_open(FILEINFO_MIME);
92 4
        $mimetype = finfo_file($finfo, $filename);
93 4
        finfo_close($finfo);
94 4
        return $mimetype;
95
    }
96
97
}
98