Passed
Push — main ( b29b0b...aea47e )
by Roberto
12:37
created

ConditionalRedirectMiddleware::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Lepton\Middleware;
4
5
use Lepton\Http\Request;
6
use Lepton\Http\Response\{HttpResponse, RedirectResponse};
7
use Lepton\Middleware\AbstractMiddleware;
8
use Lepton\Core\Application;
9
10
abstract class ConditionalRedirectMiddleware extends AbstractMiddleware
11
{
12
    protected function handle(mixed ...$middlewareParams): HttpResponse|Request
13
    {
14
        $url = preg_replace("/^" . str_replace("/", "\/", Application::getAppConfig()->base_url) . "\//", "", $this->request->url);
15
16
        if(!in_array($url, $middlewareParams["allow"])){
17
            if($this->check_request($middlewareParams)){
18
                return new RedirectResponse($middlewareParams["redirect"], htmx: false, parse: false);
19
            }
20
        }
21
        return $this->request;
22
   }
23
24
    abstract function check_request($middlewareParams);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
}
26