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 Doctrine\ORM\Mapping as ORM;
|
2016-07-19 19:01:39 +03:00
|
|
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
2018-09-13 23:36:28 +03:00
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2016-04-17 11:46:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Visit
|
|
|
|
* @author
|
|
|
|
* @link
|
|
|
|
*
|
2016-07-20 20:00:23 +03:00
|
|
|
* @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\VisitRepository")
|
2016-04-17 11:46:35 +03:00
|
|
|
* @ORM\Table(name="visits")
|
|
|
|
*/
|
2016-06-12 22:51:06 +03:00
|
|
|
class Visit extends AbstractEntity implements \JsonSerializable
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
2016-04-30 20:18:42 +03:00
|
|
|
* @ORM\Column(type="string", length=256, nullable=true)
|
2016-04-17 11:46:35 +03:00
|
|
|
*/
|
2018-09-13 23:46:28 +03:00
|
|
|
private $referer;
|
2016-04-17 11:46:35 +03:00
|
|
|
/**
|
|
|
|
* @var \DateTime
|
|
|
|
* @ORM\Column(type="datetime", nullable=false)
|
|
|
|
*/
|
2018-09-13 23:46:28 +03:00
|
|
|
private $date;
|
2016-04-17 11:46:35 +03:00
|
|
|
/**
|
2018-09-14 00:52:22 +03:00
|
|
|
* @var string|null
|
2016-04-30 20:18:42 +03:00
|
|
|
* @ORM\Column(type="string", length=256, name="remote_addr", nullable=true)
|
2016-04-17 11:46:35 +03:00
|
|
|
*/
|
2018-09-13 23:46:28 +03:00
|
|
|
private $remoteAddr;
|
|
|
|
/**
|
2018-09-14 00:52:22 +03:00
|
|
|
* @var string|null
|
2018-09-14 00:50:09 +03:00
|
|
|
* @ORM\Column(type="string", length=256, name="remote_addr_hash", nullable=true)
|
2018-09-13 23:46:28 +03:00
|
|
|
*/
|
|
|
|
private $remoteAddrHash;
|
2016-04-17 11:46:35 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
2016-04-30 20:18:42 +03:00
|
|
|
* @ORM\Column(type="string", length=256, name="user_agent", nullable=true)
|
2016-04-17 11:46:35 +03:00
|
|
|
*/
|
2018-09-13 23:46:28 +03:00
|
|
|
private $userAgent;
|
2016-04-17 11:46:35 +03:00
|
|
|
/**
|
|
|
|
* @var ShortUrl
|
|
|
|
* @ORM\ManyToOne(targetEntity=ShortUrl::class)
|
|
|
|
* @ORM\JoinColumn(name="short_url_id", referencedColumnName="id")
|
|
|
|
*/
|
2018-09-13 23:46:28 +03:00
|
|
|
private $shortUrl;
|
2016-07-20 10:35:46 +03:00
|
|
|
/**
|
|
|
|
* @var VisitLocation
|
2016-07-20 20:00:23 +03:00
|
|
|
* @ORM\ManyToOne(targetEntity=VisitLocation::class, cascade={"persist"})
|
2016-07-20 10:35:46 +03:00
|
|
|
* @ORM\JoinColumn(name="visit_location_id", referencedColumnName="id", nullable=true)
|
|
|
|
*/
|
2018-09-13 23:46:28 +03:00
|
|
|
private $visitLocation;
|
2016-04-17 11:46:35 +03:00
|
|
|
|
2016-04-30 20:18:42 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->date = new \DateTime();
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:56:55 +03:00
|
|
|
public function getReferer(): string
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
|
|
|
return $this->referer;
|
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
public function setReferer(string $referer): self
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
|
|
|
$this->referer = $referer;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:56:55 +03:00
|
|
|
public function getDate(): \DateTime
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
|
|
|
return $this->date;
|
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
public function setDate(\DateTime $date): self
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
|
|
|
$this->date = $date;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:56:55 +03:00
|
|
|
public function getShortUrl(): ShortUrl
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
return $this->shortUrl;
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
public function setShortUrl(ShortUrl $shortUrl): self
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
$this->shortUrl = $shortUrl;
|
2016-04-17 11:46:35 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:56:55 +03:00
|
|
|
public function getRemoteAddr(): string
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
return $this->remoteAddr;
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
public function setRemoteAddr(?string $remoteAddr): self
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2018-09-13 23:36:28 +03:00
|
|
|
$this->remoteAddr = $this->obfuscateAddress($remoteAddr);
|
2018-09-13 23:46:28 +03:00
|
|
|
$this->remoteAddrHash = $this->hashAddress($remoteAddr);
|
|
|
|
|
2016-04-17 11:46:35 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
} catch (WrongIpException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 23:46:28 +03:00
|
|
|
private function hashAddress(?string $address): ?string
|
|
|
|
{
|
|
|
|
return $address ? \hash('sha256', $address) : null;
|
|
|
|
}
|
|
|
|
|
2018-09-14 00:50:09 +03:00
|
|
|
public function resetObfuscatedAddr(): self
|
|
|
|
{
|
|
|
|
$this->remoteAddr = null;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:56:55 +03:00
|
|
|
public function getUserAgent(): string
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
return $this->userAgent;
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
public function setUserAgent(string $userAgent): self
|
2016-04-17 11:46:35 +03:00
|
|
|
{
|
2016-04-30 20:18:42 +03:00
|
|
|
$this->userAgent = $userAgent;
|
2016-04-17 11:46:35 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2016-06-12 22:51:06 +03:00
|
|
|
|
2018-03-28 00:56:55 +03:00
|
|
|
public function getVisitLocation(): VisitLocation
|
2016-07-20 10:35:46 +03:00
|
|
|
{
|
|
|
|
return $this->visitLocation;
|
|
|
|
}
|
|
|
|
|
2018-09-13 23:36:28 +03:00
|
|
|
public function setVisitLocation(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,
|
2017-04-16 11:27:27 +03:00
|
|
|
'date' => isset($this->date) ? $this->date->format(\DateTime::ATOM) : null,
|
2016-06-12 22:51:06 +03:00
|
|
|
'remoteAddr' => $this->remoteAddr,
|
|
|
|
'userAgent' => $this->userAgent,
|
2016-07-20 10:35:46 +03:00
|
|
|
'visitLocation' => $this->visitLocation,
|
2016-06-12 22:51:06 +03:00
|
|
|
];
|
|
|
|
}
|
2016-04-17 11:46:35 +03:00
|
|
|
}
|