1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\EventDispatcher; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit; |
10
|
|
|
use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface; |
11
|
|
|
use Symfony\Component\Mercure\PublisherInterface; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
class NotifyVisitToMercure |
15
|
|
|
{ |
16
|
|
|
private PublisherInterface $publisher; |
17
|
|
|
private MercureUpdatesGeneratorInterface $updatesGenerator; |
18
|
|
|
private EntityManagerInterface $em; |
19
|
|
|
private LoggerInterface $logger; |
20
|
|
|
|
21
|
3 |
|
public function __construct( |
22
|
|
|
PublisherInterface $publisher, |
23
|
|
|
MercureUpdatesGeneratorInterface $updatesGenerator, |
24
|
|
|
EntityManagerInterface $em, |
25
|
|
|
LoggerInterface $logger |
26
|
|
|
) { |
27
|
3 |
|
$this->publisher = $publisher; |
28
|
3 |
|
$this->em = $em; |
29
|
3 |
|
$this->logger = $logger; |
30
|
3 |
|
$this->updatesGenerator = $updatesGenerator; |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
public function __invoke(VisitLocated $shortUrlLocated): void |
34
|
|
|
{ |
35
|
3 |
|
$visitId = $shortUrlLocated->visitId(); |
36
|
|
|
|
37
|
|
|
/** @var Visit|null $visit */ |
38
|
3 |
|
$visit = $this->em->find(Visit::class, $visitId); |
39
|
3 |
|
if ($visit === null) { |
40
|
1 |
|
$this->logger->warning('Tried to notify mercure for visit with id "{visitId}", but it does not exist.', [ |
41
|
1 |
|
'visitId' => $visitId, |
42
|
|
|
]); |
43
|
1 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
2 |
|
($this->publisher)($this->updatesGenerator->newVisitUpdate($visit)); |
48
|
1 |
|
} catch (Throwable $e) { |
49
|
1 |
|
$this->logger->debug('Error while trying to notify mercure hub with new visit. {e}', [ |
50
|
1 |
|
'e' => $e, |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|