Updated ImportedLinksProcessor to support importing visits if provided

This commit is contained in:
Alejandro Celaya
2021-04-10 23:24:01 +02:00
parent e23cd6a856
commit 1efa973507
13 changed files with 132 additions and 70 deletions

View File

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