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

ConditionalRedirectMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 15
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 3
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