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

102 lines
2.8 KiB
PHP
Raw Normal View History

2016-04-17 11:46:35 +03:00
<?php
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\WrongIpException;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation;
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
{
/** @var string */
private $referer;
/** @var Chronos */
private $date;
/** @var string|null */
private $remoteAddr;
/** @var string */
private $userAgent;
/** @var ShortUrl */
private $shortUrl;
/** @var VisitLocation */
private $visitLocation;
2016-04-17 11:46:35 +03:00
public function __construct(ShortUrl $shortUrl, Visitor $visitor, ?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->obfuscateAddress($visitor->getRemoteAddress());
}
private function obfuscateAddress(?string $address): ?string
{
// Localhost addresses do not need to be obfuscated
if ($address === null || $address === IpAddress::LOCALHOST) {
return $address;
}
try {
return (string) IpAddress::fromString($address)->getObfuscatedCopy();
} catch (WrongIpException $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 getVisitLocation(): VisitLocationInterface
2016-07-20 10:35:46 +03:00
{
return $this->visitLocation ?? new UnknownVisitLocation();
2016-07-20 10:35:46 +03:00
}
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,
'date' => isset($this->date) ? $this->date->toAtomString() : null,
'userAgent' => $this->userAgent,
2016-07-20 10:35:46 +03:00
'visitLocation' => $this->visitLocation,
// Deprecated
'remoteAddr' => null,
];
}
2018-11-17 21:23:25 +03:00
/**
* @internal
*/
public function getDate(): Chronos
{
return $this->date;
}
2016-04-17 11:46:35 +03:00
}