Completed
Push — master ( 3323aa...a1dfe7 )
by Dusan
02:01
created

Dispatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 68
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A insert() 0 5 1
A extract() 0 4 1
A isEmpty() 0 4 1
A process() 0 17 3
A __invoke() 0 5 1
A __clone() 0 4 1
1
<?php
2
/**
3
 * This file is part of the http-middleware package.
4
 *
5
 * Copyright (c) Dusan Vejin
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace WeCodeIn\Http\ServerMiddleware;
14
15
use Interop\Http\Factory\ResponseFactoryInterface;
16
use Interop\Http\ServerMiddleware\DelegateInterface;
17
use Interop\Http\ServerMiddleware\MiddlewareInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use WeCodeIn\Http\ServerMiddleware\Exception\InvalidMiddlewareResponseException;
21
22
/**
23
 * @author Dusan Vejin <[email protected]>
24
 */
25
class Dispatcher implements DelegateInterface
26
{
27
    /**
28
     * @var \SplPriorityQueue
29
     */
30
    protected $queue;
31
32
    /**
33
     * @var int
34
     */
35
    protected $serial = PHP_INT_MAX;
36
37
    /**
38
     * @var ResponseFactoryInterface
39
     */
40
    protected $responseFactory;
41
42 4
    public function __construct(ResponseFactoryInterface $responseFactory)
43
    {
44 4
        $this->queue = new \SplPriorityQueue();
45 4
        $this->responseFactory = $responseFactory;
46 4
    }
47
48 3
    public function insert(MiddlewareInterface $middleware, int $priority = 0) : Dispatcher
49
    {
50 3
        $this->queue->insert($middleware, [$priority, $this->serial--]);
51 3
        return $this;
52
    }
53
54 3
    public function extract() : MiddlewareInterface
55
    {
56 3
        return $this->queue->extract();
57
    }
58
59 4
    public function isEmpty() : bool
60
    {
61 4
        return $this->queue->isEmpty();
62
    }
63
64 4
    public function process(ServerRequestInterface $request) : ResponseInterface
65
    {
66 4
        if ($this->isEmpty()) {
67 3
            return $this->responseFactory->createResponse();
68
        }
69
70 3
        $middleware = $this->extract();
71 3
        $response = $middleware->process($request, $this);
72
73 3
        if (!$response instanceof ResponseInterface) {
74 1
            throw new InvalidMiddlewareResponseException(sprintf(
75 1
                'Middleware %s did not return a %s', get_class($middleware), ResponseInterface::class
76
            ));
77
        }
78
79 2
        return $response;
80
    }
81
82 2
    public function __invoke(ServerRequestInterface $request) : ResponseInterface
83
    {
84 2
        $delegate = clone $this;
85 2
        return $delegate->process($request);
86
    }
87
88 2
    public function __clone()
89
    {
90 2
        $this->queue = clone $this->queue;
91 2
    }
92
}
93