MiddlewareQueue   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 36
ccs 7
cts 7
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getIterator() 0 3 1
A add() 0 4 1
1
<?php
2
/**
3
 * @author  PhileCMS
4
 * @link    https://philecms.github.io
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Http;
9
10
use Iterator;
11
use IteratorAggregate;
12
use IteratorIterator;
13
use Psr\Http\Server\MiddlewareInterface;
14
use SplPriorityQueue;
15
16
/**
17
 * Middleware queue
18
 */
19
class MiddlewareQueue implements IteratorAggregate
20
{
21
    public const DEFAULT_PRIORITY = 100;
22
23
    /** @var int counter for FIFO order for items with same priority */
24
    protected $serial = PHP_INT_MAX;
25
26
    /** @var SplPriorityQueue middleware */
27
    protected $queue;
28
29 8
    public function __construct()
30
    {
31 8
        $this->queue = new SplPriorityQueue();
32
    }
33
34
    /**
35
     * Adds middleware to queue
36
     *
37
     * @param MiddlewareInterface $middleware
38
     * @param int $priority
39
     * @return self
40
     */
41 8
    public function add(MiddlewareInterface $middleware, int $priority = self::DEFAULT_PRIORITY): self
42
    {
43 8
        $this->queue->insert($middleware, [$priority, $this->serial--]);
44 8
        return $this;
45
    }
46
47
    /**
48
     * Implements IteratorAggregate
49
     *
50
     * @return \Iterator
51
     */
52 8
    public function getIterator(): Iterator
53
    {
54 8
        return new IteratorIterator($this->queue);
55
    }
56
}
57