Guardian::guard()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 3
nc 3
nop 3
1
<?php
2
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
 */
12
class Guardian
13
{
14
    /**
15
     * Instantiate a guard from an attribute.
16
     */
17
    protected function instantiate(\ReflectionAttribute $attribute): Guard
18
    {
19
        return $attribute->newInstance();
20
    }
21
22
    /**
23
     * Run a set of guards.
24
     */
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