This commit is contained in:
2024-03-10 23:21:23 -04:00
parent 69a3c3f157
commit 562b740d68
4 changed files with 500 additions and 145 deletions

View File

@@ -15,57 +15,57 @@ class Client
/**
* @var TransportInterface
*/
private $_transport;
private TransportInterface $_transport;
/**
* @var array
*/
private $_to = [];
private array $_to = [];
/**
* @var array
*/
private $_cc = [];
private array $_cc = [];
/**
* @var array
*/
private $_bcc = [];
private array $_bcc = [];
/**
* @var array
*/
private $_files = [];
private array $_files = [];
/**
* @var string
*/
private $_from = '';
private string $_from = '';
/**
* @var string
*/
private $_subject = '(No Subject)';
private string $_subject = '(No Subject)';
/**
* @var string
*/
private $_body = '';
private string $_body = '';
/**
* @var bool
*/
private $_isHtml = false;
private bool $_isHtml = false;
/**
* @var bool
*/
private $_catch = false;
private bool $_catch = false;
/**
* @var bool|\DateTimeInterface
*/
private $_sendTime = false;
private bool|\DateTimeInterface $_sendTime = false;
/**
* Client constructor.
@@ -81,7 +81,7 @@ class Client
* @param string $to
* @throws ValidationException
*/
public function addTo(string $to)
public function addTo(string $to): void
{
if (!Validator::validateEmailAddress($to)) {
throw new ValidationException('Email address is invalid');
@@ -95,7 +95,7 @@ class Client
* @param array $to
* @throws ValidationException
*/
public function setAllTo(array $to)
public function setAllTo(array $to): void
{
foreach ($to as $item) {
if (!Validator::validateEmailAddress($item)) {
@@ -110,7 +110,7 @@ class Client
* @param string $cc
* @throws ValidationException
*/
public function addCc(string $cc)
public function addCc(string $cc): void
{
if (!Validator::validateEmailAddress($cc)) {
throw new ValidationException('Email address is invalid');
@@ -123,7 +123,7 @@ class Client
* @param string $bcc
* @throws ValidationException
*/
public function addBcc(string $bcc)
public function addBcc(string $bcc): void
{
if (!Validator::validateEmailAddress($bcc)) {
throw new ValidationException('Email address is invalid');
@@ -136,7 +136,7 @@ class Client
* @param string $body
* @param bool $isHtml
*/
public function setBody(string $body, bool $isHtml = false)
public function setBody(string $body, bool $isHtml = false): void
{
$this->_body = $body;
$this->_isHtml = $isHtml;
@@ -145,7 +145,7 @@ class Client
/**
* @param string $subject
*/
public function setSubject(string $subject)
public function setSubject(string $subject): void
{
$this->_subject = $subject;
}
@@ -155,7 +155,7 @@ class Client
*
* @throws ValidationException
*/
public function setFrom(string $from)
public function setFrom(string $from): void
{
if (!Validator::validateEmailAddress($from)) {
throw new ValidationException('Email address is invalid');
@@ -169,7 +169,7 @@ class Client
* @throws ValidationException
* @return mixed
*/
public function send(bool $catch = false)
public function send(bool $catch = false): mixed
{
$this->_catch = $catch;
$payload = $this->_buildPayload();
@@ -249,7 +249,7 @@ class Client
/**
* @throws ValidationException
*/
private function _validateFields()
private function _validateFields(): void
{
if (empty($this->_to)) {
throw new ValidationException('To Address is required');
@@ -265,7 +265,7 @@ class Client
*
* @throws ValidationException
*/
public function addAttachment(string $fileLocation)
public function addAttachment(string $fileLocation): void
{
if (!file_exists($fileLocation)) {
throw new ValidationException('File does not exist.');
@@ -276,7 +276,7 @@ class Client
/**
* @param \DateTimeInterface $sendTime
*/
public function sendTime(\DateTimeInterface $sendTime)
public function sendTime(\DateTimeInterface $sendTime): void
{
$this->_sendTime = $sendTime;
}

View File

@@ -3,6 +3,7 @@
namespace Siteworx\Mail\Transports;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use Psr\Log\LoggerInterface;
@@ -14,37 +15,37 @@ class ApiTransport implements TransportInterface
/**
* @var string
*/
private $_apiEndpoint = 'https://email.siteworxpro.com';
private string $_apiEndpoint = 'https://email.siteworxpro.com';
/**
* @var string
*/
private $_clientId = '';
private string $_clientId = '';
/**
* @var string
*/
private $_clientSecret = '';
private string $_clientSecret = '';
/**
* @var string
*/
private $_accessToken = '';
private string $_accessToken = '';
/**
* @var Guzzle
*/
private $_client;
private Guzzle $_client;
/**
* @var CacheInterface
* @var CacheInterface | null
*/
private $_cache = null;
private ?CacheInterface $_cache = null;
/**
* @var LoggerInterface
* @var LoggerInterface | null
*/
private $_logger = null;
private ?LoggerInterface $_logger = null;
/**
* Client constructor.
@@ -71,17 +72,17 @@ class ApiTransport implements TransportInterface
}
/**
* @param mixed $Cache
* @param mixed $cache
*/
public function setCache(CacheInterface $Cache)
public function setCache(CacheInterface $cache): void
{
$this->_cache = $Cache;
$this->_cache = $cache;
}
/**
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
$this->_logger = $logger;
}
@@ -97,7 +98,7 @@ class ApiTransport implements TransportInterface
/**
* @param string $clientId
*/
public function setClientId(string $clientId)
public function setClientId(string $clientId): void
{
$this->_clientId = $clientId;
}
@@ -105,16 +106,12 @@ class ApiTransport implements TransportInterface
/**
* @param string $clientSecret
*/
public function setClientSecret(string $clientSecret)
public function setClientSecret(string $clientSecret): void
{
$this->_clientSecret = $clientSecret;
}
/**
* @param array $payload
* @return \stdClass
*/
public function sentMailPayload(array $payload)
public function sentMailPayload(array $payload): \stdClass
{
$this->setToken();
@@ -139,7 +136,7 @@ class ApiTransport implements TransportInterface
$this->_logger->debug(\json_encode($body));
}
} catch (ServerException $exception) {
} catch (ServerException | RequestException | GuzzleException $exception) {
$result = $exception->getResponse();
$body = $result->getBody()->getContents();
@@ -150,19 +147,6 @@ class ApiTransport implements TransportInterface
$this->_logger->warning('An error occurred sending the email! (' . $result->getStatusCode() . ')');
$this->_logger->debug(\json_encode($body));
}
} catch (RequestException $exception) {
$result = $exception->getResponse();
$body = $result->getBody()->getContents();
$data = json_decode($body);
if ($this->_logger !== null) {
$this->_logger->warning('An error occurred sending the email! (' . $result->getStatusCode() . ')');
$this->_logger->debug(\json_encode($body));
}
}
return $data;
@@ -185,10 +169,7 @@ class ApiTransport implements TransportInterface
}
/**
* @return \stdClass
*/
private function refreshToken()
private function refreshToken(): void
{
$params = [
'scope' => 'default',
@@ -198,36 +179,32 @@ class ApiTransport implements TransportInterface
];
try {
$result = $this->_client->post($this->_apiEndpoint . '/access_token', [
'form_params' => $params
'json' => $params
]);
$body = $result->getBody()->getContents();
$data = json_decode($body);
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
$this->_accessToken = $data->access_token;
if ($this->_cache !== null) {
$this->_cache->set('access_token', $this->_accessToken, $data->expires_in);
$this->_cache?->set('access_token', $this->_accessToken, $data->expires_in);
} catch (\JsonException | ServerException | RequestException | GuzzleException $exception) {
$result = $exception->getResponse();
$body = $result?->getBody()->getContents();
try {
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
$this->_logger?->error($e->getMessage());
}
} catch (ServerException $exception) {
$result = $exception->getResponse();
$body = $result->getBody()->getContents();
$data = json_decode($body);
} catch (RequestException $exception) {
$result = $exception->getResponse();
$body = $result->getBody()->getContents();
$data = json_decode($body);
$this->_logger?->error($exception->getMessage() . ' ' . $data['message'] ?? '');
throw new \RuntimeException($exception);
}
return $data;
}
public function deleteEmail(string $uuid): bool