Completed
Push — master ( 1de050...162d05 )
by Alejandro
46:23 queued 09:21
created

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