-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEventSubscriber.php
60 lines (49 loc) · 1.89 KB
/
EventSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Setono\SyliusFacebookPlugin\EventSubscriber;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Setono\MetaConversionsApiBundle\Event\ConversionsApiEventRaised;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Throwable;
// todo should be renamed to something more descriptive, i.e. 'CatchingEventSubscriber'
/**
* Since we do not deem Facebook to be 'mission critical', we will catch all errors related to
* sending an event to Facebook and log it as an error. This way the error won't interfere with any
* 'real' business, i.e. buying stuff, but it will still be logged correctly, so that developers can act upon it
*/
abstract class EventSubscriber implements EventSubscriberInterface, LoggerAwareInterface
{
private LoggerInterface $logger;
private EventDispatcherInterface $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->logger = new NullLogger();
$this->eventDispatcher = $eventDispatcher;
}
public function track(): void
{
try {
$event = $this->callback()(...func_get_args());
if (null === $event) {
return;
}
$this->eventDispatcher->dispatch(new ConversionsApiEventRaised($event));
} catch (Throwable $e) {
$this->logger->error($e->getMessage());
}
}
/**
* This callable will receive the event arguments coming from the originating event,
* and it must return a Setono\MetaConversionsApi\Event\Event
*
* @return callable(... mixed): ?\Setono\MetaConversionsApi\Event\Event
*/
abstract protected function callback(): callable;
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
}