Created named constructors for Visit entity and added tracking of the visited URL

This commit is contained in:
Alejandro Celaya
2021-02-07 21:31:12 +01:00
parent f5666c9451
commit 12b07bb0ac
19 changed files with 101 additions and 60 deletions

View File

@@ -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;