mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-25 10:20:18 -06:00
Merge pull request #2068 from acelaya-forks/feature/modernize-entities
Feature/modernize entities
This commit is contained in:
commit
457a7a14e5
17
CHANGELOG.md
17
CHANGELOG.md
@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
|
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
### Added
|
||||||
|
* *Nothing*
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* [#2034](https://github.com/shlinkio/shlink/issues/2034) Modernize entities, using constructor property promotion and readonly wherever possible.
|
||||||
|
|
||||||
|
### Deprecated
|
||||||
|
* *Nothing*
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
* *Nothing*
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
* *Nothing*
|
||||||
|
|
||||||
|
|
||||||
## [4.0.3] - 2024-03-15
|
## [4.0.3] - 2024-03-15
|
||||||
### Added
|
### Added
|
||||||
* *Nothing*
|
* *Nothing*
|
||||||
|
@ -50,11 +50,11 @@ class ListKeysCommand extends Command
|
|||||||
$enabledOnly = $input->getOption('enabled-only');
|
$enabledOnly = $input->getOption('enabled-only');
|
||||||
|
|
||||||
$rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) {
|
$rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) {
|
||||||
$expiration = $apiKey->getExpirationDate();
|
$expiration = $apiKey->expirationDate;
|
||||||
$messagePattern = $this->determineMessagePattern($apiKey);
|
$messagePattern = $this->determineMessagePattern($apiKey);
|
||||||
|
|
||||||
// Set columns for this row
|
// Set columns for this row
|
||||||
$rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name() ?? '-')];
|
$rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name ?? '-')];
|
||||||
if (! $enabledOnly) {
|
if (! $enabledOnly) {
|
||||||
$rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey));
|
$rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey));
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ class GetDomainVisitsCommand extends AbstractVisitsListCommand
|
|||||||
*/
|
*/
|
||||||
protected function mapExtraFields(Visit $visit): array
|
protected function mapExtraFields(Visit $visit): array
|
||||||
{
|
{
|
||||||
$shortUrl = $visit->getShortUrl();
|
$shortUrl = $visit->shortUrl;
|
||||||
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,11 +229,11 @@ class ListShortUrlsCommand extends Command
|
|||||||
}
|
}
|
||||||
if ($input->getOption('show-api-key')) {
|
if ($input->getOption('show-api-key')) {
|
||||||
$columnsMap['API Key'] = static fn (array $_, ShortUrl $shortUrl): string =>
|
$columnsMap['API Key'] = static fn (array $_, ShortUrl $shortUrl): string =>
|
||||||
$shortUrl->authorApiKey()?->__toString() ?? '';
|
$shortUrl->authorApiKey?->__toString() ?? '';
|
||||||
}
|
}
|
||||||
if ($input->getOption('show-api-key-name')) {
|
if ($input->getOption('show-api-key-name')) {
|
||||||
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string =>
|
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string =>
|
||||||
$shortUrl->authorApiKey()?->name();
|
$shortUrl->authorApiKey?->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $columnsMap;
|
return $columnsMap;
|
||||||
|
@ -44,7 +44,7 @@ class GetTagVisitsCommand extends AbstractVisitsListCommand
|
|||||||
*/
|
*/
|
||||||
protected function mapExtraFields(Visit $visit): array
|
protected function mapExtraFields(Visit $visit): array
|
||||||
{
|
{
|
||||||
$shortUrl = $visit->getShortUrl();
|
$shortUrl = $visit->shortUrl;
|
||||||
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,8 +55,8 @@ abstract class AbstractVisitsListCommand extends Command
|
|||||||
|
|
||||||
$rowData = [
|
$rowData = [
|
||||||
...$visit->jsonSerialize(),
|
...$visit->jsonSerialize(),
|
||||||
'country' => $visit->getVisitLocation()?->getCountryName() ?? 'Unknown',
|
'country' => $visit->getVisitLocation()?->countryName ?? 'Unknown',
|
||||||
'city' => $visit->getVisitLocation()?->getCityName() ?? 'Unknown',
|
'city' => $visit->getVisitLocation()?->cityName ?? 'Unknown',
|
||||||
...$extraFields,
|
...$extraFields,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class GetNonOrphanVisitsCommand extends AbstractVisitsListCommand
|
|||||||
*/
|
*/
|
||||||
protected function mapExtraFields(Visit $visit): array
|
protected function mapExtraFields(Visit $visit): array
|
||||||
{
|
{
|
||||||
$shortUrl = $visit->getShortUrl();
|
$shortUrl = $visit->shortUrl;
|
||||||
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
return $shortUrl === null ? [] : ['shortUrl' => $this->shortUrlStringifier->stringify($shortUrl)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,6 @@ class GetOrphanVisitsCommand extends AbstractVisitsListCommand
|
|||||||
*/
|
*/
|
||||||
protected function mapExtraFields(Visit $visit): array
|
protected function mapExtraFields(Visit $visit): array
|
||||||
{
|
{
|
||||||
return ['type' => $visit->type()->value];
|
return ['type' => $visit->type->value];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
|
|||||||
*/
|
*/
|
||||||
public function geolocateVisit(Visit $visit): Location
|
public function geolocateVisit(Visit $visit): Location
|
||||||
{
|
{
|
||||||
$ipAddr = $visit->getRemoteAddr() ?? '?';
|
$ipAddr = $visit->remoteAddr ?? '?';
|
||||||
$this->io->write(sprintf('Processing IP <fg=blue>%s</>', $ipAddr));
|
$this->io->write(sprintf('Processing IP <fg=blue>%s</>', $ipAddr));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -154,9 +154,9 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
|
|||||||
|
|
||||||
public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void
|
public function onVisitLocated(VisitLocation $visitLocation, Visit $visit): void
|
||||||
{
|
{
|
||||||
if (! $visitLocation->isEmpty()) {
|
if (! $visitLocation->isEmpty) {
|
||||||
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->getCountryName()));
|
$this->io->writeln(sprintf(' [<info>Address located in "%s"</info>]', $visitLocation->countryName));
|
||||||
} elseif ($visit->hasRemoteAddr() && $visit->getRemoteAddr() !== IpAddress::LOCALHOST) {
|
} elseif ($visit->hasRemoteAddr() && $visit->remoteAddr !== IpAddress::LOCALHOST) {
|
||||||
$this->io->writeln(' <comment>[Could not locate address]</comment>');
|
$this->io->writeln(' <comment>[Could not locate address]</comment>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,12 @@ use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
|||||||
|
|
||||||
class Domain extends AbstractEntity implements JsonSerializable, NotFoundRedirectConfigInterface
|
class Domain extends AbstractEntity implements JsonSerializable, NotFoundRedirectConfigInterface
|
||||||
{
|
{
|
||||||
private ?string $baseUrlRedirect = null;
|
private function __construct(
|
||||||
private ?string $regular404Redirect = null;
|
public readonly string $authority,
|
||||||
private ?string $invalidShortUrlRedirect = null;
|
private ?string $baseUrlRedirect = null,
|
||||||
|
private ?string $regular404Redirect = null,
|
||||||
private function __construct(public readonly string $authority)
|
private ?string $invalidShortUrlRedirect = null,
|
||||||
{
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function withAuthority(string $authority): self
|
public static function withAuthority(string $authority): self
|
||||||
|
@ -55,7 +55,7 @@ class LocateVisit
|
|||||||
}
|
}
|
||||||
|
|
||||||
$isLocatable = $originalIpAddress !== null || $visit->isLocatable();
|
$isLocatable = $originalIpAddress !== null || $visit->isLocatable();
|
||||||
$addr = $originalIpAddress ?? $visit->getRemoteAddr() ?? '';
|
$addr = $originalIpAddress ?? $visit->remoteAddr ?? '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$location = $isLocatable ? $this->ipLocationResolver->resolveIpLocation($addr) : Location::emptyInstance();
|
$location = $isLocatable ? $this->ipLocationResolver->resolveIpLocation($addr) : Location::emptyInstance();
|
||||||
|
@ -46,21 +46,21 @@ class SendVisitToMatomo
|
|||||||
|
|
||||||
$tracker
|
$tracker
|
||||||
->setUrl($this->resolveUrlToTrack($visit))
|
->setUrl($this->resolveUrlToTrack($visit))
|
||||||
->setCustomTrackingParameter('type', $visit->type()->value)
|
->setCustomTrackingParameter('type', $visit->type->value)
|
||||||
->setUserAgent($visit->userAgent())
|
->setUserAgent($visit->userAgent)
|
||||||
->setUrlReferrer($visit->referer());
|
->setUrlReferrer($visit->referer);
|
||||||
|
|
||||||
$location = $visit->getVisitLocation();
|
$location = $visit->getVisitLocation();
|
||||||
if ($location !== null) {
|
if ($location !== null) {
|
||||||
$tracker
|
$tracker
|
||||||
->setCity($location->getCityName())
|
->setCity($location->cityName)
|
||||||
->setCountry($location->getCountryName())
|
->setCountry($location->countryName)
|
||||||
->setLatitude($location->getLatitude())
|
->setLatitude($location->latitude)
|
||||||
->setLongitude($location->getLongitude());
|
->setLongitude($location->longitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set not obfuscated IP if possible, as matomo handles obfuscation itself
|
// Set not obfuscated IP if possible, as matomo handles obfuscation itself
|
||||||
$ip = $visitLocated->originalIpAddress ?? $visit->getRemoteAddr();
|
$ip = $visitLocated->originalIpAddress ?? $visit->remoteAddr;
|
||||||
if ($ip !== null) {
|
if ($ip !== null) {
|
||||||
$tracker->setIp($ip);
|
$tracker->setIp($ip);
|
||||||
}
|
}
|
||||||
@ -79,9 +79,9 @@ class SendVisitToMatomo
|
|||||||
|
|
||||||
public function resolveUrlToTrack(Visit $visit): string
|
public function resolveUrlToTrack(Visit $visit): string
|
||||||
{
|
{
|
||||||
$shortUrl = $visit->getShortUrl();
|
$shortUrl = $visit->shortUrl;
|
||||||
if ($shortUrl === null) {
|
if ($shortUrl === null) {
|
||||||
return $visit->visitedUrl() ?? '';
|
return $visit->visitedUrl ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->shortUrlStringifier->stringify($shortUrl);
|
return $this->shortUrlStringifier->stringify($shortUrl);
|
||||||
|
@ -20,7 +20,7 @@ final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInte
|
|||||||
public function newVisitUpdate(Visit $visit): Update
|
public function newVisitUpdate(Visit $visit): Update
|
||||||
{
|
{
|
||||||
return Update::forTopicAndPayload(Topic::NEW_VISIT->value, [
|
return Update::forTopicAndPayload(Topic::NEW_VISIT->value, [
|
||||||
'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()),
|
'shortUrl' => $this->shortUrlTransformer->transform($visit->shortUrl),
|
||||||
'visit' => $visit->jsonSerialize(),
|
'visit' => $visit->jsonSerialize(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInte
|
|||||||
|
|
||||||
public function newShortUrlVisitUpdate(Visit $visit): Update
|
public function newShortUrlVisitUpdate(Visit $visit): Update
|
||||||
{
|
{
|
||||||
$shortUrl = $visit->getShortUrl();
|
$shortUrl = $visit->shortUrl;
|
||||||
$topic = Topic::newShortUrlVisit($shortUrl?->getShortCode());
|
$topic = Topic::newShortUrlVisit($shortUrl?->getShortCode());
|
||||||
|
|
||||||
return Update::forTopicAndPayload($topic, [
|
return Update::forTopicAndPayload($topic, [
|
||||||
|
@ -32,29 +32,30 @@ use function sprintf;
|
|||||||
|
|
||||||
class ShortUrl extends AbstractEntity
|
class ShortUrl extends AbstractEntity
|
||||||
{
|
{
|
||||||
private string $longUrl;
|
/**
|
||||||
private string $shortCode;
|
* @param Collection<int, Tag> $tags
|
||||||
private Chronos $dateCreated;
|
* @param Collection<int, Visit> & Selectable $visits
|
||||||
/** @var Collection<int, Visit> & Selectable */
|
*/
|
||||||
private Collection & Selectable $visits;
|
private function __construct(
|
||||||
/** @var Collection<int, Tag> */
|
private string $longUrl,
|
||||||
private Collection $tags;
|
private string $shortCode,
|
||||||
private ?Chronos $validSince = null;
|
private Chronos $dateCreated = new Chronos(),
|
||||||
private ?Chronos $validUntil = null;
|
private Collection $tags = new ArrayCollection(),
|
||||||
private ?int $maxVisits = null;
|
private Collection & Selectable $visits = new ArrayCollection(),
|
||||||
private ?Domain $domain = null;
|
private ?Chronos $validSince = null,
|
||||||
private bool $customSlugWasProvided;
|
private ?Chronos $validUntil = null,
|
||||||
private int $shortCodeLength;
|
private ?int $maxVisits = null,
|
||||||
private ?string $importSource = null;
|
private ?Domain $domain = null,
|
||||||
private ?string $importOriginalShortCode = null;
|
private bool $customSlugWasProvided = false,
|
||||||
private ?ApiKey $authorApiKey = null;
|
private int $shortCodeLength = 0,
|
||||||
private ?string $title = null;
|
public readonly ?ApiKey $authorApiKey = null,
|
||||||
private bool $titleWasAutoResolved = false;
|
private ?string $title = null,
|
||||||
private bool $crawlable = false;
|
private bool $titleWasAutoResolved = false,
|
||||||
private bool $forwardQuery = true;
|
private bool $crawlable = false,
|
||||||
|
private bool $forwardQuery = true,
|
||||||
private function __construct()
|
private ?string $importSource = null,
|
||||||
{
|
private ?string $importOriginalShortCode = null,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,31 +79,29 @@ class ShortUrl extends AbstractEntity
|
|||||||
ShortUrlCreation $creation,
|
ShortUrlCreation $creation,
|
||||||
?ShortUrlRelationResolverInterface $relationResolver = null,
|
?ShortUrlRelationResolverInterface $relationResolver = null,
|
||||||
): self {
|
): self {
|
||||||
$instance = new self();
|
|
||||||
$relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver();
|
$relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver();
|
||||||
|
$shortCodeLength = $creation->shortCodeLength;
|
||||||
|
|
||||||
$instance->longUrl = $creation->getLongUrl();
|
return new self(
|
||||||
$instance->dateCreated = Chronos::now();
|
longUrl: $creation->getLongUrl(),
|
||||||
$instance->visits = new ArrayCollection();
|
shortCode: sprintf(
|
||||||
$instance->tags = $relationResolver->resolveTags($creation->tags);
|
'%s%s',
|
||||||
$instance->validSince = $creation->validSince;
|
$creation->pathPrefix ?? '',
|
||||||
$instance->validUntil = $creation->validUntil;
|
$creation->customSlug ?? generateRandomShortCode($shortCodeLength, $creation->shortUrlMode),
|
||||||
$instance->maxVisits = $creation->maxVisits;
|
),
|
||||||
$instance->customSlugWasProvided = $creation->hasCustomSlug();
|
tags: $relationResolver->resolveTags($creation->tags),
|
||||||
$instance->shortCodeLength = $creation->shortCodeLength;
|
validSince: $creation->validSince,
|
||||||
$instance->shortCode = sprintf(
|
validUntil: $creation->validUntil,
|
||||||
'%s%s',
|
maxVisits: $creation->maxVisits,
|
||||||
$creation->pathPrefix ?? '',
|
domain: $relationResolver->resolveDomain($creation->domain),
|
||||||
$creation->customSlug ?? generateRandomShortCode($instance->shortCodeLength, $creation->shortUrlMode),
|
customSlugWasProvided: $creation->hasCustomSlug(),
|
||||||
|
shortCodeLength: $shortCodeLength,
|
||||||
|
authorApiKey: $creation->apiKey,
|
||||||
|
title: $creation->title,
|
||||||
|
titleWasAutoResolved: $creation->titleWasAutoResolved,
|
||||||
|
crawlable: $creation->crawlable,
|
||||||
|
forwardQuery: $creation->forwardQuery,
|
||||||
);
|
);
|
||||||
$instance->domain = $relationResolver->resolveDomain($creation->domain);
|
|
||||||
$instance->authorApiKey = $creation->apiKey;
|
|
||||||
$instance->title = $creation->title;
|
|
||||||
$instance->titleWasAutoResolved = $creation->titleWasAutoResolved;
|
|
||||||
$instance->crawlable = $creation->crawlable;
|
|
||||||
$instance->forwardQuery = $creation->forwardQuery;
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromImport(
|
public static function fromImport(
|
||||||
@ -123,11 +122,11 @@ class ShortUrl extends AbstractEntity
|
|||||||
|
|
||||||
$instance = self::create(ShortUrlCreation::fromRawData($meta), $relationResolver);
|
$instance = self::create(ShortUrlCreation::fromRawData($meta), $relationResolver);
|
||||||
|
|
||||||
$instance->importSource = $url->source->value;
|
|
||||||
$instance->importOriginalShortCode = $url->shortCode;
|
|
||||||
$instance->validSince = normalizeOptionalDate($url->meta->validSince);
|
$instance->validSince = normalizeOptionalDate($url->meta->validSince);
|
||||||
$instance->validUntil = normalizeOptionalDate($url->meta->validUntil);
|
$instance->validUntil = normalizeOptionalDate($url->meta->validUntil);
|
||||||
$instance->dateCreated = normalizeDate($url->createdAt);
|
$instance->dateCreated = normalizeDate($url->createdAt);
|
||||||
|
$instance->importSource = $url->source->value;
|
||||||
|
$instance->importOriginalShortCode = $url->shortCode;
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
@ -196,11 +195,6 @@ class ShortUrl extends AbstractEntity
|
|||||||
return $this->tags;
|
return $this->tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function authorApiKey(): ?ApiKey
|
|
||||||
{
|
|
||||||
return $this->authorApiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getValidSince(): ?Chronos
|
public function getValidSince(): ?Chronos
|
||||||
{
|
{
|
||||||
return $this->validSince;
|
return $this->validSince;
|
||||||
|
@ -20,29 +20,22 @@ use function Shlinkio\Shlink\Core\normalizeDate;
|
|||||||
|
|
||||||
class Visit extends AbstractEntity implements JsonSerializable
|
class Visit extends AbstractEntity implements JsonSerializable
|
||||||
{
|
{
|
||||||
private string $referer;
|
private function __construct(
|
||||||
private Chronos $date;
|
public readonly ?ShortUrl $shortUrl,
|
||||||
private ?string $remoteAddr = null;
|
public readonly VisitType $type,
|
||||||
private ?string $visitedUrl = null;
|
public readonly string $userAgent,
|
||||||
private string $userAgent;
|
public readonly string $referer,
|
||||||
private VisitType $type;
|
private readonly bool $potentialBot,
|
||||||
private ?ShortUrl $shortUrl;
|
public readonly ?string $remoteAddr = null,
|
||||||
private ?VisitLocation $visitLocation = null;
|
public readonly ?string $visitedUrl = null,
|
||||||
private bool $potentialBot;
|
private ?VisitLocation $visitLocation = null,
|
||||||
|
private Chronos $date = new Chronos(),
|
||||||
private function __construct(?ShortUrl $shortUrl, VisitType $type)
|
) {
|
||||||
{
|
|
||||||
$this->shortUrl = $shortUrl;
|
|
||||||
$this->date = Chronos::now();
|
|
||||||
$this->type = $type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
|
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
|
||||||
{
|
{
|
||||||
$instance = new self($shortUrl, VisitType::VALID_SHORT_URL);
|
return self::hydrateFromVisitor($shortUrl, VisitType::VALID_SHORT_URL, $visitor, $anonymize);
|
||||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
|
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
|
||||||
@ -52,13 +45,10 @@ class Visit extends AbstractEntity implements JsonSerializable
|
|||||||
|
|
||||||
public static function fromOrphanImport(ImportedShlinkOrphanVisit $importedVisit): self
|
public static function fromOrphanImport(ImportedShlinkOrphanVisit $importedVisit): self
|
||||||
{
|
{
|
||||||
$instance = self::fromImportOrOrphanImport(
|
return self::fromImportOrOrphanImport(
|
||||||
$importedVisit,
|
$importedVisit,
|
||||||
VisitType::tryFrom($importedVisit->type) ?? VisitType::IMPORTED,
|
VisitType::tryFrom($importedVisit->type) ?? VisitType::IMPORTED,
|
||||||
);
|
);
|
||||||
$instance->visitedUrl = $importedVisit->visitedUrl;
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function fromImportOrOrphanImport(
|
private static function fromImportOrOrphanImport(
|
||||||
@ -66,52 +56,52 @@ class Visit extends AbstractEntity implements JsonSerializable
|
|||||||
VisitType $type,
|
VisitType $type,
|
||||||
?ShortUrl $shortUrl = null,
|
?ShortUrl $shortUrl = null,
|
||||||
): self {
|
): self {
|
||||||
$instance = new self($shortUrl, $type);
|
|
||||||
$instance->userAgent = $importedVisit->userAgent;
|
|
||||||
$instance->potentialBot = isCrawler($instance->userAgent);
|
|
||||||
$instance->referer = $importedVisit->referer;
|
|
||||||
$instance->date = normalizeDate($importedVisit->date);
|
|
||||||
|
|
||||||
$importedLocation = $importedVisit->location;
|
$importedLocation = $importedVisit->location;
|
||||||
$instance->visitLocation = $importedLocation !== null ? VisitLocation::fromImport($importedLocation) : null;
|
return new self(
|
||||||
|
shortUrl: $shortUrl,
|
||||||
return $instance;
|
type: $type,
|
||||||
|
userAgent: $importedVisit->userAgent,
|
||||||
|
referer: $importedVisit->referer,
|
||||||
|
potentialBot: isCrawler($importedVisit->userAgent),
|
||||||
|
visitedUrl: $importedVisit instanceof ImportedShlinkOrphanVisit ? $importedVisit->visitedUrl : null,
|
||||||
|
visitLocation: $importedLocation !== null ? VisitLocation::fromImport($importedLocation) : null,
|
||||||
|
date: normalizeDate($importedVisit->date),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
|
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
|
||||||
{
|
{
|
||||||
$instance = new self(null, VisitType::BASE_URL);
|
return self::hydrateFromVisitor(null, VisitType::BASE_URL, $visitor, $anonymize);
|
||||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
|
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
|
||||||
{
|
{
|
||||||
$instance = new self(null, VisitType::INVALID_SHORT_URL);
|
return self::hydrateFromVisitor(null, VisitType::INVALID_SHORT_URL, $visitor, $anonymize);
|
||||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
|
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
|
||||||
{
|
{
|
||||||
$instance = new self(null, VisitType::REGULAR_404);
|
return self::hydrateFromVisitor(null, VisitType::REGULAR_404, $visitor, $anonymize);
|
||||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function hydrateFromVisitor(Visitor $visitor, bool $anonymize = true): void
|
private static function hydrateFromVisitor(
|
||||||
{
|
?ShortUrl $shortUrl,
|
||||||
$this->userAgent = $visitor->userAgent;
|
VisitType $type,
|
||||||
$this->referer = $visitor->referer;
|
Visitor $visitor,
|
||||||
$this->remoteAddr = $this->processAddress($anonymize, $visitor->remoteAddress);
|
bool $anonymize,
|
||||||
$this->visitedUrl = $visitor->visitedUrl;
|
): self {
|
||||||
$this->potentialBot = $visitor->isPotentialBot();
|
return new self(
|
||||||
|
shortUrl: $shortUrl,
|
||||||
|
type: $type,
|
||||||
|
userAgent: $visitor->userAgent,
|
||||||
|
referer: $visitor->referer,
|
||||||
|
potentialBot: $visitor->isPotentialBot(),
|
||||||
|
remoteAddr: self::processAddress($anonymize, $visitor->remoteAddress),
|
||||||
|
visitedUrl: $visitor->visitedUrl,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processAddress(bool $anonymize, ?string $address): ?string
|
private static function processAddress(bool $anonymize, ?string $address): ?string
|
||||||
{
|
{
|
||||||
// Localhost addresses do not need to be anonymized
|
// Localhost addresses do not need to be anonymized
|
||||||
if (! $anonymize || $address === null || $address === IpAddress::LOCALHOST) {
|
if (! $anonymize || $address === null || $address === IpAddress::LOCALHOST) {
|
||||||
@ -125,21 +115,11 @@ class Visit extends AbstractEntity implements JsonSerializable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRemoteAddr(): ?string
|
|
||||||
{
|
|
||||||
return $this->remoteAddr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasRemoteAddr(): bool
|
public function hasRemoteAddr(): bool
|
||||||
{
|
{
|
||||||
return ! empty($this->remoteAddr);
|
return ! empty($this->remoteAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getShortUrl(): ?ShortUrl
|
|
||||||
{
|
|
||||||
return $this->shortUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getVisitLocation(): ?VisitLocation
|
public function getVisitLocation(): ?VisitLocation
|
||||||
{
|
{
|
||||||
return $this->visitLocation;
|
return $this->visitLocation;
|
||||||
@ -161,23 +141,13 @@ class Visit extends AbstractEntity implements JsonSerializable
|
|||||||
return $this->shortUrl === null;
|
return $this->shortUrl === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function visitedUrl(): ?string
|
|
||||||
{
|
|
||||||
return $this->visitedUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function type(): VisitType
|
|
||||||
{
|
|
||||||
return $this->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Needed only for ArrayCollections to be able to apply criteria filtering
|
* Needed only for ArrayCollections to be able to apply criteria filtering
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public function getType(): VisitType
|
public function getType(): VisitType
|
||||||
{
|
{
|
||||||
return $this->type();
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -188,16 +158,6 @@ class Visit extends AbstractEntity implements JsonSerializable
|
|||||||
return $this->date;
|
return $this->date;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function userAgent(): string
|
|
||||||
{
|
|
||||||
return $this->userAgent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function referer(): string
|
|
||||||
{
|
|
||||||
return $this->referer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function jsonSerialize(): array
|
public function jsonSerialize(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -11,89 +11,54 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
|||||||
|
|
||||||
class VisitLocation extends AbstractEntity implements JsonSerializable
|
class VisitLocation extends AbstractEntity implements JsonSerializable
|
||||||
{
|
{
|
||||||
private string $countryCode;
|
public readonly bool $isEmpty;
|
||||||
private string $countryName;
|
|
||||||
private string $regionName;
|
|
||||||
private string $cityName;
|
|
||||||
private float $latitude;
|
|
||||||
private float $longitude;
|
|
||||||
private string $timezone;
|
|
||||||
private bool $isEmpty;
|
|
||||||
|
|
||||||
private function __construct()
|
private function __construct(
|
||||||
{
|
public readonly string $countryCode,
|
||||||
|
public readonly string $countryName,
|
||||||
|
public readonly string $regionName,
|
||||||
|
public readonly string $cityName,
|
||||||
|
public readonly float $latitude,
|
||||||
|
public readonly float $longitude,
|
||||||
|
public readonly string $timezone,
|
||||||
|
) {
|
||||||
|
$this->isEmpty = (
|
||||||
|
$countryCode === '' &&
|
||||||
|
$countryName === '' &&
|
||||||
|
$regionName === '' &&
|
||||||
|
$cityName === '' &&
|
||||||
|
$latitude === 0.0 &&
|
||||||
|
$longitude === 0.0 &&
|
||||||
|
$timezone === ''
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromGeolocation(Location $location): self
|
public static function fromGeolocation(Location $location): self
|
||||||
{
|
{
|
||||||
$instance = new self();
|
return new self(
|
||||||
|
countryCode: $location->countryCode,
|
||||||
$instance->countryCode = $location->countryCode;
|
countryName: $location->countryName,
|
||||||
$instance->countryName = $location->countryName;
|
regionName: $location->regionName,
|
||||||
$instance->regionName = $location->regionName;
|
cityName: $location->city,
|
||||||
$instance->cityName = $location->city;
|
latitude: $location->latitude,
|
||||||
$instance->latitude = $location->latitude;
|
longitude: $location->longitude,
|
||||||
$instance->longitude = $location->longitude;
|
timezone: $location->timeZone,
|
||||||
$instance->timezone = $location->timeZone;
|
);
|
||||||
$instance->computeIsEmpty();
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromImport(ImportedShlinkVisitLocation $location): self
|
public static function fromImport(ImportedShlinkVisitLocation $location): self
|
||||||
{
|
{
|
||||||
$instance = new self();
|
return new self(
|
||||||
|
countryCode: $location->countryCode,
|
||||||
$instance->countryCode = $location->countryCode;
|
countryName: $location->countryName,
|
||||||
$instance->countryName = $location->countryName;
|
regionName: $location->regionName,
|
||||||
$instance->regionName = $location->regionName;
|
cityName: $location->cityName,
|
||||||
$instance->cityName = $location->cityName;
|
latitude: $location->latitude,
|
||||||
$instance->latitude = $location->latitude;
|
longitude: $location->longitude,
|
||||||
$instance->longitude = $location->longitude;
|
timezone: $location->timezone,
|
||||||
$instance->timezone = $location->timezone;
|
|
||||||
$instance->computeIsEmpty();
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function computeIsEmpty(): void
|
|
||||||
{
|
|
||||||
$this->isEmpty = (
|
|
||||||
$this->countryCode === '' &&
|
|
||||||
$this->countryName === '' &&
|
|
||||||
$this->regionName === '' &&
|
|
||||||
$this->cityName === '' &&
|
|
||||||
$this->latitude === 0.0 &&
|
|
||||||
$this->longitude === 0.0 &&
|
|
||||||
$this->timezone === ''
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCountryName(): string
|
|
||||||
{
|
|
||||||
return $this->countryName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLatitude(): float
|
|
||||||
{
|
|
||||||
return $this->latitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLongitude(): float
|
|
||||||
{
|
|
||||||
return $this->longitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCityName(): string
|
|
||||||
{
|
|
||||||
return $this->cityName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isEmpty(): bool
|
|
||||||
{
|
|
||||||
return $this->isEmpty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function jsonSerialize(): array
|
public function jsonSerialize(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -26,7 +26,7 @@ class VisitToLocationHelper implements VisitToLocationHelperInterface
|
|||||||
throw IpCannotBeLocatedException::forEmptyAddress();
|
throw IpCannotBeLocatedException::forEmptyAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
$ipAddr = $visit->getRemoteAddr() ?? '';
|
$ipAddr = $visit->remoteAddr ?? '';
|
||||||
if ($ipAddr === IpAddress::LOCALHOST) {
|
if ($ipAddr === IpAddress::LOCALHOST) {
|
||||||
throw IpCannotBeLocatedException::forLocalhost();
|
throw IpCannotBeLocatedException::forLocalhost();
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,8 @@ class OrphanVisitDataTransformer implements DataTransformerInterface
|
|||||||
public function transform($visit): array // phpcs:ignore
|
public function transform($visit): array // phpcs:ignore
|
||||||
{
|
{
|
||||||
$serializedVisit = $visit->jsonSerialize();
|
$serializedVisit = $visit->jsonSerialize();
|
||||||
$serializedVisit['visitedUrl'] = $visit->visitedUrl();
|
$serializedVisit['visitedUrl'] = $visit->visitedUrl;
|
||||||
$serializedVisit['type'] = $visit->type()->value;
|
$serializedVisit['type'] = $visit->type->value;
|
||||||
|
|
||||||
return $serializedVisit;
|
return $serializedVisit;
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,7 @@ class LocateVisitTest extends TestCase
|
|||||||
#[Test, DataProvider('provideIpAddresses')]
|
#[Test, DataProvider('provideIpAddresses')]
|
||||||
public function locatableVisitsResolveToLocation(Visit $visit, ?string $originalIpAddress): void
|
public function locatableVisitsResolveToLocation(Visit $visit, ?string $originalIpAddress): void
|
||||||
{
|
{
|
||||||
$ipAddr = $originalIpAddress ?? $visit->getRemoteAddr();
|
$ipAddr = $originalIpAddress ?? $visit->remoteAddr;
|
||||||
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
||||||
$event = new UrlVisited('123', $originalIpAddress);
|
$event = new UrlVisited('123', $originalIpAddress);
|
||||||
|
|
||||||
|
@ -76,13 +76,13 @@ class SendVisitToMatomoTest extends TestCase
|
|||||||
|
|
||||||
if ($visit->isOrphan()) {
|
if ($visit->isOrphan()) {
|
||||||
$tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([
|
$tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([
|
||||||
['type', $visit->type()->value, $tracker],
|
['type', $visit->type->value, $tracker],
|
||||||
['orphan', 'true', $tracker],
|
['orphan', 'true', $tracker],
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
$tracker->expects($this->once())->method('setCustomTrackingParameter')->with(
|
$tracker->expects($this->once())->method('setCustomTrackingParameter')->with(
|
||||||
'type',
|
'type',
|
||||||
$visit->type()->value,
|
$visit->type->value,
|
||||||
)->willReturn($tracker);
|
)->willReturn($tracker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,8 +93,8 @@ class PublishingUpdatesGeneratorTest extends TestCase
|
|||||||
'visitLocation' => null,
|
'visitLocation' => null,
|
||||||
'date' => $orphanVisit->getDate()->toAtomString(),
|
'date' => $orphanVisit->getDate()->toAtomString(),
|
||||||
'potentialBot' => false,
|
'potentialBot' => false,
|
||||||
'visitedUrl' => $orphanVisit->visitedUrl(),
|
'visitedUrl' => $orphanVisit->visitedUrl,
|
||||||
'type' => $orphanVisit->type()->value,
|
'type' => $orphanVisit->type->value,
|
||||||
],
|
],
|
||||||
], $update->payload);
|
], $update->payload);
|
||||||
}
|
}
|
||||||
|
@ -244,7 +244,7 @@ class ImportedLinksProcessorTest extends TestCase
|
|||||||
$this->em->expects($this->once())->method('persist')->willReturnCallback(
|
$this->em->expects($this->once())->method('persist')->willReturnCallback(
|
||||||
static fn (Visit $visit) => Assert::assertSame(
|
static fn (Visit $visit) => Assert::assertSame(
|
||||||
$foundShortUrl ?? $originalShortUrl,
|
$foundShortUrl ?? $originalShortUrl,
|
||||||
$visit->getShortUrl(),
|
$visit->shortUrl,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ class VisitLocationTest extends TestCase
|
|||||||
$payload = new Location(...$args);
|
$payload = new Location(...$args);
|
||||||
$location = VisitLocation::fromGeolocation($payload);
|
$location = VisitLocation::fromGeolocation($payload);
|
||||||
|
|
||||||
self::assertEquals($isEmpty, $location->isEmpty());
|
self::assertEquals($isEmpty, $location->isEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function provideArgs(): iterable
|
public static function provideArgs(): iterable
|
||||||
|
@ -49,7 +49,7 @@ class VisitTest extends TestCase
|
|||||||
$anonymize,
|
$anonymize,
|
||||||
);
|
);
|
||||||
|
|
||||||
self::assertEquals($expectedAddress, $visit->getRemoteAddr());
|
self::assertEquals($expectedAddress, $visit->remoteAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function provideAddresses(): iterable
|
public static function provideAddresses(): iterable
|
||||||
|
@ -17,21 +17,17 @@ use Shlinkio\Shlink\Rest\ApiKey\Role;
|
|||||||
|
|
||||||
class ApiKey extends AbstractEntity
|
class ApiKey extends AbstractEntity
|
||||||
{
|
{
|
||||||
private string $key;
|
|
||||||
private ?Chronos $expirationDate = null;
|
|
||||||
private bool $enabled;
|
|
||||||
/** @var Collection<string, ApiKeyRole> */
|
|
||||||
private Collection $roles;
|
|
||||||
private ?string $name = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param Collection<string, ApiKeyRole> $roles
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function __construct(string $key)
|
private function __construct(
|
||||||
{
|
private string $key,
|
||||||
$this->key = $key;
|
public readonly ?string $name = null,
|
||||||
$this->enabled = true;
|
public readonly ?Chronos $expirationDate = null,
|
||||||
$this->roles = new ArrayCollection();
|
private bool $enabled = true,
|
||||||
|
private Collection $roles = new ArrayCollection(),
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,10 +43,7 @@ class ApiKey extends AbstractEntity
|
|||||||
*/
|
*/
|
||||||
public static function fromMeta(ApiKeyMeta $meta): self
|
public static function fromMeta(ApiKeyMeta $meta): self
|
||||||
{
|
{
|
||||||
$apiKey = new self($meta->key);
|
$apiKey = new self($meta->key, $meta->name, $meta->expirationDate);
|
||||||
$apiKey->name = $meta->name;
|
|
||||||
$apiKey->expirationDate = $meta->expirationDate;
|
|
||||||
|
|
||||||
foreach ($meta->roleDefinitions as $roleDefinition) {
|
foreach ($meta->roleDefinitions as $roleDefinition) {
|
||||||
$apiKey->registerRole($roleDefinition);
|
$apiKey->registerRole($roleDefinition);
|
||||||
}
|
}
|
||||||
@ -58,21 +51,11 @@ class ApiKey extends AbstractEntity
|
|||||||
return $apiKey;
|
return $apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExpirationDate(): ?Chronos
|
|
||||||
{
|
|
||||||
return $this->expirationDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isExpired(): bool
|
public function isExpired(): bool
|
||||||
{
|
{
|
||||||
return $this->expirationDate !== null && $this->expirationDate->lessThan(Chronos::now());
|
return $this->expirationDate !== null && $this->expirationDate->lessThan(Chronos::now());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function name(): ?string
|
|
||||||
{
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isEnabled(): bool
|
public function isEnabled(): bool
|
||||||
{
|
{
|
||||||
return $this->enabled;
|
return $this->enabled;
|
||||||
|
@ -44,8 +44,8 @@ class ApiKeyServiceTest extends TestCase
|
|||||||
ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles),
|
ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles),
|
||||||
);
|
);
|
||||||
|
|
||||||
self::assertEquals($date, $key->getExpirationDate());
|
self::assertEquals($date, $key->expirationDate);
|
||||||
self::assertEquals($name, $key->name());
|
self::assertEquals($name, $key->name);
|
||||||
foreach ($roles as $roleDefinition) {
|
foreach ($roles as $roleDefinition) {
|
||||||
self::assertTrue($key->hasRole($roleDefinition->role));
|
self::assertTrue($key->hasRole($roleDefinition->role));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user