From aee4445f25af5e33a1a5bbebea3d74053e8ce1b4 Mon Sep 17 00:00:00 2001 From: Alexander Kellner Date: Mon, 15 Jul 2024 16:00:30 +0200 Subject: [PATCH] [FEATURE] Add event for Readable class to extend referrer with readable labels for defined domains --- Classes/Domain/Service/Referrer/Readable.php | 12 +++++- Classes/Events/ReadableReferrersEvent.php | 38 +++++++++++++++++++ .../Domain/Service/Referrer/ReadableTest.php | 6 +-- .../Service/Referrer/ReadableFixture.php | 13 +++++++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 Classes/Events/ReadableReferrersEvent.php create mode 100644 Tests/Unit/Fixtures/Domain/Service/Referrer/ReadableFixture.php diff --git a/Classes/Domain/Service/Referrer/Readable.php b/Classes/Domain/Service/Referrer/Readable.php index 8a46dbb0..ecd8fd07 100644 --- a/Classes/Domain/Service/Referrer/Readable.php +++ b/Classes/Domain/Service/Referrer/Readable.php @@ -3,6 +3,10 @@ declare(strict_types=1); namespace In2code\Lux\Domain\Service\Referrer; +use In2code\Lux\Events\ReadableReferrersEvent; +use Psr\EventDispatcher\EventDispatcherInterface; +use TYPO3\CMS\Core\Utility\GeneralUtility; + /** * Class Readable * converts referrers like m.twitter.com to a readable referrer like "Twitter" @@ -412,12 +416,16 @@ class Readable ], ], ]; - protected string $referrer = ''; + private EventDispatcherInterface $eventDispatcher; public function __construct(string $referrer = '') { - $this->referrer = $referrer; + $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); + /** @var ReadableReferrersEvent $event */ + $event = $this->eventDispatcher->dispatch(new ReadableReferrersEvent($referrer, $this->sources)); + $this->referrer = $event->getReferrer(); + $this->sources = $event->getSources(); } public function getReadableReferrer(): string diff --git a/Classes/Events/ReadableReferrersEvent.php b/Classes/Events/ReadableReferrersEvent.php new file mode 100644 index 00000000..2462d5d1 --- /dev/null +++ b/Classes/Events/ReadableReferrersEvent.php @@ -0,0 +1,38 @@ +referrer = $referrer; + $this->sources = $sources; + } + + public function getReferrer(): string + { + return $this->referrer; + } + + public function setReferrer(string $referrer): self + { + $this->referrer = $referrer; + return $this; + } + + public function getSources(): array + { + return $this->sources; + } + + public function setSources(array $sources): self + { + $this->sources = $sources; + return $this; + } +} diff --git a/Tests/Unit/Domain/Service/Referrer/ReadableTest.php b/Tests/Unit/Domain/Service/Referrer/ReadableTest.php index 94be24d9..6d2367a1 100644 --- a/Tests/Unit/Domain/Service/Referrer/ReadableTest.php +++ b/Tests/Unit/Domain/Service/Referrer/ReadableTest.php @@ -2,7 +2,7 @@ namespace In2code\Lux\Tests\Unit\Domain\Service; -use In2code\Lux\Domain\Service\Referrer\Readable; +use In2code\Lux\Tests\Unit\Fixtures\Domain\Service\Referrer\ReadableFixture; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; /** @@ -46,7 +46,7 @@ public static function getReadableReferrerDataProvider(): array */ public function testGetReadableReferrer(string $referrer, string $expectedResult): void { - $readable = new Readable($referrer); + $readable = new ReadableFixture($referrer); self::assertSame($expectedResult, $readable->getReadableReferrer()); } @@ -56,7 +56,7 @@ public function testGetReadableReferrer(string $referrer, string $expectedResult */ public function testGetOriginalReferrer(): void { - $readable = new Readable(''); + $readable = new ReadableFixture(''); self::assertGreaterThan($readable->getOriginalReferrer(), 10); } } diff --git a/Tests/Unit/Fixtures/Domain/Service/Referrer/ReadableFixture.php b/Tests/Unit/Fixtures/Domain/Service/Referrer/ReadableFixture.php new file mode 100644 index 00000000..78405597 --- /dev/null +++ b/Tests/Unit/Fixtures/Domain/Service/Referrer/ReadableFixture.php @@ -0,0 +1,13 @@ +referrer = $referrer; + } +}