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; } private function processAddress(bool $anonymize, ?string $address): ?string { // Localhost addresses do not need to be anonymized if (! $anonymize || $address === null || $address === IpAddress::LOCALHOST) { return $address; } try { return (string) IpAddress::fromString($address)->getAnonymizedCopy(); } catch (InvalidArgumentException $e) { return null; } } public function getRemoteAddr(): ?string { return $this->remoteAddr; } public function hasRemoteAddr(): bool { return ! empty($this->remoteAddr); } public function getShortUrl(): ?ShortUrl { return $this->shortUrl; } public function getVisitLocation(): ?VisitLocationInterface { return $this->visitLocation; } public function isLocatable(): bool { return $this->hasRemoteAddr() && $this->remoteAddr !== IpAddress::LOCALHOST; } public function locate(VisitLocation $visitLocation): self { $this->visitLocation = $visitLocation; return $this; } public function jsonSerialize(): array { return [ 'referer' => $this->referer, 'date' => $this->date->toAtomString(), 'userAgent' => $this->userAgent, 'visitLocation' => $this->visitLocation, ]; } /** * @internal */ public function getDate(): Chronos { return $this->date; } }