StaticHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 55
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 5 2
B resolveRequest() 0 45 9
1
<?php
2
3
namespace Lepton\Core\Handler;
4
use Lepton\Routing\Match\{Match404, MatchFile};
5
use Lepton\Http\Response\FileResponse;
6
use Lepton\Core\Application;
7
use Lepton\Http\Response\NotFoundResponse;
8
9
class StaticHandler extends AbstractHandler
10
{
11
    public function resolveRequest() : MatchFile|Match404{
12
13
        // Get the requested file path
14
        $url = $this->request->url;
15
        $url = preg_replace("/^".str_replace("/", "\/", Application::getAppConfig()->base_url)."\//", "", $url);
16
17
18
        $root = Application::$documentRoot;
19
        $root .= Application::getAppConfig()->base_url;
20
        $filePath = $root."/".Application::getAppConfig()->static_files_dir. "/". $url;
21
22
        // Check if the file exists
23
        if (file_exists($filePath)) {
24
            // Get the file extension
25
            $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
26
            // Set content type based on the file extension
27
            switch ($fileExtension) {
28
                case 'png':
29
                    $contentType = 'image/png';
30
                    break;
31
                case 'jpg':
32
                case 'jpeg':
33
                    $contentType = 'image/jpeg';
34
                    break;
35
                case 'gif':
36
                    $contentType = 'image/gif';
37
                    break;
38
                case 'css':
39
                    $contentType = 'text/css';
40
                    break;
41
                case 'js':
42
                    $contentType = 'text/javascript';
43
                    break;
44
                case 'pdf':
45
                    $contentType = 'application/pdf';
46
                    break;
47
                default:
48
                    $contentType = '';
49
                    break;
50
            }
51
            //die($contentType);
52
            return new MatchFile(filePath: $filePath, contentType: $contentType);
53
        } else {
54
            die($filePath);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Lepton\Routing\Match\Mat...Routing\Match\MatchFile. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
55
          return new Match404();
0 ignored issues
show
Unused Code introduced by
return new Lepton\Routing\Match\Match404() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
56
        }
57
    }
58
59
    public function handle($match): FileResponse|NotFoundResponse{
60
      if($match instanceof Match404){
61
        return new NotFoundResponse();
62
      }
63
      return new FileResponse($match->filePath, $match->contentType);
64
    }
65
}
66