Completed
Push — master ( 756481...5cbf4f )
by Andrew
02:54
created

Emitter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 5
c 9
b 0
f 1
lcom 1
cbo 2
dl 0
loc 49
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A emit() 0 6 1
A createChain() 0 18 3
1
<?php namespace Cairns\Radiate;
2
3
use Cairns\Radiate\Middleware\Middleware;
4
use Cairns\Radiate\Middleware\InvalidMiddlewareException;
5
6
final class Emitter
7
{
8
    /**
9
     * @var callable
10
     */
11
    private $pipeline;
12
13
    /**
14
     * @param Middleware[] $middleware
15
     */
16 2
    public function __construct($middleware)
17
    {
18 2
        $this->pipeline = $this->createChain($middleware);
19 1
    }
20
21
    /**
22
     * @param $event
23
     * @return mixed
24
     */
25 1
    public function emit($event)
26
    {
27 1
        $pipeline = $this->pipeline;
28
29 1
        return $pipeline($event);
30
    }
31
32
    /**
33
     * @param Middleware[] $middleware
34
     * @return callable
35
     */
36 2
    private function createChain($middleware)
37
    {
38
        $last = function () {
39
            // nothing
40 2
        };
41
42 2
        while ($link = array_pop($middleware)) {
43 2
            if (! $link instanceof Middleware) {
44 1
                throw new InvalidMiddlewareException('Shitballs!');
45
            }
46
47 1
            $last = function ($event) use ($link, $last) {
48 1
                return $link->execute($event, $last);
49 1
            };
50 1
        }
51
52 1
        return $last;
53
    }
54
}
55