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

ServerStaticMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 73
ccs 22
cts 26
cp 0.8462
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mimeContentType() 0 29 4
A beforeProcess() 0 26 6
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 FastRoute\Dispatcher;
12
13
class ServerStaticMiddleware implements BeforeMiddlewareInterface
14
{
15
    /**
16
     * Undocumented function
17
     *
18
     * @param mixed $dispatcherStatus
19
     * @param HttpResponse $response
20
     * @param HttpRequest $request
21
     * @return MiddlewareResult
22
     */
23 2
    public function beforeProcess(
24
        $dispatcherStatus,
25
        HttpResponse $response,
26
        HttpRequest $request
27
    )
28
    {
29 2
        if ($dispatcherStatus != Dispatcher::NOT_FOUND) {
30
            return MiddlewareResult::continue();
31
        }
32
33 2
        $file = $_SERVER['SCRIPT_FILENAME'];
34 2
        if (!empty($file) && strpos($file, $_SERVER["SCRIPT_NAME"]) !== false && file_exists($file)) {
35 2
            $mime = $this->mimeContentType($file);
36
37 1
            if (empty($mime)) {
38
                return MiddlewareResult::continue();
39
            }
40
41 1
            $response->addHeader("Content-Type", $mime);
42 1
            $response->emptyResponse();
43 1
            $response->getResponseBag()->setSerializationRule(ResponseBag::RAW);
44 1
            $response->write(file_get_contents($file));
45 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

45
            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...
46
        }
47
48
        return MiddlewareResult::continue();
49
    }
50
51
    /**
52
     * Get the Mime Type based on the filename
53
     *
54
     * @param string $filename
55
     * @return string
56
     */
57 6
    public function mimeContentType($filename)
58
    {
59 6
        if (!file_exists($filename)) {
60 1
            return null;
61
        }
62
63
        $prohibitedTypes = [
64
            "php",
65
            "vb",
66
            "cs",
67
            "rb",
68
            "py",
69
            "py3",
70
            "lua"
71
        ];
72
73 5
        $ext = substr(strrchr($filename, "."), 1);
74 5
        if (in_array($ext, $prohibitedTypes)) {
75 1
            throw new Error415Exception("File type not supported");
76
        }
77
78 4
        if (!function_exists('finfo_open')) {
79
            throw new Error500Exception("ServerStaticMiddleware requires finfo extension");
80
        }
81
82 4
        $finfo = finfo_open(FILEINFO_MIME);
83 4
        $mimetype = finfo_file($finfo, $filename);
84 4
        finfo_close($finfo);
85 4
        return $mimetype;
86
    }
87
88
}
89