-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriberManager.php
More file actions
128 lines (112 loc) · 3.77 KB
/
SubscriberManager.php
File metadata and controls
128 lines (112 loc) · 3.77 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Perform\MailingListBundle;
use Doctrine\ORM\EntityManagerInterface;
use Perform\MailingListBundle\Connector\ConnectorInterface;
use Perform\MailingListBundle\Enricher\EnricherInterface;
use Perform\BaseBundle\DependencyInjection\LoopableServiceLocator;
use Perform\MailingListBundle\Entity\Subscriber;
use Perform\MailingListBundle\Exception\ConnectorNotFoundException;
use Psr\Log\LoggerInterface;
/**
* @author Glynn Forrest <me@glynnforrest.com>
**/
class SubscriberManager
{
protected $em;
protected $connectors;
protected $enrichers;
protected $logger;
protected $signups = [];
/**
* @param EntityManagerInterface $em
* @param LoopableServiceLocator $connectors
* @param LoopableServiceLocator $enrichers
* @param LoggerInterface $logger
*/
public function __construct(EntityManagerInterface $em, LoopableServiceLocator $connectors, LoopableServiceLocator $enrichers, LoggerInterface $logger)
{
$this->em = $em;
$this->connectors = $connectors;
$this->enrichers = $enrichers;
$this->logger = $logger;
}
/**
* @return string
*/
public function getDefaultConnectorName()
{
if (empty($names = $this->connectors->getNames())) {
throw new ConnectorNotFoundException('No mailing list connectors are registered.');
}
return $names[0];
}
/**
* Get a mailing list connector. If no name is given, return the default connector.
*
* @param string|null $name
*
* @return ConnectorInterface[]
*/
public function getConnector($name = null)
{
if (!$name) {
$name = $this->getDefaultConnectorName();
}
if (!$this->connectors->has($name)) {
throw new ConnectorNotFoundException(sprintf('The mailing list connector "%s" was not found.', $name));
}
return $this->connectors->get($name);
}
public function addSubscriber(Subscriber $subscriber)
{
if (!$subscriber->getConnectorName()) {
$subscriber->setConnectorName($this->getDefaultConnectorName());
}
$this->em->persist($subscriber);
$this->em->flush();
$this->signups[] = $subscriber;
}
public function flush()
{
if (empty($this->signups)) {
return;
}
$this->em->beginTransaction();
try {
foreach ($this->enrichers as $enricher) {
$enricher->enrich($this->signups);
}
foreach ($this->signups as $subscriber) {
$this->getConnector($subscriber->getConnectorName())->subscribe($subscriber);
$this->em->remove($subscriber);
$this->logger->info(
sprintf('Created new subscriber "%s" with connector "%s".',
$subscriber->getEmail(),
$subscriber->getConnectorName()),
[
'email' => $subscriber->getEmail(),
'list' => $subscriber->getList(),
'connector' => $subscriber->getConnectorName(),
'attributes' => $subscriber->getAttributes(),
]);
}
$this->em->flush();
$this->signups = [];
$this->em->commit();
} catch (\Exception $e) {
$this->em->rollback();
throw $e;
}
}
public function processQueue($batchSize)
{
$repo = $this->em->getRepository('PerformMailingListBundle:Subscriber');
while (true) {
$this->signups = $repo->findBy([], [], $batchSize);
if (empty($this->signups)) {
return;
}
$this->flush();
}
}
}