PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/mailgun/mailgun-php/src/Mailgun/Messages/MessageBuilder.php

https://gitlab.com/m.ghanbari/mailgun-subscriber
PHP | 484 lines | 280 code | 59 blank | 145 comment | 52 complexity | 3fab0fdff04f875d0051020dbae44316 MD5 | raw file
  1. <?PHP
  2. namespace Mailgun\Messages;
  3. use Mailgun\Constants\Api;
  4. use Mailgun\Constants\ExceptionMessages;
  5. use Mailgun\Messages\Exceptions\InvalidParameter;
  6. use Mailgun\Messages\Exceptions\TooManyParameters;
  7. /**
  8. * This class is used for composing a properly formed
  9. * message object. Dealing with arrays can be cumbersome,
  10. * this class makes the process easier. See the official
  11. * documentation (link below) for usage instructions.
  12. *
  13. * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
  14. */
  15. class MessageBuilder
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $message = array();
  21. /**
  22. * @var array
  23. */
  24. protected $variables = array();
  25. /**
  26. * @var array
  27. */
  28. protected $files = array();
  29. /**
  30. * @var array
  31. */
  32. protected $counters = array(
  33. 'recipients' => array(
  34. 'to' => 0,
  35. 'cc' => 0,
  36. 'bcc' => 0
  37. ),
  38. 'attributes' => array(
  39. 'attachment' => 0,
  40. 'campaign_id' => 0,
  41. 'custom_option' => 0,
  42. 'tag' => 0
  43. )
  44. );
  45. /**
  46. * @param array $params
  47. * @param string $key
  48. * @param mixed $default
  49. * @return mixed
  50. */
  51. protected function safeGet($params, $key, $default)
  52. {
  53. if (array_key_exists($key, $params)) {
  54. return $params[$key];
  55. }
  56. return $default;
  57. }
  58. /**
  59. * @param array $params
  60. * @return mixed|string
  61. */
  62. protected function getFullName($params)
  63. {
  64. if (array_key_exists("first", $params)) {
  65. $first = $this->safeGet($params, "first", "");
  66. $last = $this->safeGet($params, "last", "");
  67. return trim("$first $last");
  68. }
  69. return $this->safeGet($params, "full_name", "");
  70. }
  71. /**
  72. * @param string $address
  73. * @param array $variables
  74. * @return string
  75. */
  76. protected function parseAddress($address, $variables)
  77. {
  78. if (!is_array($variables)) {
  79. return $address;
  80. }
  81. $fullName = $this->getFullName($variables);
  82. if ($fullName != null) {
  83. return "'$fullName' <$address>";
  84. }
  85. return $address;
  86. }
  87. /**
  88. * @param string $headerName
  89. * @param string $address
  90. * @param array $variables
  91. */
  92. protected function addRecipient($headerName, $address, $variables)
  93. {
  94. $compiledAddress = $this->parseAddress($address, $variables);
  95. if (isset($this->message[$headerName])) {
  96. array_push($this->message[$headerName], $compiledAddress);
  97. } elseif ($headerName == "h:reply-to") {
  98. $this->message[$headerName] = $compiledAddress;
  99. } else {
  100. $this->message[$headerName] = array($compiledAddress);
  101. }
  102. if (array_key_exists($headerName, $this->counters['recipients'])) {
  103. $this->counters['recipients'][$headerName] += 1;
  104. }
  105. }
  106. /**
  107. * @param string $address
  108. * @param array|null $variables
  109. * @return mixed
  110. * @throws TooManyParameters
  111. */
  112. public function addToRecipient($address, $variables = null)
  113. {
  114. if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) {
  115. throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
  116. }
  117. $this->addRecipient("to", $address, $variables);
  118. return end($this->message['to']);
  119. }
  120. /**
  121. * @param string $address
  122. * @param array|null $variables
  123. * @return mixed
  124. * @throws TooManyParameters
  125. */
  126. public function addCcRecipient($address, $variables = null)
  127. {
  128. if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) {
  129. throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
  130. }
  131. $this->addRecipient("cc", $address, $variables);
  132. return end($this->message['cc']);
  133. }
  134. /**
  135. * @param string $address
  136. * @param array|null $variables
  137. * @return mixed
  138. * @throws TooManyParameters
  139. */
  140. public function addBccRecipient($address, $variables = null)
  141. {
  142. if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) {
  143. throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
  144. }
  145. $this->addRecipient("bcc", $address, $variables);
  146. return end($this->message['bcc']);
  147. }
  148. /**
  149. * @param string $address
  150. * @param array|null $variables
  151. * @return mixed
  152. */
  153. public function setFromAddress($address, $variables = null)
  154. {
  155. $this->addRecipient("from", $address, $variables);
  156. return $this->message['from'];
  157. }
  158. /**
  159. * @param string $address
  160. * @param array|null $variables
  161. * @return mixed
  162. */
  163. public function setReplyToAddress($address, $variables = null)
  164. {
  165. $this->addRecipient("h:reply-to", $address, $variables);
  166. return $this->message['h:reply-to'];
  167. }
  168. /**
  169. * @param string $subject
  170. * @return mixed
  171. */
  172. public function setSubject($subject = "")
  173. {
  174. if ($subject == null || $subject == "") {
  175. $subject = " ";
  176. }
  177. $this->message['subject'] = $subject;
  178. return $this->message['subject'];
  179. }
  180. /**
  181. * @param string $headerName
  182. * @param mixed $headerData
  183. * @return mixed
  184. */
  185. public function addCustomHeader($headerName, $headerData)
  186. {
  187. if (!preg_match("/^h:/i", $headerName)) {
  188. $headerName = "h:" . $headerName;
  189. }
  190. $this->message[$headerName] = array($headerData);
  191. return $this->message[$headerName];
  192. }
  193. /**
  194. * @param string $textBody
  195. * @return string
  196. */
  197. public function setTextBody($textBody)
  198. {
  199. if ($textBody == null || $textBody == "") {
  200. $textBody = " ";
  201. }
  202. $this->message['text'] = $textBody;
  203. return $this->message['text'];
  204. }
  205. /**
  206. * @param string $htmlBody
  207. * @return string
  208. */
  209. public function setHtmlBody($htmlBody)
  210. {
  211. if ($htmlBody == null || $htmlBody == "") {
  212. $htmlBody = " ";
  213. }
  214. $this->message['html'] = $htmlBody;
  215. return $this->message['html'];
  216. }
  217. /**
  218. * @param string $attachmentPath
  219. * @param string|null $attachmentName
  220. * @return bool
  221. */
  222. public function addAttachment($attachmentPath, $attachmentName = null)
  223. {
  224. if (isset($this->files["attachment"])) {
  225. $attachment = array(
  226. 'filePath' => $attachmentPath,
  227. 'remoteName' => $attachmentName
  228. );
  229. array_push($this->files["attachment"], $attachment);
  230. } else {
  231. $this->files["attachment"] = array(
  232. array(
  233. 'filePath' => $attachmentPath,
  234. 'remoteName' => $attachmentName
  235. )
  236. );
  237. }
  238. return true;
  239. }
  240. /**
  241. * @param string $inlineImagePath
  242. * @param string|null $inlineImageName
  243. *
  244. * @return bool|true
  245. * @throws InvalidParameter
  246. */
  247. public function addInlineImage($inlineImagePath, $inlineImageName = null)
  248. {
  249. if (strpos($inlineImagePath, '@') === 0) {
  250. if (isset($this->files['inline'])) {
  251. $inlineAttachment = array(
  252. 'filePath' => $inlineImagePath,
  253. 'remoteName' => $inlineImageName
  254. );
  255. array_push($this->files['inline'], $inlineAttachment);
  256. } else {
  257. $this->files['inline'] = array(
  258. array(
  259. 'filePath' => $inlineImagePath,
  260. 'remoteName' => $inlineImageName
  261. )
  262. );
  263. }
  264. return true;
  265. } else {
  266. throw new InvalidParameter(ExceptionMessages::INVALID_PARAMETER_INLINE);
  267. }
  268. }
  269. /**
  270. * @param boolean $testMode
  271. * @return string
  272. */
  273. public function setTestMode($testMode)
  274. {
  275. if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
  276. $testMode = "yes";
  277. } else {
  278. $testMode = "no";
  279. }
  280. $this->message['o:testmode'] = $testMode;
  281. return $this->message['o:testmode'];
  282. }
  283. /**
  284. * @param string|int $campaignId
  285. * @return string|int
  286. * @throws TooManyParameters
  287. */
  288. public function addCampaignId($campaignId)
  289. {
  290. if ($this->counters['attributes']['campaign_id'] < Api::CAMPAIGN_ID_LIMIT) {
  291. if (isset($this->message['o:campaign'])) {
  292. array_push($this->message['o:campaign'], $campaignId);
  293. } else {
  294. $this->message['o:campaign'] = array($campaignId);
  295. }
  296. $this->counters['attributes']['campaign_id'] += 1;
  297. return $this->message['o:campaign'];
  298. } else {
  299. throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_CAMPAIGNS);
  300. }
  301. }
  302. /**
  303. * @param string $tag
  304. * @throws TooManyParameters
  305. */
  306. public function addTag($tag)
  307. {
  308. if ($this->counters['attributes']['tag'] < Api::TAG_LIMIT) {
  309. if (isset($this->message['o:tag'])) {
  310. array_push($this->message['o:tag'], $tag);
  311. } else {
  312. $this->message['o:tag'] = array($tag);
  313. }
  314. $this->counters['attributes']['tag'] += 1;
  315. return $this->message['o:tag'];
  316. } else {
  317. throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_TAGS);
  318. }
  319. }
  320. /**
  321. * @param boolean $enabled
  322. * @return mixed
  323. */
  324. public function setDkim($enabled)
  325. {
  326. if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
  327. $enabled = "yes";
  328. } else {
  329. $enabled = "no";
  330. }
  331. $this->message["o:dkim"] = $enabled;
  332. return $this->message["o:dkim"];
  333. }
  334. /**
  335. * @param boolean $enabled
  336. * @return string
  337. */
  338. public function setOpenTracking($enabled)
  339. {
  340. if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
  341. $enabled = "yes";
  342. } else {
  343. $enabled = "no";
  344. }
  345. $this->message['o:tracking-opens'] = $enabled;
  346. return $this->message['o:tracking-opens'];
  347. }
  348. /**
  349. * @param boolean $enabled
  350. * @return string
  351. */
  352. public function setClickTracking($enabled)
  353. {
  354. if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
  355. $enabled = "yes";
  356. } elseif ($enabled == "html") {
  357. $enabled = "html";
  358. } else {
  359. $enabled = "no";
  360. }
  361. $this->message['o:tracking-clicks'] = $enabled;
  362. return $this->message['o:tracking-clicks'];
  363. }
  364. /**
  365. * @param string $timeDate
  366. * @param string|null $timeZone
  367. * @return string
  368. */
  369. public function setDeliveryTime($timeDate, $timeZone = null)
  370. {
  371. if (isset($timeZone)) {
  372. $timeZoneObj = new \DateTimeZone("$timeZone");
  373. } else {
  374. $timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE);
  375. }
  376. $dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
  377. $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822);
  378. $this->message['o:deliverytime'] = $formattedTimeDate;
  379. return $this->message['o:deliverytime'];
  380. }
  381. /**
  382. * @param string $customName
  383. * @param mixed $data
  384. */
  385. public function addCustomData($customName, $data)
  386. {
  387. $this->message['v:' . $customName] = json_encode($data);
  388. }
  389. /**
  390. * @param string $parameterName
  391. * @param mixed $data
  392. * @return mixed
  393. */
  394. public function addCustomParameter($parameterName, $data)
  395. {
  396. if (isset($this->message[$parameterName])) {
  397. array_push($this->message[$parameterName], $data);
  398. return $this->message[$parameterName];
  399. } else {
  400. $this->message[$parameterName] = array($data);
  401. return $this->message[$parameterName];
  402. }
  403. }
  404. /**
  405. * @param array $message
  406. */
  407. public function setMessage($message)
  408. {
  409. $this->message = $message;
  410. }
  411. /**
  412. * @return array
  413. */
  414. public function getMessage()
  415. {
  416. return $this->message;
  417. }
  418. /**
  419. * @return array
  420. */
  421. public function getFiles()
  422. {
  423. return $this->files;
  424. }
  425. }