MiddlewareQueue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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