|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2020 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2020 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Provider; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\CoreBundle\MessageHandler\Message\MessageInterface; |
|
20
|
|
|
use SWP\Bundle\CoreBundle\Model\FailedEntry; |
|
21
|
|
|
use Symfony\Component\Messenger\Envelope; |
|
22
|
|
|
use Symfony\Component\Messenger\Stamp\RedeliveryStamp; |
|
23
|
|
|
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp; |
|
24
|
|
|
use Symfony\Component\Messenger\Stamp\StampInterface; |
|
25
|
|
|
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp; |
|
26
|
|
|
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; |
|
27
|
|
|
|
|
28
|
|
|
class FailedEntriesProvider |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var ReceiverInterface */ |
|
31
|
|
|
private $receiver; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(ReceiverInterface $receiver) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->receiver = $receiver; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getFailedEntries(?int $max): array |
|
39
|
|
|
{ |
|
40
|
|
|
$envelopes = $this->receiver->all($max); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$rows = []; |
|
43
|
|
|
$history = []; |
|
44
|
|
|
foreach ($envelopes as $envelope) { |
|
45
|
|
|
$lastRedeliveryStampWithException = $this->getLastRedeliveryStampWithException($envelope); |
|
46
|
|
|
/** @var SentToFailureTransportStamp|null $sentToFailureTransportStamp */ |
|
47
|
|
|
$sentToFailureTransportStamp = $envelope->last(SentToFailureTransportStamp::class); |
|
48
|
|
|
|
|
49
|
|
|
/** @var TransportMessageIdStamp $stamp */ |
|
50
|
|
|
$stamp = $envelope->last(TransportMessageIdStamp::class); |
|
51
|
|
|
$redeliveryStamps = $this->getRedeliveryStamps($envelope); |
|
52
|
|
|
foreach ($redeliveryStamps as $redeliveryStamp) { |
|
53
|
|
|
$history[] = $redeliveryStamp->getRedeliveredAt(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$rows[] = new FailedEntry( |
|
57
|
|
|
(int) $stamp->getId(), |
|
58
|
|
|
get_class($envelope->getMessage()), |
|
59
|
|
|
null === $lastRedeliveryStampWithException ? null : $lastRedeliveryStampWithException->getRedeliveredAt(), |
|
60
|
|
|
null === $lastRedeliveryStampWithException ? null : $lastRedeliveryStampWithException->getExceptionMessage(), |
|
61
|
|
|
$sentToFailureTransportStamp->getOriginalReceiverName(), |
|
62
|
|
|
$history, |
|
63
|
|
|
$envelope->getMessage() instanceof MessageInterface ? $envelope->getMessage()->toArray() : [], |
|
64
|
|
|
$this->getExceptionTraceAsString($lastRedeliveryStampWithException) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $rows; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getLastRedeliveryStampWithException(Envelope $envelope): ?StampInterface |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var RedeliveryStamp $stamp */ |
|
74
|
|
|
foreach (array_reverse($this->getRedeliveryStamps($envelope)) as $stamp) { |
|
75
|
|
|
if (null !== $stamp->getExceptionMessage()) { |
|
76
|
|
|
return $stamp; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getRedeliveryStamps(Envelope $envelope): array |
|
84
|
|
|
{ |
|
85
|
|
|
return $envelope->all(RedeliveryStamp::class); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function getExceptionTraceAsString(StampInterface $lastRedeliveryStampWithException): ?string |
|
89
|
|
|
{ |
|
90
|
|
|
/* @var RedeliveryStamp $lastRedeliveryStampWithException */ |
|
91
|
|
|
return null === $lastRedeliveryStampWithException ? null : $lastRedeliveryStampWithException->getFlattenException()->getTraceAsString(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: