mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-08 08:13:11 +08:00
Updated ImportedLinksProcessor to support importing visits if provided
This commit is contained in:
@@ -11,6 +11,7 @@ use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
||||
use Shlinkio\Shlink\Common\Util\IpAddress;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit;
|
||||
|
||||
class Visit extends AbstractEntity implements JsonSerializable
|
||||
{
|
||||
@@ -21,22 +22,71 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
|
||||
private string $referer;
|
||||
private Chronos $date;
|
||||
private ?string $remoteAddr;
|
||||
private ?string $visitedUrl;
|
||||
private ?string $remoteAddr = null;
|
||||
private ?string $visitedUrl = null;
|
||||
private string $userAgent;
|
||||
private string $type;
|
||||
private ?ShortUrl $shortUrl;
|
||||
private ?VisitLocation $visitLocation = null;
|
||||
|
||||
private function __construct(?ShortUrl $shortUrl, Visitor $visitor, string $type, bool $anonymize = true)
|
||||
private function __construct(?ShortUrl $shortUrl, string $type)
|
||||
{
|
||||
$this->shortUrl = $shortUrl;
|
||||
$this->date = Chronos::now();
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
$instance = new self($shortUrl, self::TYPE_VALID_SHORT_URL);
|
||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromImport(ImportedShlinkVisit $importedVisit, ShortUrl $shortUrl): self
|
||||
{
|
||||
$instance = new self($shortUrl, self::TYPE_VALID_SHORT_URL);
|
||||
$instance->userAgent = $importedVisit->userAgent();
|
||||
$instance->referer = $importedVisit->referer();
|
||||
$instance->date = Chronos::instance($importedVisit->date());
|
||||
|
||||
$importedLocation = $importedVisit->location();
|
||||
$instance->visitLocation = $importedLocation !== null ? VisitLocation::fromImport($importedLocation) : null;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
$instance = new self(null, self::TYPE_BASE_URL);
|
||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
$instance = new self(null, self::TYPE_INVALID_SHORT_URL);
|
||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
$instance = new self(null, self::TYPE_REGULAR_404);
|
||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
private function hydrateFromVisitor(Visitor $visitor, bool $anonymize = true): void
|
||||
{
|
||||
$this->userAgent = $visitor->getUserAgent();
|
||||
$this->referer = $visitor->getReferer();
|
||||
$this->remoteAddr = $this->processAddress($anonymize, $visitor->getRemoteAddress());
|
||||
$this->visitedUrl = $visitor->getVisitedUrl();
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
private function processAddress(bool $anonymize, ?string $address): ?string
|
||||
@@ -53,26 +103,6 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
}
|
||||
}
|
||||
|
||||
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self($shortUrl, $visitor, self::TYPE_VALID_SHORT_URL, $anonymize);
|
||||
}
|
||||
|
||||
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self(null, $visitor, self::TYPE_BASE_URL, $anonymize);
|
||||
}
|
||||
|
||||
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self(null, $visitor, self::TYPE_INVALID_SHORT_URL, $anonymize);
|
||||
}
|
||||
|
||||
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
|
||||
{
|
||||
return new self(null, $visitor, self::TYPE_REGULAR_404, $anonymize);
|
||||
}
|
||||
|
||||
public function getRemoteAddr(): ?string
|
||||
{
|
||||
return $this->remoteAddr;
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Entity;
|
||||
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisitLocation;
|
||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||
|
||||
class VisitLocation extends AbstractEntity implements VisitLocationInterface
|
||||
@@ -19,9 +20,53 @@ class VisitLocation extends AbstractEntity implements VisitLocationInterface
|
||||
private string $timezone;
|
||||
private bool $isEmpty;
|
||||
|
||||
public function __construct(Location $location)
|
||||
private function __construct()
|
||||
{
|
||||
$this->exchangeLocationInfo($location);
|
||||
}
|
||||
|
||||
public static function fromGeolocation(Location $location): self
|
||||
{
|
||||
$instance = new self();
|
||||
|
||||
$instance->countryCode = $location->countryCode();
|
||||
$instance->countryName = $location->countryName();
|
||||
$instance->regionName = $location->regionName();
|
||||
$instance->cityName = $location->city();
|
||||
$instance->latitude = $location->latitude();
|
||||
$instance->longitude = $location->longitude();
|
||||
$instance->timezone = $location->timeZone();
|
||||
$instance->computeIsEmpty();
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromImport(ImportedShlinkVisitLocation $location): self
|
||||
{
|
||||
$instance = new self();
|
||||
|
||||
$instance->countryCode = $location->countryCode();
|
||||
$instance->countryName = $location->countryName();
|
||||
$instance->regionName = $location->regionName();
|
||||
$instance->cityName = $location->cityName();
|
||||
$instance->latitude = $location->latitude();
|
||||
$instance->longitude = $location->longitude();
|
||||
$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
|
||||
@@ -49,26 +94,6 @@ class VisitLocation extends AbstractEntity implements VisitLocationInterface
|
||||
return $this->isEmpty;
|
||||
}
|
||||
|
||||
private function exchangeLocationInfo(Location $info): void
|
||||
{
|
||||
$this->countryCode = $info->countryCode();
|
||||
$this->countryName = $info->countryName();
|
||||
$this->regionName = $info->regionName();
|
||||
$this->cityName = $info->city();
|
||||
$this->latitude = $info->latitude();
|
||||
$this->longitude = $info->longitude();
|
||||
$this->timezone = $info->timeZone();
|
||||
$this->isEmpty = (
|
||||
$this->countryCode === '' &&
|
||||
$this->countryName === '' &&
|
||||
$this->regionName === '' &&
|
||||
$this->cityName === '' &&
|
||||
$this->latitude === 0.0 &&
|
||||
$this->longitude === 0.0 &&
|
||||
$this->timezone === ''
|
||||
);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user