Runner::setPathResolver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PTS\PSR15\MiddlewareManager;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
class Runner implements RequestHandlerInterface
11
{
12
    /** @var MiddlewareManager */
13
    protected $manager;
14
    /** @var int */
15
    protected $index = 0;
16
17
    /** @var PathResolver|null */
18
    protected $pathResolver;
19
20
    public function __construct(MiddlewareManager $manager)
21
    {
22
        $this->manager = $manager;
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function handle(ServerRequestInterface $request): ResponseInterface
29
    {
30
        [$next, $path] = $this->manager->get($this->index);
31
32
        if (null === $next) {
33
            throw new \OutOfRangeException('Handler not found');
34
        }
35
36
        $this->rewindBy(1);
37
        $response = $this->callNext($path, $request, $next);
38
        $this->rewindBy(-1);
39
40
        return $response;
41
    }
42
43
    protected function callNext(?string $path, ServerRequestInterface $request, MiddlewareInterface $next)
44
    {
45
        if (null === $path) {
46
            return $next->process($request, $this);
47
        }
48
49
        return $this->pathResolver->isMatch($path, $request)
0 ignored issues
show
Bug introduced by
The method isMatch() 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

49
        return $this->pathResolver->/** @scrutinizer ignore-call */ isMatch($path, $request)

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...
50
            ? $next->process($request, $this)
51
            : $this->handle($request);
52
    }
53
54
    public function setPathResolver(?PathResolver $resolver): void
55
    {
56
        $this->pathResolver = $resolver;
57
    }
58
59
    protected function rewindBy(int $val = 1): void
60
    {
61
        $this->index += $val;
62
    }
63
}
64