Initialization

This commit is contained in:
Loann 2020-04-09 10:50:49 +02:00
commit f6712b5b13
4 changed files with 222 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

42
BMailApi.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace BMail;
use GuzzleHttp\Client;
class BMailApi
{
/**
* The API key
*
* @var string
*/
private $api_key;
public function __construct(string $api_key)
{
$this->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());
}
}
}

7
BMailException.php Normal file
View File

@ -0,0 +1,7 @@
<?php
namespace BMail;
class BMailException extends \Exception {}

172
BMailTemplate.php Normal file
View File

@ -0,0 +1,172 @@
<?php
namespace BMail;
class BMailTemplate implements \JsonSerializable
{
/**
* The template for the message
*
* @var integer
*/
public $template;
/**
* The sender for the message
*
* @var array
*/
public $sender = [];
/**
* The recipients for the message
*
* @var array
*/
public $recipients = [];
/**
* The reply address for the message
*
* @var array
*/
public $replyTo = [];
/**
* The parameters for the message
*
* @var array
*/
public $parameters = [];
public function __construct(int $template)
{
$this->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();
}
}