1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\EventListener; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventSubscriber; |
8
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
9
|
|
|
use Odiseo\SyliusMailchimpPlugin\Handler\StoreRegisterHandlerInterface; |
10
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
11
|
|
|
|
12
|
|
|
final class StoreSubscriber implements EventSubscriber |
13
|
|
|
{ |
14
|
|
|
/** @var StoreRegisterHandlerInterface */ |
15
|
|
|
private $storeRegisterHandler; |
16
|
|
|
|
17
|
|
|
public function __construct(StoreRegisterHandlerInterface $storeRegisterHandler) |
18
|
|
|
{ |
19
|
|
|
$this->storeRegisterHandler = $storeRegisterHandler; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getSubscribedEvents(): array |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
'postPersist', |
29
|
|
|
'postUpdate', |
30
|
|
|
'postRemove', |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param LifecycleEventArgs $args |
36
|
|
|
*/ |
37
|
|
|
public function postPersist(LifecycleEventArgs $args): void |
38
|
|
|
{ |
39
|
|
|
$channel = $args->getEntity(); |
40
|
|
|
|
41
|
|
|
if ($channel instanceof ChannelInterface) { |
42
|
|
|
$this->register($channel); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param LifecycleEventArgs $args |
48
|
|
|
*/ |
49
|
|
|
public function postUpdate(LifecycleEventArgs $args): void |
50
|
|
|
{ |
51
|
|
|
$channel = $args->getEntity(); |
52
|
|
|
|
53
|
|
|
if ($channel instanceof ChannelInterface) { |
54
|
|
|
$this->register($channel); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param LifecycleEventArgs $args |
60
|
|
|
*/ |
61
|
|
|
public function postRemove(LifecycleEventArgs $args): void |
62
|
|
|
{ |
63
|
|
|
$channel = $args->getEntity(); |
64
|
|
|
|
65
|
|
|
if ($channel instanceof ChannelInterface) { |
66
|
|
|
$this->unregister($channel); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ChannelInterface $channel |
72
|
|
|
*/ |
73
|
|
|
private function register(ChannelInterface $channel): void |
74
|
|
|
{ |
75
|
|
|
$this->storeRegisterHandler->register($channel); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param ChannelInterface $channel |
80
|
|
|
*/ |
81
|
|
|
private function unregister(ChannelInterface $channel): void |
82
|
|
|
{ |
83
|
|
|
$this->storeRegisterHandler->unregister($channel); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|