diff --git a/src/Service/Notification.php b/src/Service/Notification.php index 42f3bd33..5a7ebec0 100644 --- a/src/Service/Notification.php +++ b/src/Service/Notification.php @@ -5,16 +5,18 @@ use Exception; use Psr\Log\LoggerInterface; use SilverStripe\Control\Email\Email; +use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Extensible; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Security\Member; /** - * Ecapsulates setting up an Email in order to allow for Dependency Injection and to avoid introducing a hard + * Encapsulates setting up an Email in order to allow for dependency injection and to avoid introducing a hard * coupling to the SilverStripe core Email class in code that consumes this class. */ class Notification { + use Configurable; use Injectable; use Extensible; @@ -26,6 +28,14 @@ class Notification 'Logger' => '%$' . LoggerInterface::class . '.mfa', ]; + /** + * Whether sending emails is enabled for MFA changes + * + * @config + * @var bool + */ + private static $enabled = true; + /** * @var LoggerInterface */ @@ -52,6 +62,10 @@ public function setLogger(LoggerInterface $logger): self */ public function send(Member $member, string $template, array $data = []): bool { + if (!$this->config()->get('enabled')) { + return false; + } + // Catch exceptions with setting the "to" address and sending the email. try { $email = Email::create() diff --git a/tests/php/Service/NotificationTest.php b/tests/php/Service/NotificationTest.php new file mode 100644 index 00000000..07fa7d92 --- /dev/null +++ b/tests/php/Service/NotificationTest.php @@ -0,0 +1,17 @@ +set('enabled', false); + $result = Notification::create()->send($this->createMock(Member::class), 'foo'); + $this->assertFalse($result); + } +}