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\UnknownVisitLocation;
|
|
|
|
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
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
private string $referer;
|
|
|
|
private Chronos $date;
|
|
|
|
private ?string $remoteAddr;
|
|
|
|
private string $userAgent;
|
|
|
|
private ShortUrl $shortUrl;
|
|
|
|
private ?VisitLocation $visitLocation = null;
|
2016-04-17 11:46:35 +03:00
|
|
|
|
2018-10-28 18:20:10 +03:00
|
|
|
public function __construct(ShortUrl $shortUrl, Visitor $visitor, ?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();
|
|
|
|
$this->remoteAddr = $this->obfuscateAddress($visitor->getRemoteAddress());
|
2018-10-18 20:19:42 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
private function obfuscateAddress(?string $address): ?string
|
|
|
|
{
|
2018-09-14 00:50:09 +03:00
|
|
|
// Localhost addresses do not need to be obfuscated
|
|
|
|
if ($address === null || $address === IpAddress::LOCALHOST) {
|
|
|
|
return $address;
|
2018-09-13 23:36:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (string) IpAddress::fromString($address)->getObfuscatedCopy();
|
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
|
|
|
|
2019-12-27 19:07:20 +03:00
|
|
|
public function getShortUrl(): ShortUrl
|
|
|
|
{
|
|
|
|
return $this->shortUrl;
|
|
|
|
}
|
|
|
|
|
2018-12-08 14:12:11 +03:00
|
|
|
public function getVisitLocation(): VisitLocationInterface
|
2016-07-20 10:35:46 +03:00
|
|
|
{
|
2018-12-08 14:12:11 +03:00
|
|
|
return $this->visitLocation ?? new UnknownVisitLocation();
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-12 22:51:06 +03:00
|
|
|
/**
|
|
|
|
* 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>,
|
2016-06-12 22:51:06 +03:00
|
|
|
* 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
|
2016-06-12 22:51:06 +03:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'referer' => $this->referer,
|
2019-02-17 22:28:34 +03:00
|
|
|
'date' => $this->date !== null ? $this->date->toAtomString() : null,
|
2016-06-12 22:51:06 +03:00
|
|
|
'userAgent' => $this->userAgent,
|
2016-07-20 10:35:46 +03:00
|
|
|
'visitLocation' => $this->visitLocation,
|
2018-09-14 20:04:40 +03:00
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
'remoteAddr' => null,
|
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
|
|
|
}
|