|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Core\Entity; |
|
5
|
|
|
|
|
6
|
|
|
use Cake\Chronos\Chronos; |
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
8
|
|
|
use JsonSerializable; |
|
9
|
|
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity; |
|
10
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Visit |
|
17
|
|
|
* @author |
|
18
|
|
|
* @link |
|
19
|
|
|
* |
|
20
|
|
|
* @ORM\Entity(repositoryClass=VisitRepository::class) |
|
21
|
|
|
* @ORM\Table(name="visits") |
|
22
|
|
|
*/ |
|
23
|
|
|
class Visit extends AbstractEntity implements JsonSerializable |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
* @ORM\Column(type="string", length=256, nullable=true) |
|
28
|
|
|
*/ |
|
29
|
|
|
private $referer; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Chronos |
|
32
|
|
|
* @ORM\Column(type="chronos_datetime", nullable=false) |
|
33
|
|
|
*/ |
|
34
|
|
|
private $date; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string|null |
|
37
|
|
|
* @ORM\Column(type="string", length=256, name="remote_addr", nullable=true) |
|
38
|
|
|
*/ |
|
39
|
|
|
private $remoteAddr; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
* @ORM\Column(type="string", length=256, name="user_agent", nullable=true) |
|
43
|
|
|
*/ |
|
44
|
|
|
private $userAgent; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var ShortUrl |
|
47
|
|
|
* @ORM\ManyToOne(targetEntity=ShortUrl::class) |
|
48
|
|
|
* @ORM\JoinColumn(name="short_url_id", referencedColumnName="id") |
|
49
|
|
|
*/ |
|
50
|
|
|
private $shortUrl; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var VisitLocation |
|
53
|
|
|
* @ORM\ManyToOne(targetEntity=VisitLocation::class, cascade={"persist"}) |
|
54
|
|
|
* @ORM\JoinColumn(name="visit_location_id", referencedColumnName="id", nullable=true) |
|
55
|
|
|
*/ |
|
56
|
|
|
private $visitLocation; |
|
57
|
|
|
|
|
58
|
12 |
|
public function __construct(ShortUrl $shortUrl, Visitor $visitor, ?Chronos $date = null) |
|
59
|
|
|
{ |
|
60
|
12 |
|
$this->shortUrl = $shortUrl; |
|
61
|
12 |
|
$this->date = $date ?? Chronos::now(); |
|
62
|
12 |
|
$this->userAgent = $visitor->getUserAgent(); |
|
63
|
12 |
|
$this->referer = $visitor->getReferer(); |
|
64
|
12 |
|
$this->remoteAddr = $this->obfuscateAddress($visitor->getRemoteAddress()); |
|
65
|
12 |
|
} |
|
66
|
|
|
|
|
67
|
12 |
|
private function obfuscateAddress(?string $address): ?string |
|
68
|
|
|
{ |
|
69
|
|
|
// Localhost addresses do not need to be obfuscated |
|
70
|
12 |
|
if ($address === null || $address === IpAddress::LOCALHOST) { |
|
71
|
8 |
|
return $address; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
5 |
|
return (string) IpAddress::fromString($address)->getObfuscatedCopy(); |
|
76
|
2 |
|
} catch (WrongIpException $e) { |
|
77
|
2 |
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
public function getRemoteAddr(): ?string |
|
82
|
|
|
{ |
|
83
|
4 |
|
return $this->remoteAddr; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
public function hasRemoteAddr(): bool |
|
87
|
|
|
{ |
|
88
|
3 |
|
return ! empty($this->remoteAddr); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function getVisitLocation(): VisitLocation |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->visitLocation; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function setVisitLocation(VisitLocation $visitLocation): self |
|
97
|
|
|
{ |
|
98
|
4 |
|
$this->visitLocation = $visitLocation; |
|
99
|
4 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Specify data which should be serialized to JSON |
|
104
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
105
|
|
|
* @return array data which can be serialized by <b>json_encode</b>, |
|
106
|
|
|
* which is a value of any type other than a resource. |
|
107
|
|
|
* @since 5.4.0 |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function jsonSerialize(): array |
|
110
|
|
|
{ |
|
111
|
|
|
return [ |
|
112
|
1 |
|
'referer' => $this->referer, |
|
113
|
1 |
|
'date' => isset($this->date) ? $this->date->toAtomString() : null, |
|
114
|
1 |
|
'userAgent' => $this->userAgent, |
|
115
|
1 |
|
'visitLocation' => $this->visitLocation, |
|
116
|
|
|
|
|
117
|
|
|
// Deprecated |
|
118
|
|
|
'remoteAddr' => null, |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|