Skip to content

Commit

Permalink
Use DI instead of global getter, remove redundant check
Browse files Browse the repository at this point in the history
  • Loading branch information
flack committed Jun 25, 2024
1 parent 91e741b commit 05b6453
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
36 changes: 15 additions & 21 deletions lib/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class midcom_exception_handler implements EventSubscriberInterface
{
private Throwable $error;

public function __construct(private array $error_actions, private midcom_helper_style $style)
{}

public static function getSubscribedEvents()
{
return [
Expand Down Expand Up @@ -68,7 +71,6 @@ public function handle(ExceptionEvent $event)

private function process(int $httpcode, string $message) : Response
{

if ($httpcode == Response::HTTP_FORBIDDEN) {
return new midcom_response_accessdenied($message);
}
Expand All @@ -80,17 +82,15 @@ private function process(int $httpcode, string $message) : Response
return new midcom_response_login;
}

$style = midcom::get()->style;

$style->data['error_title'] = Response::$statusTexts[$httpcode];
$style->data['error_message'] = $message;
$style->data['error_code'] = $httpcode;
$style->data['error_exception'] = $this->error;
$style->data['error_handler'] = $this;
$this->style->data['error_title'] = Response::$statusTexts[$httpcode];
$this->style->data['error_message'] = $message;
$this->style->data['error_code'] = $httpcode;
$this->style->data['error_exception'] = $this->error;
$this->style->data['error_handler'] = $this;

ob_start();
if (!$style->show_midcom('midcom_error_' . $httpcode)) {
$style->show_midcom('midcom_error');
if (!$this->style->show_midcom('midcom_error_' . $httpcode)) {
$this->style->show_midcom('midcom_error');
}
$content = ob_get_clean();

Expand All @@ -106,8 +106,7 @@ private function process(int $httpcode, string $message) : Response
*/
private function process_actions(ServerBag $server, int $httpcode, string $message)
{
$error_actions = midcom::get()->config->get_array('error_actions');
if (!isset($error_actions[$httpcode]['action'])) {
if (!isset($this->error_actions[$httpcode]['action'])) {
// No action specified for this error code, skip
return;
}
Expand All @@ -120,12 +119,12 @@ private function process_actions(ServerBag $server, int $httpcode, string $messa
}

// Send as email handler
if ($error_actions[$httpcode]['action'] == 'email') {
$this->_send_email($msg, $error_actions[$httpcode], $server->getString('SERVER_NAME'));
if ($this->error_actions[$httpcode]['action'] == 'email') {
$this->_send_email($msg, $this->error_actions[$httpcode], $server->getString('SERVER_NAME'));
}
// Append to log file handler
elseif ($error_actions[$httpcode]['action'] == 'log') {
$this->_log($msg, $error_actions[$httpcode]);
elseif ($this->error_actions[$httpcode]['action'] == 'log') {
$this->_log($msg, $this->error_actions[$httpcode]);
}
}

Expand Down Expand Up @@ -156,11 +155,6 @@ private function _send_email(string $msg, array $config, string $servername)
return;
}

if (!midcom::get()->componentloader->is_installed('org.openpsa.mail')) {
debug_add("Email sending library org.openpsa.mail, used for error notifications is not installed", MIDCOM_LOG_WARN);
return;
}

$mail = new org_openpsa_mail();
$mail->to = $config['email'];
$mail->from = "\"MidCOM error notifier\" <webmaster@{$servername}>";
Expand Down
1 change: 1 addition & 0 deletions src/midcom/bundle/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:

exception_handler:
class: midcom_exception_handler
arguments: ['%midcom.error_actions%', '@style']
tags:
- {name: 'kernel.event_subscriber'}

Expand Down

0 comments on commit 05b6453

Please sign in to comment.