|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the MindbazBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) David DELEVOYE <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Kozikaza\MindbazBundle\SwiftMailer; |
|
13
|
|
|
|
|
14
|
|
|
use Kozikaza\MindbazBundle\Exception\InvalidCampaignException; |
|
15
|
|
|
use Kozikaza\MindbazBundle\Exception\MissingSubscribersException; |
|
16
|
|
|
use Kozikaza\MindbazBundle\Manager\MessageManager; |
|
17
|
|
|
use Kozikaza\MindbazBundle\Manager\SubscriberManager; |
|
18
|
|
|
use Kozikaza\MindbazBundle\Model\Subscriber; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Vincent Chalamon <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class MindbazTransport implements \Swift_Transport |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var SubscriberManager |
|
27
|
|
|
*/ |
|
28
|
|
|
private $subscriberManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MessageManager |
|
32
|
|
|
*/ |
|
33
|
|
|
private $messageManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \Swift_Events_EventDispatcher |
|
37
|
|
|
*/ |
|
38
|
|
|
private $eventDispatcher; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $campaigns; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var bool |
|
47
|
|
|
*/ |
|
48
|
|
|
private $insertMissingSubscribers = false; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string|null |
|
52
|
|
|
*/ |
|
53
|
|
|
private $campaign; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param SubscriberManager $subscriberManager |
|
57
|
|
|
* @param MessageManager $messageManager |
|
58
|
|
|
* @param \Swift_Events_EventDispatcher $eventDispatcher |
|
59
|
|
|
* @param array $campaigns |
|
60
|
|
|
* @param bool $insertMissingSubscribers |
|
61
|
|
|
*/ |
|
62
|
9 |
|
public function __construct(SubscriberManager $subscriberManager, MessageManager $messageManager, \Swift_Events_EventDispatcher $eventDispatcher, array $campaigns, $insertMissingSubscribers) |
|
63
|
|
|
{ |
|
64
|
9 |
|
$this->subscriberManager = $subscriberManager; |
|
65
|
9 |
|
$this->messageManager = $messageManager; |
|
66
|
9 |
|
$this->eventDispatcher = $eventDispatcher; |
|
67
|
9 |
|
$this->campaigns = $campaigns; |
|
68
|
9 |
|
$this->insertMissingSubscribers = $insertMissingSubscribers; |
|
69
|
9 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string|null $campaign |
|
73
|
|
|
* |
|
74
|
|
|
* @return MindbazTransport |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function setCampaign($campaign = null) |
|
77
|
|
|
{ |
|
78
|
3 |
|
$this->campaign = $campaign; |
|
79
|
|
|
|
|
80
|
3 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param bool $insertMissingSubscribers |
|
85
|
|
|
* |
|
86
|
|
|
* @return MindbazTransport |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function setInsertMissingSubscribers($insertMissingSubscribers) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->insertMissingSubscribers = $insertMissingSubscribers; |
|
91
|
|
|
|
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function isStarted() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function start() |
|
107
|
|
|
{ |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function stop() |
|
114
|
|
|
{ |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
4 |
|
public function send(\Swift_Mime_Message $message, &$failedRecipients = null) |
|
121
|
|
|
{ |
|
122
|
|
|
// Security: a valid campaign is required |
|
123
|
4 |
|
if (null === $this->campaign || !array_key_exists($this->campaign, $this->campaigns)) { |
|
124
|
1 |
|
throw new InvalidCampaignException(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Find subscribers by email addresses |
|
128
|
3 |
|
$emails = array_map('strtolower', array_keys($message->getTo())); |
|
129
|
3 |
|
$subscribers = $this->subscriberManager->findByEmail($emails); |
|
130
|
|
|
|
|
131
|
3 |
|
$invalid = array_diff($emails, array_map(function (Subscriber $subscriber) { |
|
132
|
1 |
|
return $subscriber->getEmail(); |
|
133
|
3 |
|
}, $subscribers)); |
|
134
|
|
|
|
|
135
|
|
|
// Don't insert missing subscribers |
|
136
|
3 |
|
if (false === $this->insertMissingSubscribers && 0 < count($invalid)) { |
|
137
|
1 |
|
throw new MissingSubscribersException($invalid); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Insert missing subscribers |
|
141
|
2 |
|
foreach ($invalid as $email) { |
|
142
|
1 |
|
$subscribers[] = $this->subscriberManager->create(['email' => $email]); |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
// Send email |
|
146
|
2 |
|
foreach ($subscribers as $subscriber) { |
|
147
|
2 |
|
$this->messageManager->send($this->campaigns[$this->campaign], $subscriber, $message); |
|
148
|
2 |
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
return count($subscribers); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function registerPlugin(\Swift_Events_EventListener $plugin) |
|
157
|
|
|
{ |
|
158
|
1 |
|
$this->eventDispatcher->bindEventListener($plugin); |
|
159
|
1 |
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|