Completed
Pull Request — master (#195)
by Alejandro
07:04
created

Visit::getDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Entity;
5
6
use Doctrine\ORM\Mapping as ORM;
7
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
8
use Shlinkio\Shlink\Common\Exception\WrongIpException;
9
use Shlinkio\Shlink\Common\Util\IpAddress;
10
11
/**
12
 * Class Visit
13
 * @author
14
 * @link
15
 *
16
 * @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\VisitRepository")
17
 * @ORM\Table(name="visits")
18
 */
19
class Visit extends AbstractEntity implements \JsonSerializable
20
{
21
    /**
22
     * @var string
23
     * @ORM\Column(type="string", length=256, nullable=true)
24
     */
25
    private $referer;
26
    /**
27
     * @var \DateTime
28
     * @ORM\Column(type="datetime", nullable=false)
29
     */
30
    private $date;
31
    /**
32
     * @var string|null
33
     * @ORM\Column(type="string", length=256, name="remote_addr", nullable=true)
34
     */
35
    private $remoteAddr;
36
    /**
37
     * @var string
38
     * @ORM\Column(type="string", length=256, name="user_agent", nullable=true)
39
     */
40
    private $userAgent;
41
    /**
42
     * @var ShortUrl
43
     * @ORM\ManyToOne(targetEntity=ShortUrl::class)
44
     * @ORM\JoinColumn(name="short_url_id", referencedColumnName="id")
45
     */
46
    private $shortUrl;
47
    /**
48
     * @var VisitLocation
49
     * @ORM\ManyToOne(targetEntity=VisitLocation::class, cascade={"persist"})
50
     * @ORM\JoinColumn(name="visit_location_id", referencedColumnName="id", nullable=true)
51
     */
52 8
    private $visitLocation;
53
54 8
    public function __construct()
55 8
    {
56
        $this->date = new \DateTime();
57
    }
58
59
    public function getReferer(): string
60
    {
61
        return $this->referer;
62
    }
63
64
    public function setReferer(string $referer): self
65
    {
66
        $this->referer = $referer;
67
        return $this;
68
    }
69 3
70
    public function getDate(): \DateTime
71 3
    {
72 3
        return $this->date;
73
    }
74
75
    public function setDate(\DateTime $date): self
76
    {
77
        $this->date = $date;
78
        return $this;
79
    }
80
81
    public function getShortUrl(): ShortUrl
82
    {
83
        return $this->shortUrl;
84
    }
85
86
    public function setShortUrl(ShortUrl $shortUrl): self
87
    {
88
        $this->shortUrl = $shortUrl;
89
        return $this;
90
    }
91
92
    public function getRemoteAddr(): ?string
93
    {
94
        return $this->remoteAddr;
95
    }
96
97
    public function setRemoteAddr(?string $remoteAddr): self
98
    {
99
        $this->remoteAddr = $this->obfuscateAddress($remoteAddr);
100
        return $this;
101
    }
102
103
    private function obfuscateAddress(?string $address): ?string
104
    {
105 2
        // Localhost addresses do not need to be obfuscated
106
        if ($address === null || $address === IpAddress::LOCALHOST) {
107 2
            return $address;
108 2
        }
109
110
        try {
111
            return (string) IpAddress::fromString($address)->getObfuscatedCopy();
112
        } catch (WrongIpException $e) {
113
            return null;
114 4
        }
115
    }
116 4
117
    public function getUserAgent(): string
118
    {
119
        return $this->userAgent;
120
    }
121
122
    public function setUserAgent(string $userAgent): self
123 6
    {
124
        $this->userAgent = $userAgent;
125 6
        return $this;
126 6
    }
127
128
    public function getVisitLocation(): VisitLocation
129
    {
130
        return $this->visitLocation;
131
    }
132
133
    public function setVisitLocation(VisitLocation $visitLocation): self
134
    {
135
        $this->visitLocation = $visitLocation;
136
        return $this;
137
    }
138
139
    /**
140
     * Specify data which should be serialized to JSON
141 3
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
142
     * @return array data which can be serialized by <b>json_encode</b>,
143 3
     * which is a value of any type other than a resource.
144 3
     * @since 5.4.0
145
     */
146
    public function jsonSerialize(): array
147
    {
148
        return [
149
            'referer' => $this->referer,
150
            'date' => isset($this->date) ? $this->date->format(\DateTime::ATOM) : null,
151
            'userAgent' => $this->userAgent,
152
            'visitLocation' => $this->visitLocation,
153
154
            // Deprecated
155
            'remoteAddr' => null,
156
        ];
157
    }
158
}
159