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=512, 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
|
15 |
|
public function __construct(ShortUrl $shortUrl, Visitor $visitor, ?Chronos $date = null) |
59
|
|
|
{ |
60
|
15 |
|
$this->shortUrl = $shortUrl; |
61
|
15 |
|
$this->date = $date ?? Chronos::now(); |
62
|
15 |
|
$this->userAgent = $visitor->getUserAgent(); |
63
|
15 |
|
$this->referer = $visitor->getReferer(); |
64
|
15 |
|
$this->remoteAddr = $this->obfuscateAddress($visitor->getRemoteAddress()); |
65
|
|
|
} |
66
|
|
|
|
67
|
15 |
|
private function obfuscateAddress(?string $address): ?string |
68
|
|
|
{ |
69
|
|
|
// Localhost addresses do not need to be obfuscated |
70
|
15 |
|
if ($address === null || $address === IpAddress::LOCALHOST) { |
71
|
10 |
|
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
|
5 |
|
public function hasRemoteAddr(): bool |
87
|
|
|
{ |
88
|
5 |
|
return ! empty($this->remoteAddr); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function getVisitLocation(): VisitLocation |
92
|
|
|
{ |
93
|
1 |
|
return $this->visitLocation; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
public function locate(VisitLocation $visitLocation): self |
97
|
|
|
{ |
98
|
2 |
|
$this->visitLocation = $visitLocation; |
99
|
2 |
|
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
|
|
|
|