AuthorizationHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 3
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace jschreuder\MiddleAuth\Basic;
4
5
use jschreuder\MiddleAuth\AuthorizationHandlerInterface;
6
use jschreuder\MiddleAuth\AuthorizationMiddlewareInterface;
7
use jschreuder\MiddleAuth\AuthorizationRequestInterface;
8
use jschreuder\MiddleAuth\AuthorizationResponseInterface;
9
10
final class AuthorizationHandler implements AuthorizationHandlerInterface
11
{
12
    private \SplQueue $queue;
13
    private bool $called = false;
14
15 6
    public function __construct(\SplQueue $queue)
16
    {
17 6
        $this->queue = $queue;
18
    }
19
20 4
    public function handle(AuthorizationRequestInterface $request): AuthorizationResponseInterface
21
    {
22 4
        if ($this->queue->count() === 0) {
23 1
            throw new \RuntimeException('No more handlers to call on.');
24
        }
25 3
        if ($this->called) {
26 1
            throw new \RuntimeException('Already processed, cannot be ran twice.');
27
        }
28
29
        /** @var  AuthorizationMiddlewareInterface $next */
30 3
        $next = $this->queue->dequeue();
31 3
        $this->called = true;
32
33 3
        return $next->process($request, new self($this->queue));
34
    }
35
}
36