Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

ReduceHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 12 1
1
<?php
2
declare(strict_types=1);
3
4
namespace hiapi\Core\Endpoint\Middleware;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use League\Tactician\Middleware;
8
9
/**
10
 * Class ReduceHandler takes a single command, packs it as
11
 * is was called like a bulk command, and passes it to the [[handler]].
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class ReduceHandler implements Middleware
16
{
17
    /**
18
     * @var callable
19
     */
20
    private $handler;
21
22
    public function __construct(callable $handler)
23
    {
24
        $this->handler = $handler;
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function execute($command, callable $next)
31
    {
32
        $handler = $this->handler;
33
34
        $bulkCommand = new ArrayCollection([$command]);
35
        /** @var \IteratorAggregate $result */
36
        $result = $handler($bulkCommand);
37
38
        $resultArray = iterator_to_array($result, true);
39
40
        return reset($resultArray);
41
    }
42
}
43