1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Quantum PHP Framework |
||
5 | * |
||
6 | * An open source software development framework for PHP |
||
7 | * |
||
8 | * @package Quantum |
||
9 | * @author Arman Ag. <[email protected]> |
||
10 | * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
||
11 | * @link http://quantum.softberg.org/ |
||
12 | * @since 2.9.0 |
||
13 | */ |
||
14 | |||
15 | namespace Quantum\Libraries\Mailer\Adapters; |
||
16 | |||
17 | use Quantum\Libraries\Mailer\MailerInterface; |
||
18 | use Quantum\Libraries\Curl\HttpClient; |
||
19 | use Quantum\Libraries\Mailer\MailTrap; |
||
20 | use Exception; |
||
21 | |||
22 | /** |
||
23 | * class MailgunAdapter |
||
24 | * @package Quantum\Libraries\Mailer |
||
25 | */ |
||
26 | class MailgunAdapter implements MailerInterface |
||
27 | { |
||
28 | |||
29 | use MailerAdapterTrait; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public $name = 'Mailgun'; |
||
35 | |||
36 | /** |
||
37 | * @var HttpClient |
||
38 | */ |
||
39 | private $httpClient; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $apiKey; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $apiUrl = 'https://api.mailgun.net/v3/'; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $data = []; |
||
55 | |||
56 | /** |
||
57 | * @var MailgunAdapter|null |
||
58 | */ |
||
59 | private static $instance = null; |
||
60 | |||
61 | /** |
||
62 | * MailgunAdapter constructor |
||
63 | * @param array $params |
||
64 | */ |
||
65 | private function __construct(array $params) |
||
66 | { |
||
67 | $this->httpClient = new HttpClient(); |
||
68 | |||
69 | $this->apiKey = $params['api_key']; |
||
70 | $this->apiUrl .= $params['domain'] . '/messages'; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get Instance |
||
75 | * @param array $params |
||
76 | * @return MailgunAdapter |
||
77 | */ |
||
78 | public static function getInstance(array $params): MailgunAdapter |
||
79 | { |
||
80 | if (self::$instance === null) { |
||
81 | self::$instance = new self($params); |
||
82 | } |
||
83 | |||
84 | return self::$instance; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Prepares the data |
||
89 | */ |
||
90 | private function prepare() |
||
0 ignored issues
–
show
|
|||
91 | { |
||
92 | $this->data['from'] = $this->from['name'] . " " . $this->from['email']; |
||
93 | |||
94 | $to = []; |
||
95 | foreach ($this->addresses as $address) { |
||
96 | $to[] = $address['email']; |
||
97 | } |
||
98 | |||
99 | $this->data['to'] = implode(',', $to); |
||
100 | |||
101 | if ($this->subject) { |
||
102 | $this->data['subject'] = $this->subject; |
||
103 | } |
||
104 | |||
105 | if ($this->message) { |
||
106 | if ($this->templatePath) { |
||
107 | $body = $this->createFromTemplate(); |
||
108 | } else { |
||
109 | $body = is_array($this->message) ? implode($this->message) : $this->message; |
||
110 | } |
||
111 | |||
112 | $this->data['html'] = trim(str_replace("\n", "", $body)); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return bool |
||
118 | */ |
||
119 | private function sendEmail(): bool |
||
0 ignored issues
–
show
|
|||
120 | { |
||
121 | try { |
||
122 | $this->httpClient |
||
123 | ->createRequest($this->apiUrl) |
||
124 | ->setMethod('POST') |
||
125 | ->setHeaders([ |
||
126 | 'Authorization' => 'Basic ' . base64_encode('api:' . $this->apiKey), |
||
127 | 'Content-Type' => 'application/x-www-form-urlencoded' |
||
128 | ]) |
||
129 | ->setData(json_encode($this->data)) |
||
130 | ->start(); |
||
131 | |||
132 | return true; |
||
133 | } catch (Exception $e) { |
||
134 | return false; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return bool |
||
140 | * @throws Exception |
||
141 | */ |
||
142 | private function saveEmail(): bool |
||
0 ignored issues
–
show
|
|||
143 | { |
||
144 | return MailTrap::getInstance()->saveMessage($this->getMessageId(), $this->getMessageContent()); |
||
145 | } |
||
146 | } |
||
147 |
This check looks for private methods that have been defined, but are not used inside the class.