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;
|
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';
|
|
|
|
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;
|
2020-01-07 00:31:00 +03:00
|
|
|
private ?string $remoteAddr = null;
|
2021-02-07 13:26:01 +03:00
|
|
|
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
|
|
|
|
2020-05-08 16:43:09 +03:00
|
|
|
public function __construct(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true, ?Chronos $date = null)
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
$this->shortUrl = $shortUrl;
|
2018-10-28 18:20:10 +03:00
|
|
|
$this->date = $date ?? Chronos::now();
|
|
|
|
$this->userAgent = $visitor->getUserAgent();
|
|
|
|
$this->referer = $visitor->getReferer();
|
2020-05-08 16:43:09 +03:00
|
|
|
$this->remoteAddr = $this->processAddress($anonymize, $visitor->getRemoteAddress());
|
2021-02-07 13:26:01 +03:00
|
|
|
$this->type = self::TYPE_VALID_SHORT_URL;
|
2018-10-18 20:19:42 +03:00
|
|
|
}
|
|
|
|
|
2020-05-08 16:43:09 +03:00
|
|
|
private function processAddress(bool $anonymize, ?string $address): ?string
|
2018-09-13 23:36:28 +03:00
|
|
|
{
|
2020-05-08 16:43:09 +03:00
|
|
|
// Localhost addresses do not need to be anonymized
|
|
|
|
if (! $anonymize || $address === null || $address === IpAddress::LOCALHOST) {
|
2018-09-14 00:50:09 +03:00
|
|
|
return $address;
|
2018-09-13 23:36:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-05-09 10:34:59 +03:00
|
|
|
return (string) IpAddress::fromString($address)->getAnonymizedCopy();
|
2019-08-11 15:29:22 +03:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2018-09-13 23:36:28 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|