Skip to content

Commit

Permalink
Log unsuccessful deliveries to the system log (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg authored May 24, 2024
1 parent 6f3cfbc commit 2ba97e7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Terminal42\NotificationCenterBundle\EventListener\DbafsMetadataListener;
use Terminal42\NotificationCenterBundle\EventListener\DisableDeliveryListener;
use Terminal42\NotificationCenterBundle\EventListener\DoctrineSchemaListener;
use Terminal42\NotificationCenterBundle\EventListener\LogUnsuccessfulDeliveries;
use Terminal42\NotificationCenterBundle\EventListener\NotificationCenterProListener;
use Terminal42\NotificationCenterBundle\EventListener\NotificationTypeForModuleListener;
use Terminal42\NotificationCenterBundle\EventListener\ProcessFormDataListener;
Expand Down Expand Up @@ -133,4 +134,9 @@
;

$services->set(DbafsMetadataListener::class);
$services->set(LogUnsuccessfulDeliveries::class)
->args([
service('monolog.logger.contao.error')->nullOnInvalid(),
])
;
};
1 change: 1 addition & 0 deletions depcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
'Contao\ModulePassword', // This class exists in Contao 4.13 but not in 5.3
])
->ignoreErrorsOnPackage('contao/newsletter-bundle', [ErrorType::DEV_DEPENDENCY_IN_PROD]) // This is an optional integration
->ignoreErrorsOnPackage('psr/log', [ErrorType::SHADOW_DEPENDENCY]) // Logging is optional
->ignoreErrorsOnPackage('terminal42/dcawizard', [ErrorType::UNUSED_DEPENDENCY]) // This is a widget used in the back end but not inside code
;
34 changes: 34 additions & 0 deletions src/EventListener/LogUnsuccessfulDeliveries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Terminal42\NotificationCenterBundle\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Terminal42\NotificationCenterBundle\Event\ReceiptEvent;

#[AsEventListener]
class LogUnsuccessfulDeliveries
{
public function __construct(private readonly LoggerInterface|null $contaoErrorLogger)
{
}

public function __invoke(ReceiptEvent $event): void
{
if (null === $this->contaoErrorLogger) {
return;
}

$receipt = $event->receipt;

if ($receipt->wasDelivered()) {
return;
}

$exception = $receipt->getException();

$this->contaoErrorLogger->error($exception->getMessage(), ['exception' => $exception]);
}
}

0 comments on commit 2ba97e7

Please sign in to comment.