MessageTaskRunner   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
eloc 22
c 1
b 0
f 1
dl 0
loc 63
ccs 25
cts 25
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handledStampReturn() 0 13 3
A __invoke() 0 10 2
A handlerOutput() 0 15 3
A supports() 0 3 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner;
4
5
use Symfony\Component\Messenger\Envelope;
6
use Symfony\Component\Messenger\MessageBusInterface;
7
use Symfony\Component\Messenger\Stamp\HandledStamp;
8
use Symfony\Component\Messenger\Stamp\SentStamp;
9
use Zenstruck\ScheduleBundle\Schedule\Task;
10
use Zenstruck\ScheduleBundle\Schedule\Task\MessageTask;
11
use Zenstruck\ScheduleBundle\Schedule\Task\Result;
12
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner;
13
14
/**
15
 * @experimental This is experimental and may experience BC breaks
16
 *
17
 * @author Kevin Bond <[email protected]>
18
 */
19
final class MessageTaskRunner implements TaskRunner
20
{
21
    /** @var MessageBusInterface */
22
    private $bus;
23 6
24
    public function __construct(MessageBusInterface $bus)
25 6
    {
26 6
        $this->bus = $bus;
27
    }
28
29
    /**
30
     * @param MessageTask $task
31 6
     */
32
    public function __invoke(Task $task): Result
33 6
    {
34 6
        $envelope = $this->bus->dispatch($task->getMessage(), $task->getStamps());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle\Schedule\Task\MessageTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $envelope = $this->bus->dispatch($task->/** @scrutinizer ignore-call */ getMessage(), $task->getStamps());
Loading history...
Bug introduced by
The method getStamps() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle\Schedule\Task\MessageTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $envelope = $this->bus->dispatch($task->getMessage(), $task->/** @scrutinizer ignore-call */ getStamps());
Loading history...
35
        $output = $this->handlerOutput($envelope);
36 6
37 1
        if (empty($output)) {
38
            return Result::failure($task, 'Message not handled or sent to transport.');
39
        }
40 5
41
        return Result::successful($task, \implode("\n", $output));
42
    }
43 6
44
    public function supports(Task $task): bool
45 6
    {
46
        return $task instanceof MessageTask;
47
    }
48 6
49
    /**
50 6
     * @return string[]
51
     */
52 6
    private function handlerOutput(Envelope $envelope): array
53
    {
54 3
        $output = [];
55
56
        foreach ($envelope->all(HandledStamp::class) as $stamp) {
57 6
            /** @var HandledStamp $stamp */
58
            $output[] = \sprintf('Handled by: "%s", return: %s', $stamp->getHandlerName(), $this->handledStampReturn($stamp));
59 3
        }
60
61
        foreach ($envelope->all(SentStamp::class) as $stamp) {
62 6
            /** @var SentStamp $stamp */
63
            $output[] = \sprintf('Sent to: "%s"', $stamp->getSenderClass());
64
        }
65 3
66
        return $output;
67 3
    }
68
69
    private function handledStampReturn(HandledStamp $stamp): string
70 3
    {
71 3
        $result = $stamp->getResult();
72
73 1
        switch (true) {
74 1
            case null === $result:
75
                return '(none)';
76
77 1
            case \is_scalar($result):
78
                return \sprintf('(%s) "%s"', get_debug_type($result), $result);
79
        }
80
81
        return \sprintf('(%s)', get_debug_type($result));
82
    }
83
}
84