- Work done on the Bug Email component

This commit is contained in:
Dave Mc Nicoll committed 2019-08-21 15:54:17 -04:00
1 parent ec873145d2
commit a7dcdf4ee7
8 files changed
+286 -139

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
<?php
namespace TheBugs\Email;
class EmailConfiguration
{
const AUTH_TYPE_SMTP = 1;
protected $type;
public $smtpHost = "";
public $smtpUsername = "";
public $smtpPassword = "";
public $smtpPort = 25;
public $smtpUseTLS = false;
public function __construct($type = self::AUTH_TYPE_SMTP)
{
$this->type = $type;
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace TheBugs\Email;
interface MailerInterface
{
public function send(string $subject, string $message, bool $html = true) : bool;
public function setFrom($from) : self;
public function setTo($to) : self;
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace TheBugs\Email;
class SwiftMailer implements MailerInterface
{
protected $emailConfiguration;
protected $transport;
protected $from;
protected $to;
public function __construct(EmailConfiguration $configuration) {
$this->emailConfiguration = $configuration;
$this->transport = ( new \Swift_SmtpTransport($this->emailConfiguration->smtpHost, $this->emailConfiguration->smtpPort) )
->setUsername($this->emailConfiguration->smtpUsername)
->setPassword($this->emailConfiguration->smtpPassword);
}
public function send(string $subject, string $message, bool $html = true) : bool
{
$swiftObj = ( new \Swift_Message($subject) )
->setFrom( $this->from )
->setTo( $this->to )
->setBody( $message, $html ? 'text/html' : 'text/plain' );
return ( new \Swift_Mailer($this->transport) )->send($swiftObj);
}
public function setFrom($from) : MailerInterface
{
$this->from = $from;
return $this;
}
public function setTo($to) : MailerInterface
{
$this->to = $to;
return $this;
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace TheBugs;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response,
Zend\Diactoros\ServerRequest,
Zend\Diactoros\Stream,
Zend\Diactoros\Uri;
class EmailErrorMiddleware implements MiddlewareInterface
{
protected /* EmailConfiguration */ $emailConfiguration;
protected /* Callable */ $callable;
protected /* Mailer */ $mailer;
public function __construct(Email\EmailConfiguration $emailConfiguration, Email\MailerInterface $mailer, Callable $callable)
{
$this->emailConfiguration = $emailConfiguration;
$this->callable = $callable;
$this->mailer = $mailer;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$response = $handler->handle($request);
if ( $response->getStatusCode() === 500 ) {
$this->mailer->setTo('dave.mcnicoll@cslsj.qc.ca');
$this->mailer->setFrom(['test@johndoe.com' => 'John Doe']);
$bugReport = $response->getBody()->__toString();
if ( false === ( $this->mailer->send(error_get_last()['message'], $bugReport, true) ) ) {
error_log("Impossile to send an email bug report from " . static::class);
}
return $this->callable->call($this);
}
return $response;
}
}
+70 -70
View File
@@ -1,70 +1,70 @@
<?php
namespace TheBugs;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response,
Zend\Diactoros\ServerRequest,
Zend\Diactoros\Stream,
Zend\Diactoros\Uri;
class JavascriptMiddleware implements MiddlewareInterface
{
const FILTERS = [
"code" => null,
"headers" => [
"User-Agent" => "TheBugs/1.0",
],
# Callable functions
"functions" => []
];
/**
* Filter list, allows custom invokable too
* @var array
*/
protected $filters = [];
public function __construct($options = [])
{
foreach(static::FILTERS as $key => $default) {
$this->filters[$key] = ( $options[$key] ?? false ) ? $options[$key] : $default;
}
}
public function throwError($errorDetails)
{
throw new Exception\JavascriptException($errorDetails['message'], 0, null, $errorDetails['location'], $errorDetails['line'], $errorDetails['url'], (array) $errorDetails['stack']);
}
/**
* Process a server request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ( ( $this->filters['code'] ?? false ) && ($this->filters['code'] !== $request->getCode())) {
return $handler->handle($request);
}
foreach($this->filters['headers'] as $key => $value) {
if ( ! in_array($value, $request->getHeader($key)) ) {
return $handler->handle($request);
}
}
foreach($this->filters['functions'] as $func) {
if ( $func->bindTo($this, $this)() ) {
return $handler->handle($request);
}
}
$this->throwError(json_decode($request->getBody(), true));
}
}
<?php
namespace TheBugs;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response,
Zend\Diactoros\ServerRequest,
Zend\Diactoros\Stream,
Zend\Diactoros\Uri;
class JavascriptMiddleware implements MiddlewareInterface
{
const FILTERS = [
"code" => null,
"headers" => [
"User-Agent" => "TheBugs/1.0",
],
# Callable functions
"functions" => []
];
/**
* Filter list, allows custom invokable too
* @var array
*/
protected $filters = [];
public function __construct($options = [])
{
foreach(static::FILTERS as $key => $default) {
$this->filters[$key] = ( $options[$key] ?? false ) ? $options[$key] : $default;
}
}
public function throwError($errorDetails)
{
throw new Exception\JavascriptException($errorDetails['message'], 0, null, $errorDetails['location'], $errorDetails['line'], $errorDetails['url'], (array) $errorDetails['stack']);
}
/**
* Process a server request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
# if ( ( $this->filters['code'] ?? false ) && ($this->filters['code'] !== $request->getCode())) {
# return $handler->handle($request);
# }
foreach($this->filters['headers'] as $key => $value) {
if ( ! in_array($value, $request->getHeader($key)) ) {
return $handler->handle($request);
}
}
foreach($this->filters['functions'] as $func) {
if ( $func->call($this) ) {
return $handler->handle($request);
}
}
$this->throwError(json_decode($request->getBody(), true));
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
use Whoops\Run as Whoops;
use Whoops\Handler\PrettyPageHandler as Handler;
$whoops = new Whoops();
$whoops->allowQuit(false);
$whoops->writeToOutput(false);
$whoops->pushHandler(new Handler());
IF CLI {
$handler->handleUnconditionally(true);
}
$body = $whoops->handleException($this->exception);