1 | <?php |
||
26 | class Message extends HttpApi |
||
27 | { |
||
28 | 14 | public function getBatchMessage(string $domain, bool $autoSend = true): BatchMessage |
|
32 | |||
33 | /** |
||
34 | * @see https://documentation.mailgun.com/en/latest/api-sending.html#sending |
||
35 | * |
||
36 | * @return SendResponse|ResponseInterface |
||
37 | */ |
||
38 | 1 | public function send(string $domain, array $params) |
|
39 | { |
||
40 | 1 | Assert::string($domain); |
|
41 | 1 | Assert::notEmpty($domain); |
|
42 | 1 | Assert::notEmpty($params); |
|
43 | |||
44 | 1 | $postDataMultipart = []; |
|
45 | 1 | $fields = ['attachment', 'inline']; |
|
46 | 1 | foreach ($fields as $fieldName) { |
|
47 | 1 | if (!isset($params[$fieldName])) { |
|
48 | 1 | continue; |
|
49 | } |
||
50 | |||
51 | 1 | Assert::isArray($params[$fieldName]); |
|
52 | 1 | foreach ($params[$fieldName] as $file) { |
|
53 | 1 | $postDataMultipart[] = $this->prepareFile($fieldName, $file); |
|
54 | } |
||
55 | |||
56 | 1 | unset($params[$fieldName]); |
|
57 | } |
||
58 | |||
59 | 1 | $postDataMultipart = array_merge($this->prepareMultipartParameters($params), $postDataMultipart); |
|
60 | 1 | $response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart); |
|
61 | 1 | $this->closeResources($postDataMultipart); |
|
62 | |||
63 | return $this->hydrateResponse($response, SendResponse::class); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @see https://documentation.mailgun.com/en/latest/api-sending.html#sending |
||
68 | * |
||
69 | * @param array $recipients with all you send emails to. Including bcc and cc |
||
70 | * @param string $message Message filepath or content |
||
71 | * |
||
72 | * @return SendResponse|ResponseInterface |
||
73 | */ |
||
74 | 2 | public function sendMime(string $domain, array $recipients, string $message, array $params) |
|
75 | { |
||
76 | 2 | Assert::string($domain); |
|
77 | 2 | Assert::notEmpty($domain); |
|
78 | 2 | Assert::notEmpty($recipients); |
|
79 | 2 | Assert::notEmpty($message); |
|
80 | 2 | Assert::nullOrIsArray($params); |
|
|
|||
81 | |||
82 | 2 | $params['to'] = $recipients; |
|
83 | 2 | $postDataMultipart = $this->prepareMultipartParameters($params); |
|
84 | |||
85 | 2 | if (strlen($message) < PHP_MAXPATHLEN && is_file($message)) { |
|
86 | $fileData = ['filePath' => $message]; |
||
87 | } else { |
||
88 | $fileData = [ |
||
89 | 2 | 'fileContent' => $message, |
|
90 | 2 | 'filename' => 'message', |
|
91 | ]; |
||
92 | } |
||
93 | 2 | $postDataMultipart[] = $this->prepareFile('message', $fileData); |
|
94 | 2 | $response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart); |
|
95 | 2 | $this->closeResources($postDataMultipart); |
|
96 | |||
97 | return $this->hydrateResponse($response, SendResponse::class); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get stored message. |
||
102 | * |
||
103 | * @see https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages |
||
104 | * |
||
105 | * @param bool $rawMessage if true we will use "Accept: message/rfc2822" header |
||
106 | * |
||
107 | * @return ShowResponse|ResponseInterface |
||
108 | */ |
||
109 | 2 | public function show(string $url, bool $rawMessage = false) |
|
110 | { |
||
111 | 2 | Assert::notEmpty($url); |
|
112 | |||
113 | 2 | $headers = []; |
|
114 | 2 | if ($rawMessage) { |
|
115 | 1 | $headers['Accept'] = 'message/rfc2822'; |
|
116 | } |
||
117 | |||
118 | 2 | $response = $this->httpGet($url, [], $headers); |
|
119 | |||
120 | return $this->hydrateResponse($response, ShowResponse::class); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param array $filePath array('fileContent' => 'content') or array('filePath' => '/foo/bar') |
||
125 | * |
||
126 | * @throws InvalidArgumentException |
||
127 | */ |
||
128 | 3 | private function prepareFile(string $fieldName, array $filePath): array |
|
157 | |||
158 | /** |
||
159 | * Prepare multipart parameters. Make sure each POST parameter is split into an array with 'name' and 'content' keys. |
||
160 | */ |
||
161 | 3 | private function prepareMultipartParameters(array $params): array |
|
176 | |||
177 | /** |
||
178 | * Close open resources. |
||
179 | */ |
||
180 | 3 | private function closeResources(array $params): void |
|
188 | } |
||
189 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.