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

135 lines
3.8 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;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
2016-04-17 11:46:35 +03:00
class Visit extends AbstractEntity implements JsonSerializable
2016-04-17 11:46:35 +03:00
{
public const TYPE_VALID_SHORT_URL = 'valid_short_url';
public const TYPE_INVALID_SHORT_URL = 'invalid_short_url';
public const TYPE_BASE_URL = 'base_url';
public const TYPE_REGULAR_404 = 'regular_404';
private string $referer;
2019-12-30 00:27:00 +03:00
private Chronos $date;
private ?string $remoteAddr;
private ?string $visitedUrl;
private string $userAgent;
private string $type;
private ?ShortUrl $shortUrl;
2019-12-30 00:27:00 +03:00
private ?VisitLocation $visitLocation = null;
2016-04-17 11:46:35 +03:00
public function __construct(
?ShortUrl $shortUrl,
Visitor $visitor,
bool $anonymize = true,
?Chronos $date = null,
string $type = self::TYPE_VALID_SHORT_URL
) {
$this->shortUrl = $shortUrl;
$this->date = $date ?? Chronos::now();
$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
{
// Localhost addresses do not need to be anonymized
if (! $anonymize || $address === null || $address === IpAddress::LOCALHOST) {
return $address;
}
try {
2020-05-09 10:34:59 +03:00
return (string) IpAddress::fromString($address)->getAnonymizedCopy();
} catch (InvalidArgumentException $e) {
return null;
}
}
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
{
return new self($shortUrl, $visitor, $anonymize);
}
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
{
return new self(null, $visitor, $anonymize, null, self::TYPE_BASE_URL);
}
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
{
return new self(null, $visitor, $anonymize, null, self::TYPE_INVALID_SHORT_URL);
}
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
{
return new self(null, $visitor, $anonymize, null, self::TYPE_REGULAR_404);
}
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(): ?VisitLocationInterface
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;
}
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,
];
}
2018-11-17 21:23:25 +03:00
/**
* @internal
*/
public function getDate(): Chronos
{
return $this->date;
}
2016-04-17 11:46:35 +03:00
}