mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Created named constructors for Visit entity and added tracking of the visited URL
This commit is contained in:
@@ -21,21 +21,27 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
|
||||
private string $referer;
|
||||
private Chronos $date;
|
||||
private ?string $remoteAddr = null;
|
||||
private ?string $visitedUrl = null;
|
||||
private ?string $remoteAddr;
|
||||
private ?string $visitedUrl;
|
||||
private string $userAgent;
|
||||
private string $type;
|
||||
private ?ShortUrl $shortUrl;
|
||||
private ?VisitLocation $visitLocation = null;
|
||||
|
||||
public function __construct(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true, ?Chronos $date = null)
|
||||
{
|
||||
public function __construct(
|
||||
?ShortUrl $shortUrl,
|
||||
Visitor $visitor,
|
||||
bool $anonymize = true,
|
||||
?Chronos $date = null,
|
||||
string $type = self::TYPE_VALID_SHORT_URL
|
||||
) {
|
||||
$this->shortUrl = $shortUrl;
|
||||
$this->date = $date ?? Chronos::now();
|
||||
$this->userAgent = $visitor->getUserAgent();
|
||||
$this->referer = $visitor->getReferer();
|
||||
$this->remoteAddr = $this->processAddress($anonymize, $visitor->getRemoteAddress());
|
||||
$this->type = self::TYPE_VALID_SHORT_URL;
|
||||
$this->visitedUrl = $visitor->getVisitedUrl();
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
private function processAddress(bool $anonymize, ?string $address): ?string
|
||||
@@ -52,6 +58,26 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
}
|
||||
}
|
||||
|
||||
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self($shortUrl, $visitor, $anonymize);
|
||||
}
|
||||
|
||||
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self(null, $visitor, $anonymize, null, self::TYPE_BASE_URL);
|
||||
}
|
||||
|
||||
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self(null, $visitor, $anonymize, null, self::TYPE_INVALID_SHORT_URL);
|
||||
}
|
||||
|
||||
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self(null, $visitor, $anonymize, null, self::TYPE_REGULAR_404);
|
||||
}
|
||||
|
||||
public function getRemoteAddr(): ?string
|
||||
{
|
||||
return $this->remoteAddr;
|
||||
|
||||
@@ -18,12 +18,14 @@ final class Visitor
|
||||
|
||||
private string $userAgent;
|
||||
private string $referer;
|
||||
private string $visitedUrl;
|
||||
private ?string $remoteAddress;
|
||||
|
||||
public function __construct(string $userAgent, string $referer, ?string $remoteAddress)
|
||||
public function __construct(string $userAgent, string $referer, ?string $remoteAddress, string $visitedUrl)
|
||||
{
|
||||
$this->userAgent = $this->cropToLength($userAgent, self::USER_AGENT_MAX_LENGTH);
|
||||
$this->referer = $this->cropToLength($referer, self::REFERER_MAX_LENGTH);
|
||||
$this->visitedUrl = $this->cropToLength($visitedUrl, self::VISITED_URL_MAX_LENGTH);
|
||||
$this->remoteAddress = $this->cropToLength($remoteAddress, self::REMOTE_ADDRESS_MAX_LENGTH);
|
||||
}
|
||||
|
||||
@@ -38,12 +40,13 @@ final class Visitor
|
||||
$request->getHeaderLine('User-Agent'),
|
||||
$request->getHeaderLine('Referer'),
|
||||
$request->getAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR),
|
||||
$request->getUri()->__toString(),
|
||||
);
|
||||
}
|
||||
|
||||
public static function emptyInstance(): self
|
||||
{
|
||||
return new self('', '', null);
|
||||
return new self('', '', null, '');
|
||||
}
|
||||
|
||||
public function getUserAgent(): string
|
||||
@@ -60,4 +63,9 @@ final class Visitor
|
||||
{
|
||||
return $this->remoteAddress;
|
||||
}
|
||||
|
||||
public function getVisitedUrl(): string
|
||||
{
|
||||
return $this->visitedUrl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ class VisitsTracker implements VisitsTrackerInterface
|
||||
|
||||
public function track(ShortUrl $shortUrl, Visitor $visitor): void
|
||||
{
|
||||
$visit = new Visit($shortUrl, $visitor, $this->anonymizeRemoteAddr);
|
||||
$visit = Visit::forValidShortUrl($shortUrl, $visitor, $this->anonymizeRemoteAddr);
|
||||
|
||||
$this->em->persist($visit);
|
||||
$this->em->flush();
|
||||
|
||||
Reference in New Issue
Block a user