Failed Conditions
Push — master ( a26426...65249a )
by Arnold
14:53 queued 04:44
created

Guardian   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A guard() 0 17 3
A instantiate() 0 3 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jasny\Controller;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
/**
9
 * Apply guards for a controller.
10
 * Guard are attributes of the controller or method.
11
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
12
class Guardian
13
{
14
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $attribute should have a doc-comment as per coding-style.
Loading history...
15
     * Instantiate a guard from an attribute.
16
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
17
    protected function instantiate(\ReflectionAttribute $attribute): Guard
18
    {
19
        return $attribute->newInstance();
20
    }
21
22
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $subject should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $response should have a doc-comment as per coding-style.
Loading history...
23
     * Run a set of guards.
24
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
25
    public function guard(
26
        \ReflectionObject|\ReflectionMethod $subject,
27
        ServerRequestInterface $request,
28
        ResponseInterface $response,
29
    ): ?ResponseInterface {
30
        $attributes = $subject->getAttributes(Guard::class, \ReflectionAttribute::IS_INSTANCEOF);
31
32
        foreach ($attributes as $attribute) {
33
            $guard = $this->instantiate($attribute);
34
            $result = $guard($request, $response);
35
36
            if ($result !== null) {
37
                return $result;
38
            }
39
        }
40
41
        return null;
42
    }
43
}
44