Passed
Push — master ( f24dfb...067f89 )
by Jelmer
02:23
created

AuthorizationPipeline::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace jschreuder\MiddleAuth\Basic;
4
5
use jschreuder\MiddleAuth\AuthorizationHandlerInterface;
6
use jschreuder\MiddleAuth\AuthorizationPipelineInterface;
7
use jschreuder\MiddleAuth\AuthorizationRequestInterface;
8
use jschreuder\MiddleAuth\AuthorizationResponseInterface;
9
use jschreuder\MiddleAuth\Util\AuthLoggerInterface;
10
use jschreuder\MiddleAuth\Util\NullAuthLogger;
11
12
final class AuthorizationPipeline implements AuthorizationPipelineInterface
13
{
14 12
    public function __construct(
15
        private \SplQueue $queue,
16
        private ?AuthLoggerInterface $logger = null
17
    )
18
    {
19 12
        if (is_null($logger)) {
20 7
            $this->logger = new NullAuthLogger();
21
        }
22
    }
23
24 4
    public function withHandler(AuthorizationHandlerInterface $handler): self
25
    {
26 4
        $newQueue = clone $this->queue;
27 4
        $newQueue->enqueue($handler);
28 4
        return new self($newQueue, $this->logger);
29
    }
30
31 10
    public function process(AuthorizationRequestInterface $request): AuthorizationResponseInterface
32
    {
33 10
        if ($this->queue->count() === 0) {
34 3
            $this->logger->warning('Authorization pipeline is empty, no handlers to process');
0 ignored issues
show
Bug introduced by
The method warning() does not exist on null. ( Ignorable by Annotation )

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

34
            $this->logger->/** @scrutinizer ignore-call */ warning('Authorization pipeline is empty, no handlers to process');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35 3
            throw new \RuntimeException('Pipeline is empty, no handlers to process.');
36
        }
37
38 7
        $this->logger->debug('Authorization pipeline processing request', [
39 7
            'subject_type' => $request->getSubject()->getType(),
40 7
            'subject_id' => $request->getSubject()->getId(),
41 7
            'resource_type' => $request->getResource()->getType(),
42 7
            'resource_id' => $request->getResource()->getId(),
43 7
            'action' => $request->getAction(),
44 7
        ]);
45
46 7
        $queue = clone $this->queue;
47 7
        $handler = $queue->dequeue();
48
49 7
        $response = $handler->handle($request);
50
51 7
        $this->logger->info(
52 7
            'Authorization decision: ' . ($response->isPermitted() ? 'PERMIT' : 'DENY'),
53 7
            [
54 7
                'subject_type' => $request->getSubject()->getType(),
55 7
                'subject_id' => $request->getSubject()->getId(),
56 7
                'resource_type' => $request->getResource()->getType(),
57 7
                'resource_id' => $request->getResource()->getId(),
58 7
                'action' => $request->getAction(),
59 7
                'permitted' => $response->isPermitted(),
60 7
                'reason' => $response->getReason(),
61 7
                'handler' => $response->getHandler(),
62 7
            ]
63 7
        );
64
65 7
        return $response;
66
    }
67
}
68