Completed
Push — master ( c32e20...5b9784 )
by Alejandro
15s
created

Visit   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 70.45%

Importance

Changes 0
Metric Value
dl 0
loc 140
rs 10
c 0
b 0
f 0
ccs 31
cts 44
cp 0.7045
wmc 19
lcom 2
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getReferer() 0 4 1
A setReferer() 0 5 1
A getDate() 0 4 1
A setDate() 0 5 1
A getShortUrl() 0 4 1
A setShortUrl() 0 5 1
A getRemoteAddr() 0 4 1
A setRemoteAddr() 0 5 1
A obfuscateAddress() 0 13 4
A getUserAgent() 0 4 1
A setUserAgent() 0 5 1
A getVisitLocation() 0 4 1
A setVisitLocation() 0 5 1
A jsonSerialize() 0 12 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|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
    private $visitLocation;
53
54 8
    public function __construct()
55
    {
56 8
        $this->date = new \DateTime();
57 8
    }
58
59
    public function getReferer(): string
60
    {
61
        return $this->referer;
62
    }
63
64 3
    public function setReferer(string $referer): self
65
    {
66 3
        $this->referer = $referer;
67 3
        return $this;
68
    }
69
70
    public function getDate(): \DateTime
71
    {
72
        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 2
    public function setShortUrl(ShortUrl $shortUrl): self
87
    {
88 2
        $this->shortUrl = $shortUrl;
89 2
        return $this;
90
    }
91
92 4
    public function getRemoteAddr(): ?string
93
    {
94 4
        return $this->remoteAddr;
95
    }
96
97 5
    public function setRemoteAddr(?string $remoteAddr): self
98
    {
99 5
        $this->remoteAddr = $this->obfuscateAddress($remoteAddr);
100 5
        return $this;
101
    }
102
103 5
    private function obfuscateAddress(?string $address): ?string
104
    {
105
        // Localhost addresses do not need to be obfuscated
106 5
        if ($address === null || $address === IpAddress::LOCALHOST) {
107 2
            return $address;
108
        }
109
110
        try {
111 4
            return (string) IpAddress::fromString($address)->getObfuscatedCopy();
112
        } catch (WrongIpException $e) {
113
            return null;
114
        }
115
    }
116
117
    public function getUserAgent(): string
118
    {
119
        return $this->userAgent;
120
    }
121
122 3
    public function setUserAgent(string $userAgent): self
123
    {
124 3
        $this->userAgent = $userAgent;
125 3
        return $this;
126
    }
127
128 1
    public function getVisitLocation(): VisitLocation
129
    {
130 1
        return $this->visitLocation;
131
    }
132
133 4
    public function setVisitLocation(VisitLocation $visitLocation): self
134
    {
135 4
        $this->visitLocation = $visitLocation;
136 4
        return $this;
137
    }
138
139
    /**
140
     * Specify data which should be serialized to JSON
141
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
142
     * @return array data which can be serialized by <b>json_encode</b>,
143
     * which is a value of any type other than a resource.
144
     * @since 5.4.0
145
     */
146 1
    public function jsonSerialize(): array
147
    {
148
        return [
149 1
            'referer' => $this->referer,
150 1
            'date' => isset($this->date) ? $this->date->format(\DateTime::ATOM) : null,
151 1
            'userAgent' => $this->userAgent,
152 1
            'visitLocation' => $this->visitLocation,
153
154
            // Deprecated
155
            'remoteAddr' => null,
156
        ];
157
    }
158
}
159