Skip to content

Commit

Permalink
[FEATURE] Add event for Readable class
Browse files Browse the repository at this point in the history
to extend referrer with readable labels for defined domains
  • Loading branch information
einpraegsam committed Jul 15, 2024
1 parent 3d9148c commit aee4445
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
12 changes: 10 additions & 2 deletions Classes/Domain/Service/Referrer/Readable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions Classes/Events/ReadableReferrersEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);
namespace In2code\Lux\Events;

final class ReadableReferrersEvent
{
protected string $referrer;
protected array $sources;

public function __construct(string $referrer, array $sources)
{
$this->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;
}
}
6 changes: 3 additions & 3 deletions Tests/Unit/Domain/Service/Referrer/ReadableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
}

Expand All @@ -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);
}
}
13 changes: 13 additions & 0 deletions Tests/Unit/Fixtures/Domain/Service/Referrer/ReadableFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace In2code\Lux\Tests\Unit\Fixtures\Domain\Service\Referrer;

use In2code\Lux\Domain\Service\Referrer\Readable;

class ReadableFixture extends Readable
{
public function __construct(string $referrer = '')
{
$this->referrer = $referrer;
}
}

0 comments on commit aee4445

Please sign in to comment.