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

Visit::getShortUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
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
33
     * @ORM\Column(type="string", length=256, name="remote_addr", nullable=true)
34
     */
35
    private $remoteAddr;
36
    /**
37
     * @var string
38
     */
39
    private $remoteAddrHash;
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 8
    public function __construct()
59
    {
60 8
        $this->date = new \DateTime();
61 8
    }
62
63
    public function getReferer(): string
64
    {
65
        return $this->referer;
66
    }
67
68 3
    public function setReferer(string $referer): self
69
    {
70 3
        $this->referer = $referer;
71 3
        return $this;
72
    }
73
74
    public function getDate(): \DateTime
75
    {
76
        return $this->date;
77
    }
78
79
    public function setDate(\DateTime $date): self
80
    {
81
        $this->date = $date;
82
        return $this;
83
    }
84
85
    public function getShortUrl(): ShortUrl
86
    {
87
        return $this->shortUrl;
88
    }
89
90 2
    public function setShortUrl(ShortUrl $shortUrl): self
91
    {
92 2
        $this->shortUrl = $shortUrl;
93 2
        return $this;
94
    }
95
96 4
    public function getRemoteAddr(): string
97
    {
98 4
        return $this->remoteAddr;
99
    }
100
101 6
    public function setRemoteAddr(?string $remoteAddr): self
102
    {
103 6
        $this->remoteAddr = $this->obfuscateAddress($remoteAddr);
104 6
        $this->remoteAddrHash = $this->hashAddress($remoteAddr);
105
106 6
        return $this;
107
    }
108
109 6
    private function obfuscateAddress(?string $address): ?string
110
    {
111 6
        if ($address === null) {
112 1
            return null;
113
        }
114
115
        try {
116 5
            return (string) IpAddress::fromString($address)->getObfuscatedCopy();
117
        } catch (WrongIpException $e) {
118
            return null;
119
        }
120
    }
121
122 6
    private function hashAddress(?string $address): ?string
123
    {
124 6
        return $address ? \hash('sha256', $address) : null;
125
    }
126
127
    public function getUserAgent(): string
128
    {
129
        return $this->userAgent;
130
    }
131
132 3
    public function setUserAgent(string $userAgent): self
133
    {
134 3
        $this->userAgent = $userAgent;
135 3
        return $this;
136
    }
137
138
    public function getVisitLocation(): VisitLocation
139
    {
140
        return $this->visitLocation;
141
    }
142
143 3
    public function setVisitLocation(VisitLocation $visitLocation): self
144
    {
145 3
        $this->visitLocation = $visitLocation;
146 3
        return $this;
147
    }
148
149
    /**
150
     * Specify data which should be serialized to JSON
151
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
152
     * @return array data which can be serialized by <b>json_encode</b>,
153
     * which is a value of any type other than a resource.
154
     * @since 5.4.0
155
     */
156 1
    public function jsonSerialize(): array
157
    {
158
        return [
159 1
            'referer' => $this->referer,
160 1
            'date' => isset($this->date) ? $this->date->format(\DateTime::ATOM) : null,
161 1
            'remoteAddr' => $this->remoteAddr,
162 1
            'userAgent' => $this->userAgent,
163 1
            'visitLocation' => $this->visitLocation,
164
        ];
165
    }
166
}
167