CollectionHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tasksuki\Component\Handler;
4
5
use Tasksuki\Component\Message\Message;
6
7
/**
8
 * Class CollectionHandler
9
 *
10
 * @package Tasksuki\Component\Handler
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class CollectionHandler implements HandlerInterface
14
{
15
    /**
16
     * @var HandlerInterface[]
17
     */
18
    private $handlers;
19
20
    /**
21
     * @var HandlerInterface[]
22
     */
23
    private $sorted;
24
25 3
    public function __construct(array $handlers = [])
26
    {
27 2
        $this->handlers = (
28 3
            function (HandlerInterface ...$handlers) {
29 2
                return $handlers;
30 3
            }
31 3
        )(...$handlers);
32 2
    }
33
34
    /**
35
     * @param Message $message
36
     */
37 2
    public function handle(Message $message)
38
    {
39 2
        foreach ($this->getHandlers() as $handler) {
40 2
            $handler->handle($message);
41
        }
42 2
    }
43
44 2
    public function addHandler(HandlerInterface $handler, int $priority = 0)
45
    {
46 2
        $this->handlers[$priority][] = $handler;
47 2
        $this->sorted = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Tas...dler\HandlerInterface>> of property $sorted.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48 2
    }
49
50
    /**
51
     * @return HandlerInterface[]
52
     */
53 2
    public function getHandlers(): array
54
    {
55 2
        if (null === $this->sorted) {
56 2
            $this->sortHandlers();
57
        }
58
59 2
        return $this->sorted;
60
    }
61
62 2
    private function sortHandlers()
63
    {
64 2
        krsort($this->handlers);
65 2
        $this->sorted = call_user_func_array('array_merge', $this->handlers);
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func_array('ar...erge', $this->handlers) of type * is incompatible with the declared type array<integer,object<Tas...dler\HandlerInterface>> of property $sorted.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
    }
67
}