SynchronousProcessor::process()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nexus;
6
7
use Nexus\Exceptions\TaskResultViolation;
8
use Psr\EventDispatcher\ListenerProviderInterface;
9
use Psr\EventDispatcher\StoppableTaskInterface;
10
use Psr\EventDispatcher\TaskInterface;
11
use Psr\EventDispatcher\TaskProcessorInterface;
12
13
class SynchronousProcessor implements TaskProcessorInterface
14
{
15
    private $listenerProvider;
16
17 3
    public function __construct(ListenerProviderInterface $listenerProvider)
18
    {
19 3
        $this->listenerProvider = $listenerProvider;
20 3
    }
21
22 3
    public function process(TaskInterface $task): TaskInterface
23
    {
24 3
        foreach ($this->listenerProvider->getListenersForEvent($task) as $listener) {
25 3
            $task = call_user_func($listener, $task);
26 3
            if (!is_object($task) || !($task instanceof TaskInterface)) {
27 1
                throw new TaskResultViolation();
28
            }
29 3
            if ($task instanceof StoppableTaskInterface && $task->isPropagationStopped()) {
30 3
                break;
31
            }
32
        }
33 2
        return $task;
34
    }
35
}
36