From f6712b5b13bc29ef4447073b7507ecfe103dfc05 Mon Sep 17 00:00:00 2001 From: Loann Meignant Date: Thu, 9 Apr 2020 10:50:49 +0200 Subject: [PATCH] Initialization --- .gitignore | 1 + BMailApi.php | 42 +++++++++++++ BMailException.php | 7 +++ BMailTemplate.php | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+) create mode 100644 .gitignore create mode 100644 BMailApi.php create mode 100644 BMailException.php create mode 100644 BMailTemplate.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/BMailApi.php b/BMailApi.php new file mode 100644 index 0000000..8232742 --- /dev/null +++ b/BMailApi.php @@ -0,0 +1,42 @@ +api_key = $api_key; + } + + public function send($template) + { + $url = "http://127.0.0.1:9000/api/project/template/send"; + + // TODO +// $url = "http://127.0.0.1:9000/api/project/template/send"; + + $client = new Client(); + + try { + return $client->request('post', $url, [ + 'form_params' => $template, + 'headers' => ['Authorization' => 'Bearer ' . $this->api_key] + ])->getBody(); + + } catch(\Exception $e) { + throw new BMailException($e->getMessage()); + } + } +} diff --git a/BMailException.php b/BMailException.php new file mode 100644 index 0000000..00ce41b --- /dev/null +++ b/BMailException.php @@ -0,0 +1,7 @@ +template = $template; + } + + /** + * @param string $address + * @param string|null $name + * @return BMailTemplate + */ + public function sender(string $address, string $name = null): BMailTemplate + { + $this->sender['address'] = $address; + + if ($name) + $this->sender['name'] = $name; + + return $this; + } + + /** + * @param string $address + * @param string|null $name + * @return BMailTemplate + */ + public function replyTo(string $address, string $name = null): BMailTemplate + { + $this->replyTo['address'] = $address; + + if ($name) + $this->replyTo['name'] = $name; + + return $this; + } + + /** + * @param array $recipients + * @return BMailTemplate + */ + public function recipients(array $recipients): BMailTemplate + { + if (!empty($recipients)) { + foreach ($recipients as $recipient) { + if (!empty($recipient[0]) && is_string($recipient[0])) { + $data = [ + 'address' => $recipient[0] + ]; + + if (!empty($recipient[1])) { + if (!empty($recipient[1]['name'])) + $data['name'] = $recipient[1]['name']; + + if (!empty($recipient[1]['parameters'])) + $data['parameters'] = $recipient[1]['parameters']; + } + + $this->recipients[] = $data; + } + } + } + + return $this; + } + + /** + * @param string $address + * @param array $informations + * @return BMailTemplate + */ + public function addRecipient(string $address, array $informations = []): BMailTemplate + { + $data = [ + 'address' => $address + ]; + + if (!empty($informations)) { + if (!empty($informations['name'])) + $data['name'] = $informations['name']; + + if (!empty($informations['parameters'])) + $data['parameters'] = $informations['parameters']; + } + + $this->recipients[] = $data; + return $this; + } + + /** + * @param array $parameters + * @return BMailTemplate + */ + public function parameters(array $parameters): BMailTemplate + { + $this->parameters = $parameters; + return $this; + } + + /** + * @param string $key + * @param string $value + * @return BMailTemplate + */ + public function addParameter(string $key, string $value): BMailTemplate + { + $this->parameters[$key] = $value; + return $this; + } + + /** + * Get an array representation of the message. + * + * @return array + */ + public function toArray() + { + return [ + 'template' => $this->template, + 'sender' => $this->sender, + 'replyTo' => $this->replyTo, + 'recipients' => $this->recipients, + 'parameters' => $this->parameters, + ]; + } + + public function jsonSerialize() + { + return $this->toArray(); + } + +}