Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visited short URL info to every visit #2275

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions module/Core/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Common\Doctrine\EntityRepositoryFactory;
use Shlinkio\Shlink\Core\Config\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdater;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
Expand Down Expand Up @@ -90,6 +89,7 @@
],
Visit\Listener\ShortUrlVisitsCountTracker::class => InvokableFactory::class,
Visit\Listener\OrphanVisitsCountTracker::class => InvokableFactory::class,
Visit\Transformer\VisitDataTransformer::class => ConfigAbstractFactory::class,

Util\DoctrineBatchHelper::class => ConfigAbstractFactory::class,
Util\RedirectResponseHelper::class => ConfigAbstractFactory::class,
Expand Down Expand Up @@ -123,7 +123,7 @@
Matomo\MatomoTrackerBuilder::class => [Matomo\MatomoOptions::class],
Matomo\MatomoVisitSender::class => [
Matomo\MatomoTrackerBuilder::class,
ShortUrlStringifier::class,
ShortUrl\Helper\ShortUrlStringifier::class,
Visit\Repository\VisitIterationRepository::class,
],

Expand Down Expand Up @@ -163,6 +163,7 @@
Visit\Geolocation\VisitLocator::class => ['em', Visit\Repository\VisitIterationRepository::class],
Visit\Geolocation\VisitToLocationHelper::class => [IpLocationResolverInterface::class],
Visit\VisitsStatsHelper::class => ['em'],
Visit\Transformer\VisitDataTransformer::class => [ShortUrl\Helper\ShortUrlStringifier::class],
Tag\TagService::class => ['em', Tag\Repository\TagRepository::class],
ShortUrl\DeleteShortUrlService::class => [
'em',
Expand Down
24 changes: 22 additions & 2 deletions module/Core/src/Visit/Entity/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ public function getType(): VisitType
}

public function jsonSerialize(): array
{
return $this->toArray();
}

/**
* @phpstan-type VisitedShortUrl array{shortCode: string, domain: string|null, shortUrl: string}
* @param (callable(ShortUrl $shortUrl): VisitedShortUrl)|null $visitedShortUrlToArray
*/
public function toArray(callable|null $visitedShortUrlToArray = null): array
{
$base = [
'referer' => $this->referer,
Expand All @@ -157,13 +166,24 @@ public function jsonSerialize(): array
'potentialBot' => $this->potentialBot,
'visitedUrl' => $this->visitedUrl,
];
if (! $this->isOrphan()) {

// Orphan visit
if ($this->shortUrl === null) {
return [
...$base,
'type' => $this->type->value,
];

}

// Should not include visited short URL
if ($visitedShortUrlToArray === null) {
return $base;
}

return [
...$base,
'type' => $this->type->value,
'visitedShortUrl' => $visitedShortUrlToArray($this->shortUrl),
];
}
}
25 changes: 25 additions & 0 deletions module/Core/src/Visit/Transformer/VisitDataTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Core\Visit\Transformer;

use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;

readonly class VisitDataTransformer implements VisitDataTransformerInterface
{
public function __construct(private ShortUrlStringifierInterface $stringifier)
{
}

public function transform(Visit $visit): array
{
return $visit->toArray(fn (ShortUrl $shortUrl) => [
'shortCode' => $shortUrl->getShortCode(),
'domain' => $shortUrl->getDomain()?->authority,
'shortUrl' => $this->stringifier->stringify($shortUrl),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Core\Visit\Transformer;

use Shlinkio\Shlink\Core\Visit\Entity\Visit;

interface VisitDataTransformerInterface
{
public function transform(Visit $visit): array;
}
21 changes: 17 additions & 4 deletions module/Rest/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,29 @@
ShortUrl\ShortUrlResolver::class,
ShortUrlDataTransformer::class,
],
Action\Visit\ShortUrlVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\Visit\TagVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\Visit\ShortUrlVisitsAction::class => [
Visit\VisitsStatsHelper::class,
Visit\Transformer\VisitDataTransformer::class,
],
Action\Visit\TagVisitsAction::class => [
Visit\VisitsStatsHelper::class,
Visit\Transformer\VisitDataTransformer::class,
],
Action\Visit\DomainVisitsAction::class => [
Visit\VisitsStatsHelper::class,
Config\Options\UrlShortenerOptions::class,
Visit\Transformer\VisitDataTransformer::class,
],
Action\Visit\GlobalVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\Visit\OrphanVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\Visit\OrphanVisitsAction::class => [
Visit\VisitsStatsHelper::class,
Visit\Transformer\VisitDataTransformer::class,
],
Action\Visit\DeleteOrphanVisitsAction::class => [Visit\VisitsDeleter::class],
Action\Visit\NonOrphanVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\Visit\NonOrphanVisitsAction::class => [
Visit\VisitsStatsHelper::class,
Visit\Transformer\VisitDataTransformer::class,
],
Action\ShortUrl\ListShortUrlsAction::class => [
ShortUrl\ShortUrlListService::class,
ShortUrlDataTransformer::class,
Expand Down
11 changes: 8 additions & 3 deletions module/Rest/src/Action/Visit/AbstractListVisitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtils;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\Transformer\VisitDataTransformerInterface;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
Expand All @@ -20,8 +21,10 @@ abstract class AbstractListVisitsAction extends AbstractRestAction
{
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];

public function __construct(protected readonly VisitsStatsHelperInterface $visitsHelper)
{
public function __construct(
protected readonly VisitsStatsHelperInterface $visitsHelper,
private readonly VisitDataTransformerInterface $transformer,
) {
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand All @@ -30,7 +33,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
$visits = $this->getVisitsPaginator($request, $params, $apiKey);

return new JsonResponse(['visits' => PagerfantaUtils::serializePaginator($visits)]);
return new JsonResponse([
'visits' => PagerfantaUtils::serializePaginator($visits, $this->transformer->transform(...)),
]);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion module/Rest/src/Action/Visit/DomainVisitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\Transformer\VisitDataTransformerInterface;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;

Expand All @@ -19,8 +20,9 @@ class DomainVisitsAction extends AbstractListVisitsAction
public function __construct(
VisitsStatsHelperInterface $visitsHelper,
private readonly UrlShortenerOptions $urlShortenerOptions,
VisitDataTransformerInterface $transformer,
) {
parent::__construct($visitsHelper);
parent::__construct($visitsHelper, $transformer);
}

protected function getVisitsPaginator(Request $request, VisitsParams $params, ApiKey $apiKey): Pagerfanta
Expand Down
Loading