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

103 lines
2.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
{
private string $referer;
2019-12-30 00:27:00 +03:00
private Chronos $date;
private ?string $remoteAddr = null;
private string $userAgent;
2019-12-30 00:27:00 +03:00
private ShortUrl $shortUrl;
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)
2016-04-17 11:46:35 +03:00
{
$this->shortUrl = $shortUrl;
$this->date = $date ?? Chronos::now();
$this->userAgent = $visitor->getUserAgent();
$this->referer = $visitor->getReferer();
$this->remoteAddr = $this->processAddress($anonymize, $visitor->getRemoteAddress());
}
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 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;
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
2018-03-28 00:56:55 +03:00
* @return array data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
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
}