· 6 years ago · Jan 13, 2020, 01:52 PM
1<?php
2/*
3 * @copyright 2014 Mautic Contributors. All rights reserved
4 * @author Mautic
5 *
6 * @link http://mautic.org
7 *
8 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
9 */
10
11namespace Mautic\EmailBundle\Swiftmailer\Transport;
12
13use Mautic\EmailBundle\Helper\MailHelper;
14use Mautic\EmailBundle\Model\TransportCallback;
15use Mautic\LeadBundle\Entity\DoNotContact;
16use Symfony\Component\HttpFoundation\Request;
17use Symfony\Component\Translation\TranslatorInterface;
18use \Swift_Mailer;
19
20/**
21 * Class NewsmanTransport.
22 */
23class NewsmanTransport extends AbstractTokenHttpTransport implements \Swift_Transport, CallbackTransportInterface
24{
25 public $apiKey, $user;
26 /**
27 * @var TranslatorInterface
28 */
29 private $translator;
30
31 /**
32 * @var TransportCallback
33 */
34 private $transportCallback;
35
36 /**
37 * NewsmanTransport constructor.
38 *
39 * @param TranslatorInterface $translator
40 * @param TransportCallback $transportCallback
41 */
42 public function __construct(TranslatorInterface $translator, TransportCallback $transportCallback)
43 {
44 $this->translator = $translator;
45 $this->transportCallback = $transportCallback;
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 protected function getPayload()
52 {
53
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 protected function getHeaders()
60 {
61 }
62
63 /**
64 * Send the given Message.
65 *
66 * Recipient/sender data will be retrieved from the Message API.
67 * The return value is the number of recipients who were accepted for delivery.
68 *
69 * @param Swift_Mime_Message $message
70 * @param string[] $failedRecipients An array of failures by-reference
71 *
72 * @return int
73 */
74 public function send(\Swift_Mime_Message $message, &$failedRecipients = null)
75 {
76 $failedRecipients = (array)$failedRecipients;
77 $recipients = array();
78 foreach ($message->getTo() as $email => $name)
79 {
80 $recipients[$email] = $name;
81 }
82 foreach ($message->getCc() as $email => $name)
83 {
84 $recipients[$email] = $name;
85 }
86 foreach ($message->getBcc() as $email => $name)
87 {
88 $recipients[$email] = $name;
89 }
90 $json_data = array(
91 "mime_message" => $message->toString(),
92 "recipients" => array_keys($recipients),
93 "account_id" => $this->user,
94 "key" => $this->apiKey
95 );
96
97 $api_url = $this->getApiEndpoint();
98
99 $ch = curl_init($api_url . "message.send_raw");
100 curl_setopt($ch, CURLOPT_POST, 1);
101 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_data));
102 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
103 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
104 $response = curl_exec($ch);
105 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
106 $result = @json_decode($response, true);
107 if ($http_code != 200)
108 {
109 if ($http_code == 500 && is_array($result) && array_key_exists("err", $result))
110 {
111 throw new Swift_TransportException(
112 $result["err"]
113 );
114 } else
115 {
116 throw new Swift_TransportException(
117 "Could not call http method. Response code: $http_code - $result"
118 );
119 }
120 }
121 return count($result);
122 }
123
124
125 /**
126 * {@inheritdoc}
127 */
128 public function getApiEndpoint()
129 {
130 return "https://cluster.newsmanapp.com/api/1.0/";
131 }
132
133 public function getEndpoint()
134 {
135
136 $apiKey = $this->getApiKey();
137 $user = $this->getUsername();
138 return 'https://ssl.newsman.app/api/1.2/rest/' . $user . '/' . $apiKey . '/';
139 }
140
141 /**
142 * Start this Transport mechanism.
143 */
144 public function start()
145 {
146 $this->apiKey = $this->getApiKey();
147 $this->user = $this->getUsername();
148
149 if (empty($this->apiKey))
150 {
151 // BC support @deprecated - remove in 3.0
152 $this->apiKey = $this->getPassword();
153 }
154
155 // Make an API call to the ping endpoint
156
157 $this->post(
158 [
159 'url' => $this->getEndpoint() . 'list.all.json',
160 'payload' => json_encode(
161 [
162
163 ]
164 ),
165 ]
166 );
167
168 $this->started = true;
169 }
170
171 /**
172 * {@inheritdoc}
173 *
174 * @param $response
175 * @param $info
176 *
177 * @return array
178 *
179 * @throws \Swift_TransportException
180 */
181 protected function handlePostResponse($response, $info)
182 {
183 $parsedResponse = '';
184 $response = json_decode($response, true);
185
186 if ($response === false)
187 {
188 $parsedResponse = $response;
189 }
190
191 if (!$this->started)
192 {
193 if (empty($response))
194 {
195 $message = 'Newsman failed to authenticate';
196 $this->throwException($message);
197 return [];
198 }
199 }
200
201 $return = [];
202 $metadata = $this->getMetadata();
203
204 var_dump($metadata);
205
206 if ($evt = $this->getDispatcher()->createResponseEvent($this, $parsedResponse, ($info['http_code'] == 200)))
207 {
208 $this->getDispatcher()->dispatchEvent($evt, 'responseReceived');
209 }
210
211 if ($response === false)
212 {
213 $this->throwException('Unexpected response');
214 } elseif (!empty($error))
215 {
216 $this->throwException('Newsman error');
217 }
218
219 return $return;
220 }
221
222 /**
223 * Returns a "transport" string to match the URL path /mailer/{transport}/callback.
224 *
225 * @return mixed
226 */
227 public function getCallbackPath()
228 {
229 return 'newsman';
230 }
231
232 /**
233 * @return int
234 */
235 public function getMaxBatchLimit()
236 {
237 return 0;
238 }
239
240 /**
241 * @param \Swift_Message $message
242 * @param int $toBeAdded
243 * @param string $type
244 *
245 * @return int
246 */
247 public function getBatchRecipientCount(\Swift_Message $message, $toBeAdded = 1, $type = 'to')
248 {
249 return 0;
250 }
251
252 /**
253 * Handle response.
254 *
255 * @param Request $request
256 */
257 public function processCallbackRequest(Request $request)
258 {
259
260 }
261}