Completed
Pull Request — master (#195)
by Alejandro
10:29
created

Visit::obfuscateAddress()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.5923

Importance

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