shlink/module/Core/src/Entity/Visit.php

180 lines
5 KiB
PHP
Raw Normal View History

2016-04-17 11:46:35 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
2016-07-19 19:01:39 +03:00
namespace Shlinkio\Shlink\Core\Entity;
2016-04-17 11:46:35 +03:00
use Cake\Chronos\Chronos;
use JsonSerializable;
2016-07-19 19:01:39 +03:00
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Model\Visitor;
2022-04-23 19:19:16 +03:00
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit;
2016-04-17 11:46:35 +03:00
use function Shlinkio\Shlink\Core\isCrawler;
class Visit extends AbstractEntity implements JsonSerializable
2016-04-17 11:46:35 +03:00
{
private string $referer;
2019-12-30 00:27:00 +03:00
private Chronos $date;
private ?string $remoteAddr = null;
private ?string $visitedUrl = null;
private string $userAgent;
2022-04-23 19:19:16 +03:00
private VisitType $type;
private ?ShortUrl $shortUrl;
2019-12-30 00:27:00 +03:00
private ?VisitLocation $visitLocation = null;
private bool $potentialBot;
2016-04-17 11:46:35 +03:00
2022-04-23 19:19:16 +03:00
private function __construct(?ShortUrl $shortUrl, VisitType $type)
{
$this->shortUrl = $shortUrl;
$this->date = Chronos::now();
$this->type = $type;
}
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
{
2022-04-23 19:19:16 +03:00
$instance = new self($shortUrl, VisitType::VALID_SHORT_URL);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
}
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
{
2022-04-23 19:19:16 +03:00
$instance = new self($shortUrl, VisitType::IMPORTED);
$instance->userAgent = $importedVisit->userAgent();
$instance->potentialBot = isCrawler($instance->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
{
2022-04-23 19:19:16 +03:00
$instance = new self(null, VisitType::BASE_URL);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
}
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
{
2022-04-23 19:19:16 +03:00
$instance = new self(null, VisitType::INVALID_SHORT_URL);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
}
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
{
2022-04-23 19:19:16 +03:00
$instance = new self(null, VisitType::REGULAR_404);
$instance->hydrateFromVisitor($visitor, $anonymize);
return $instance;
}
private function hydrateFromVisitor(Visitor $visitor, bool $anonymize = true): void
{
$this->userAgent = $visitor->userAgent;
$this->referer = $visitor->referer;
$this->remoteAddr = $this->processAddress($anonymize, $visitor->remoteAddress);
$this->visitedUrl = $visitor->visitedUrl;
$this->potentialBot = $visitor->isPotentialBot();
}
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 {
2021-12-10 15:42:33 +03:00
return IpAddress::fromString($address)->getAnonymizedCopy()->__toString();
2021-05-23 13:37:53 +03:00
} catch (InvalidArgumentException) {
return null;
}
}
public function getRemoteAddr(): ?string
2016-04-17 11:46:35 +03:00
{
return $this->remoteAddr;
2016-04-17 11:46:35 +03:00
}
public function hasRemoteAddr(): bool
2016-04-17 11:46:35 +03:00
{
return ! empty($this->remoteAddr);
2016-04-17 11:46:35 +03:00
}
public function getShortUrl(): ?ShortUrl
{
return $this->shortUrl;
}
public function getVisitLocation(): ?VisitLocation
2016-07-20 10:35:46 +03:00
{
return $this->visitLocation;
2016-07-20 10:35:46 +03:00
}
public function isLocatable(): bool
{
return $this->hasRemoteAddr() && $this->remoteAddr !== IpAddress::LOCALHOST;
}
public function locate(VisitLocation $visitLocation): self
2016-07-20 10:35:46 +03:00
{
$this->visitLocation = $visitLocation;
return $this;
}
public function isOrphan(): bool
{
return $this->shortUrl === null;
}
public function visitedUrl(): ?string
{
return $this->visitedUrl;
}
2022-04-23 19:19:16 +03:00
public function type(): VisitType
{
return $this->type;
}
/**
* Needed only for ArrayCollections to be able to apply criteria filtering
* @internal
*/
2022-04-23 19:19:16 +03:00
public function getType(): VisitType
{
return $this->type();
}
2022-04-23 19:19:16 +03:00
/**
* @internal
*/
public function getDate(): Chronos
{
return $this->date;
}
2018-03-28 00:56:55 +03:00
public function jsonSerialize(): array
{
return [
'referer' => $this->referer,
2019-12-30 01:19:00 +03:00
'date' => $this->date->toAtomString(),
'userAgent' => $this->userAgent,
2016-07-20 10:35:46 +03:00
'visitLocation' => $this->visitLocation,
'potentialBot' => $this->potentialBot,
];
}
2016-04-17 11:46:35 +03:00
}