From 11c831945c470ed63c936080ce0c14174061d68e Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Tue, 4 Feb 2020 21:32:18 +0100 Subject: [PATCH] Add logging --- src/Util/EMailer/MailBuilder.php | 7 ++++++- src/Util/EMailer/SystemMailBuilder.php | 7 ++++--- src/Util/Emailer.php | 2 +- tests/src/Util/Emailer/MailBuilderTest.php | 13 +++++++------ tests/src/Util/Emailer/SystemMailBuilderTest.php | 5 ++--- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Util/EMailer/MailBuilder.php b/src/Util/EMailer/MailBuilder.php index 074e29d3f2..c640c96b94 100644 --- a/src/Util/EMailer/MailBuilder.php +++ b/src/Util/EMailer/MailBuilder.php @@ -11,6 +11,7 @@ use Friendica\Model\User; use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Object\Email; use Friendica\Object\EMail\IEmail; +use Psr\Log\LoggerInterface; /** * A base class for building new emails @@ -26,6 +27,8 @@ abstract class MailBuilder protected $config; /** @var BaseURL */ protected $baseUrl; + /** @var LoggerInterface */ + protected $logger; /** @var string */ protected $headers; @@ -42,11 +45,12 @@ abstract class MailBuilder /** @var int */ protected $recipientUid = null; - public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config) + public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger) { $this->l10n = $l10n; $this->baseUrl = $baseUrl; $this->config = $config; + $this->logger = $logger; $hostname = $baseUrl->getHostname(); if (strpos($hostname, ':')) { @@ -97,6 +101,7 @@ abstract class MailBuilder try { $this->l10n = $user['language'] ? $this->l10n->withLang($user['language']) : $this->l10n; } catch (Exception $e) { + $this->logger->warning('cannot use language.', ['user' => $user, 'exception' => $e]); } return $this; diff --git a/src/Util/EMailer/SystemMailBuilder.php b/src/Util/EMailer/SystemMailBuilder.php index 07cbca4e27..015597fba5 100644 --- a/src/Util/EMailer/SystemMailBuilder.php +++ b/src/Util/EMailer/SystemMailBuilder.php @@ -3,13 +3,13 @@ namespace Friendica\Util\EMailer; use Exception; -use Friendica\App; use Friendica\App\BaseURL; use Friendica\Content\Text\BBCode; use Friendica\Core\Config\IConfig; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Network\HTTPException\InternalServerErrorException; +use Psr\Log\LoggerInterface; /** * Builder for system-wide emails without any dependency to concrete entities (like items, activities, ..) @@ -26,9 +26,10 @@ class SystemMailBuilder extends MailBuilder /** @var string */ protected $siteAdmin; - public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, string $siteEmailAddress, string $siteName) + public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger, + string$siteEmailAddress, string $siteName) { - parent::__construct($l10n, $baseUrl, $config); + parent::__construct($l10n, $baseUrl, $config, $logger); if ($this->config->get('config', 'admin_name')) { $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName); diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index d5d5f2cb41..1e1c6856cf 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -85,7 +85,7 @@ class Emailer */ public function newSystemMail() { - return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, + return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger, $this->getSiteEmailAddress(), $this->getSiteEmailName()); } diff --git a/tests/src/Util/Emailer/MailBuilderTest.php b/tests/src/Util/Emailer/MailBuilderTest.php index 37b8f2784c..1e475c9f70 100644 --- a/tests/src/Util/Emailer/MailBuilderTest.php +++ b/tests/src/Util/Emailer/MailBuilderTest.php @@ -10,6 +10,7 @@ use Friendica\Test\MockedTest; use Friendica\Test\Util\SampleMailBuilder; use Friendica\Test\Util\VFSTrait; use Friendica\Util\EMailer\MailBuilder; +use Psr\Log\NullLogger; /** * This class tests the "MailBuilder" (@see MailBuilder ) @@ -62,7 +63,7 @@ class MailBuilderTest extends MockedTest */ public function testBuilderInstance() { - $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config); + $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger()); $this->assertInstanceOf(MailBuilder::class, $builder); } @@ -82,7 +83,7 @@ class MailBuilderTest extends MockedTest */ public function testBuilderWithRawEmail() { - $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config); + $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger()); $testEmail = $builder ->withMessage('Subject', 'Html', 'text') @@ -112,7 +113,7 @@ class MailBuilderTest extends MockedTest */ public function testBuilderWithEmptyMail() { - $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config); + $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger()); $builder->build(true); } @@ -125,7 +126,7 @@ class MailBuilderTest extends MockedTest */ public function testBuilderWithEmptySender() { - $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config); + $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger()); $builder ->withRecipient('test@friendica.local') @@ -137,7 +138,7 @@ class MailBuilderTest extends MockedTest */ public function testBuilderWithoutMessage() { - $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config); + $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger()); $testEmail = $builder ->withRecipient('recipient@friendica.local') @@ -158,7 +159,7 @@ class MailBuilderTest extends MockedTest */ public function testBuilderWithJustPreamble() { - $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config); + $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger()); $testEmail = $builder ->withRecipient('recipient@friendica.local') diff --git a/tests/src/Util/Emailer/SystemMailBuilderTest.php b/tests/src/Util/Emailer/SystemMailBuilderTest.php index 5c531bf3cd..e855602766 100644 --- a/tests/src/Util/Emailer/SystemMailBuilderTest.php +++ b/tests/src/Util/Emailer/SystemMailBuilderTest.php @@ -5,12 +5,11 @@ namespace Friendica\Test\src\Util\Emailer; use Friendica\App\BaseURL; use Friendica\Core\Config\IConfig; use Friendica\Core\L10n; -use Friendica\Object\EMail\IEmail; use Friendica\Test\MockedTest; -use Friendica\Test\Util\SampleMailBuilder; use Friendica\Test\Util\VFSTrait; use Friendica\Util\EMailer\MailBuilder; use Friendica\Util\EMailer\SystemMailBuilder; +use Psr\Log\NullLogger; class SystemMailBuilderTest extends MockedTest { @@ -50,7 +49,7 @@ class SystemMailBuilderTest extends MockedTest */ public function testBuilderInstance() { - $builder = new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, 'moreply@friendica.local', 'FriendicaSite'); + $builder = new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger(), 'moreply@friendica.local', 'FriendicaSite'); $this->assertInstanceOf(MailBuilder::class, $builder); $this->assertInstanceOf(SystemMailBuilder::class, $builder); -- 2.39.5