From a7dcdf4ee70fcce607ab93d9f0f916b2af3bf0fa Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 21 Aug 2019 15:54:17 -0400 Subject: [PATCH] - Work done on the Bug Email component --- src/Email/EmailConfiguration.php | 26 ++++++ src/Email/MailerInterface.php | 12 +++ src/Email/SwiftMailer.php | 45 ++++++++++ src/EmailErrorMiddleware.php | 53 ++++++++++++ src/JavascriptMiddleware.php | 140 +++++++++++++++---------------- src/WhoopsHandler.php | 15 ---- templates/js_debug.js | 46 ++++++++++ templates/xhr_debug.php | 88 ++++++++----------- 8 files changed, 286 insertions(+), 139 deletions(-) create mode 100644 src/Email/EmailConfiguration.php create mode 100644 src/Email/MailerInterface.php create mode 100644 src/Email/SwiftMailer.php create mode 100644 src/EmailErrorMiddleware.php delete mode 100644 src/WhoopsHandler.php create mode 100644 templates/js_debug.js diff --git a/src/Email/EmailConfiguration.php b/src/Email/EmailConfiguration.php new file mode 100644 index 0000000..a13f9f7 --- /dev/null +++ b/src/Email/EmailConfiguration.php @@ -0,0 +1,26 @@ +type = $type; + } + +} \ No newline at end of file diff --git a/src/Email/MailerInterface.php b/src/Email/MailerInterface.php new file mode 100644 index 0000000..eddf706 --- /dev/null +++ b/src/Email/MailerInterface.php @@ -0,0 +1,12 @@ +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; + } + +} \ No newline at end of file diff --git a/src/EmailErrorMiddleware.php b/src/EmailErrorMiddleware.php new file mode 100644 index 0000000..67364bb --- /dev/null +++ b/src/EmailErrorMiddleware.php @@ -0,0 +1,53 @@ +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; + } +} \ No newline at end of file diff --git a/src/JavascriptMiddleware.php b/src/JavascriptMiddleware.php index c40cd51..8bab690 100644 --- a/src/JavascriptMiddleware.php +++ b/src/JavascriptMiddleware.php @@ -1,70 +1,70 @@ - 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)); - } - -} + 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)); + } + +} diff --git a/src/WhoopsHandler.php b/src/WhoopsHandler.php deleted file mode 100644 index ab94481..0000000 --- a/src/WhoopsHandler.php +++ /dev/null @@ -1,15 +0,0 @@ -allowQuit(false); -$whoops->writeToOutput(false); -$whoops->pushHandler(new Handler()); - -IF CLI { - $handler->handleUnconditionally(true); -} - -$body = $whoops->handleException($this->exception); diff --git a/templates/js_debug.js b/templates/js_debug.js new file mode 100644 index 0000000..a95b0c7 --- /dev/null +++ b/templates/js_debug.js @@ -0,0 +1,46 @@ + +class ErrorHandler +{ + constructor(options) { + if ( options ) { + if ( "url" in options ) { + this.url = options['url']; + } + } + + this.catchError(); + } + + catchError() { + window.onerror = function(message, url, line, column, error) { + fetch(this.url ? this.url : window.location.href, { + method: "post", + headers: { + 'Accept': "application/json", + 'Content-Type': "application/json", + 'User-Agent': "TheBugs/1.0" + }, + body: JSON.stringify({ + message: message, + url: url, + line: line, + column: column, + stack: error.stack, + location: window.location.toString() + }) + }).then( response => response ).then(data => { + console.info("Error reported", data); + }); + + return false; + }.bind(this); + } + + get url() { + return this._url; + } + + set url(set) { + return this._url = set; + } +} diff --git a/templates/xhr_debug.php b/templates/xhr_debug.php index ffdcaec..0d1d85c 100644 --- a/templates/xhr_debug.php +++ b/templates/xhr_debug.php @@ -1,54 +1,34 @@ - +