BaseAccessControlMiddleware   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 32
rs 10
c 3
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPermissions() 0 6 2
A handle() 0 19 5
1
<?php
2
3
namespace Lepton\Middleware;
4
5
use Lepton\Authenticator\AccessControlAttributes\{LoginRequired, AbstractAccessControlAttribute};
6
use Lepton\Core\Application;
7
use Lepton\Http\Request;
8
use Lepton\Routing\Match\MatchRoute;
9
use Lepton\Http\Response\{HttpResponse, RedirectResponse};
10
11
12
class BaseAccessControlMiddleware extends AbstractMiddleware
13
{
14
15
    protected function handle(mixed ...$middlewareParams): HttpResponse|Request
16
    {
17
        if($this->match instanceof MatchRoute) {
18
            $reflection = new \ReflectionMethod($this->match->controller, $this->match->method);
19
            $attributes = $reflection->getAttributes();
20
21
            foreach ($attributes as $attribute) {
22
                if(is_subclass_of($attribute->getName(), AbstractAccessControlAttribute::class)) {
23
                    return
24
                        $this->checkPermissions($attribute->getName(),  ...($attribute->getArguments()))?
25
                        $this->request :
26
                        new RedirectResponse(
27
                            Application::getAuthConfig()->login_url,
28
                            redirect_after: $this->request->url
29
                        );
30
                }
31
32
            }
33
            return $this->request;
34
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 17 is false. This is incompatible with the type-hinted return Lepton\Http\Request|Lept...p\Response\HttpResponse. 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...
35
    }
36
37
38
    protected function checkPermissions(string $modifier, mixed ...$params):bool{
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

38
    protected function checkPermissions(string $modifier, /** @scrutinizer ignore-unused */ mixed ...$params):bool{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
        if($modifier == LoginRequired::class){
40
            $authenticator = new \Lepton\Authenticator\UserAuthenticator();
41
            return $authenticator->isLoggedIn();
42
        }
43
        return true;
44
    }
45
46
}
47