Passed
Push — master ( 0675d7...59fbae )
by Kevin
02:33
created

MessageTaskRunner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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
    private $bus;
22
23 6
    public function __construct(MessageBusInterface $bus)
24
    {
25 6
        $this->bus = $bus;
26 6
    }
27
28
    /**
29
     * @param MessageTask|Task $task
30
     */
31 6
    public function __invoke(Task $task): Result
32
    {
33 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

33
        $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

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