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;
|
2018-12-08 14:12:11 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
|
2021-04-11 00:24:01 +03:00
|
|
|
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit;
|
2016-04-17 11:46:35 +03:00
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
class Visit extends AbstractEntity implements JsonSerializable
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2021-02-07 13:26:01 +03:00
|
|
|
public const TYPE_VALID_SHORT_URL = 'valid_short_url';
|
2021-04-11 20:56:00 +03:00
|
|
|
public const TYPE_IMPORTED = 'imported';
|
2021-02-07 13:26:01 +03:00
|
|
|
public const TYPE_INVALID_SHORT_URL = 'invalid_short_url';
|
|
|
|
public const TYPE_BASE_URL = 'base_url';
|
|
|
|
public const TYPE_REGULAR_404 = 'regular_404';
|
|
|
|
|
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;
|
2021-02-07 13:26:01 +03:00
|
|
|
private string $type;
|
2021-02-07 13:02:50 +03:00
|
|
|
private ?ShortUrl $shortUrl;
|
2019-12-30 00:27:00 +03:00
|
|
|
private ?VisitLocation $visitLocation = null;
|
2016-04-17 11:46:35 +03:00
|
|
|
|
2021-04-11 00:24:01 +03:00
|
|
|
private function __construct(?ShortUrl $shortUrl, string $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
|
|
|
{
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance = new self($shortUrl, self::TYPE_VALID_SHORT_URL);
|
|
|
|
$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
|
|
|
{
|
2021-04-11 20:56:00 +03:00
|
|
|
$instance = new self($shortUrl, self::TYPE_IMPORTED);
|
2021-04-11 00:24:01 +03:00
|
|
|
$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;
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
|
|
|
|
{
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance = new self(null, self::TYPE_BASE_URL);
|
|
|
|
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
|
|
|
|
|
|
return $instance;
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
|
|
|
|
{
|
2021-04-11 00:24:01 +03:00
|
|
|
$instance = new self(null, self::TYPE_INVALID_SHORT_URL);
|
|
|
|
$instance->hydrateFromVisitor($visitor, $anonymize);
|
|
|
|
|
|
|
|
return $instance;
|
2021-02-07 23:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
|
|
|
|
{
|
2021-04-11 00:24:01 +03:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-28 11:27:45 +03:00
|
|
|
public function getVisitLocation(): ?VisitLocationInterface
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function type(): string
|
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:44:02 +03:00
|
|
|
/**
|
|
|
|
* Needed only for ArrayCollections to be able to apply criteria filtering
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public function getType(): string
|
|
|
|
{
|
|
|
|
return $this->type();
|
|
|
|
}
|
|
|
|
|
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,
|
2016-06-12 22:51:06 +03:00
|
|
|
];
|
|
|
|
}
|
2018-11-17 21:23:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public function getDate(): Chronos
|
|
|
|
{
|
|
|
|
return $this->date;
|
|
|
|
}
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|