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
|
|
|
|
2018-09-29 13:52:32 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
2018-10-28 10:24:06 +03:00
|
|
|
use JsonSerializable;
|
2016-07-19 19:01:39 +03:00
|
|
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
2019-08-11 15:29:22 +03:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2018-09-13 23:36:28 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2022-04-23 19:19:16 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
2021-04-11 00:24:01 +03:00
|
|
|
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit;
|
2016-04-17 11:46:35 +03:00
|
|
|
|
2021-05-22 16:09:14 +03:00
|
|
|
use function Shlinkio\Shlink\Core\isCrawler;
|
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
class Visit extends AbstractEntity implements JsonSerializable
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2020-01-10 21:44:35 +03:00
|
|
|
private string $referer;
|
2019-12-30 00:27:00 +03:00
|
|
|
private Chronos $date;
|
2021-04-11 00:24:01 +03:00
|
|
|
private ?string $remoteAddr = null;
|
|
|
|
private ?string $visitedUrl = null;
|
2020-01-10 21:44:35 +03:00
|
|
|
private string $userAgent;
|
2022-04-23 19:19:16 +03:00
|
|
|
private VisitType $type;
|
2021-02-07 13:02:50 +03:00
|
|
|
private ?ShortUrl $shortUrl;
|
2019-12-30 00:27:00 +03:00
|
|
|
private ?VisitLocation $visitLocation = null;
|
2021-05-22 16:09:14 +03:00
|
|
|
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)
|
2021-02-09 23:22:36 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
$this->shortUrl = $shortUrl;
|
2021-02-09 23:22:36 +03:00
|
|
|
$this->date = Chronos::now();
|
2021-02-07 23:31:12 +03:00
|
|
|
$this->type = $type;
|
2018-10-18 20:19:42 +03:00
|
|
|
}
|
|
|
|
|
2021-04-11 00:24:01 +03:00
|
|
|
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
|
2018-09-13 23:36:28 +03:00
|
|
|
{
|
2022-04-23 19:19:16 +03:00
|
|
|
$instance = new self($shortUrl, VisitType::VALID_SHORT_URL);
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance->hydrateFromVisitor($visitor, $anonymize);
|
2018-09-13 23:36:28 +03:00
|
|
|
|
2021-04-11 00:24:01 +03:00
|
|
|
return $instance;
|
2018-09-13 23:36:28 +03:00
|
|
|
}
|
|
|
|
|
2021-04-11 20:56:00 +03:00
|
|
|
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
|
2021-02-07 23:31:12 +03:00
|
|
|
{
|
2022-04-23 19:19:16 +03:00
|
|
|
$instance = new self($shortUrl, VisitType::IMPORTED);
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance->userAgent = $importedVisit->userAgent();
|
2021-05-22 16:09:14 +03:00
|
|
|
$instance->potentialBot = isCrawler($instance->userAgent);
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance->referer = $importedVisit->referer();
|
|
|
|
$instance->date = Chronos::instance($importedVisit->date());
|
|
|
|
|
|
|
|
$importedLocation = $importedVisit->location();
|
|
|
|
$instance->visitLocation = $importedLocation !== null ? VisitLocation::fromImport($importedLocation) : null;
|
|
|
|
|
|
|
|
return $instance;
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
|
|
|
|
|
|
return $instance;
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
|
|
|
|
|
|
return $instance;
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function hydrateFromVisitor(Visitor $visitor, bool $anonymize = true): void
|
|
|
|
{
|
2022-04-23 15:00:47 +03:00
|
|
|
$this->userAgent = $visitor->userAgent;
|
|
|
|
$this->referer = $visitor->referer;
|
|
|
|
$this->remoteAddr = $this->processAddress($anonymize, $visitor->remoteAddress);
|
|
|
|
$this->visitedUrl = $visitor->visitedUrl;
|
2021-05-22 16:09:14 +03:00
|
|
|
$this->potentialBot = $visitor->isPotentialBot();
|
2021-04-11 00:24:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-04-11 00:24:01 +03:00
|
|
|
return null;
|
|
|
|
}
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
2018-10-28 18:20:10 +03:00
|
|
|
public function getRemoteAddr(): ?string
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2018-10-28 18:20:10 +03:00
|
|
|
return $this->remoteAddr;
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|
|
|
|
|
2018-10-28 18:20:10 +03:00
|
|
|
public function hasRemoteAddr(): bool
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2018-10-28 18:20:10 +03:00
|
|
|
return ! empty($this->remoteAddr);
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|
2016-06-12 22:51:06 +03:00
|
|
|
|
2021-02-07 13:02:50 +03:00
|
|
|
public function getShortUrl(): ?ShortUrl
|
2019-12-27 19:07:20 +03:00
|
|
|
{
|
|
|
|
return $this->shortUrl;
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:22:29 +03:00
|
|
|
public function getVisitLocation(): ?VisitLocation
|
2016-07-20 10:35:46 +03:00
|
|
|
{
|
2020-03-28 11:27:45 +03:00
|
|
|
return $this->visitLocation;
|
2016-07-20 10:35:46 +03:00
|
|
|
}
|
|
|
|
|
2019-07-13 13:04:21 +03:00
|
|
|
public function isLocatable(): bool
|
|
|
|
{
|
|
|
|
return $this->hasRemoteAddr() && $this->remoteAddr !== IpAddress::LOCALHOST;
|
|
|
|
}
|
|
|
|
|
2018-11-17 09:47:42 +03:00
|
|
|
public function locate(VisitLocation $visitLocation): self
|
2016-07-20 10:35:46 +03:00
|
|
|
{
|
|
|
|
$this->visitLocation = $visitLocation;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
public function isOrphan(): bool
|
|
|
|
{
|
|
|
|
return $this->shortUrl === null;
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:11:09 +03:00
|
|
|
public function visitedUrl(): ?string
|
|
|
|
{
|
|
|
|
return $this->visitedUrl;
|
|
|
|
}
|
|
|
|
|
2022-04-23 19:19:16 +03:00
|
|
|
public function type(): VisitType
|
2021-02-10 00:11:09 +03:00
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:44:02 +03:00
|
|
|
/**
|
|
|
|
* Needed only for ArrayCollections to be able to apply criteria filtering
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-04-23 19:19:16 +03:00
|
|
|
public function getType(): VisitType
|
2021-04-18 13:44:02 +03:00
|
|
|
{
|
|
|
|
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
|
2016-06-12 22:51:06 +03:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'referer' => $this->referer,
|
2019-12-30 01:19:00 +03:00
|
|
|
'date' => $this->date->toAtomString(),
|
2016-06-12 22:51:06 +03:00
|
|
|
'userAgent' => $this->userAgent,
|
2016-07-20 10:35:46 +03:00
|
|
|
'visitLocation' => $this->visitLocation,
|
2021-05-22 16:09:14 +03:00
|
|
|
'potentialBot' => $this->potentialBot,
|
2016-06-12 22:51:06 +03:00
|
|
|
];
|
|
|
|
}
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|