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

Guardian::instantiate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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