AuthorizationHandler::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
c 1
b 0
f 0
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