Completed
Push — develop ( 34d8b3...fd6151 )
by Alejandro
22s queued 14s
created

Visit::locate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Entity;
6
7
use Cake\Chronos\Chronos;
8
use JsonSerializable;
9
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
10
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
11
use Shlinkio\Shlink\Common\Util\IpAddress;
12
use Shlinkio\Shlink\Core\Model\Visitor;
13
use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation;
14
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
15
16
class Visit extends AbstractEntity implements JsonSerializable
17
{
18
    /** @var string */
19
    private $referer;
20
    /** @var Chronos */
21
    private $date;
22
    /** @var string|null */
23
    private $remoteAddr;
24
    /** @var string */
25
    private $userAgent;
26
    /** @var ShortUrl */
27
    private $shortUrl;
28
    /** @var VisitLocation */
29
    private $visitLocation;
30
31 23
    public function __construct(ShortUrl $shortUrl, Visitor $visitor, ?Chronos $date = null)
32
    {
33 23
        $this->shortUrl = $shortUrl;
34 23
        $this->date = $date ?? Chronos::now();
35 23
        $this->userAgent = $visitor->getUserAgent();
36 23
        $this->referer = $visitor->getReferer();
37 23
        $this->remoteAddr = $this->obfuscateAddress($visitor->getRemoteAddress());
38
    }
39
40 23
    private function obfuscateAddress(?string $address): ?string
41
    {
42
        // Localhost addresses do not need to be obfuscated
43 23
        if ($address === null || $address === IpAddress::LOCALHOST) {
44 12
            return $address;
45
        }
46
47
        try {
48 11
            return (string) IpAddress::fromString($address)->getObfuscatedCopy();
49 2
        } catch (InvalidArgumentException $e) {
50 2
            return null;
51
        }
52
    }
53
54 7
    public function getRemoteAddr(): ?string
55
    {
56 7
        return $this->remoteAddr;
57
    }
58
59 11
    public function hasRemoteAddr(): bool
60
    {
61 11
        return ! empty($this->remoteAddr);
62
    }
63
64 1
    public function getShortUrl(): ShortUrl
65
    {
66 1
        return $this->shortUrl;
67
    }
68
69 7
    public function getVisitLocation(): VisitLocationInterface
70
    {
71 7
        return $this->visitLocation ?? new UnknownVisitLocation();
72
    }
73
74 6
    public function isLocatable(): bool
75
    {
76 6
        return $this->hasRemoteAddr() && $this->remoteAddr !== IpAddress::LOCALHOST;
77
    }
78
79 8
    public function locate(VisitLocation $visitLocation): self
80
    {
81 8
        $this->visitLocation = $visitLocation;
82 8
        return $this;
83
    }
84
85
    /**
86
     * Specify data which should be serialized to JSON
87
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
88
     * @return array data which can be serialized by <b>json_encode</b>,
89
     * which is a value of any type other than a resource.
90
     * @since 5.4.0
91
     */
92 4
    public function jsonSerialize(): array
93
    {
94
        return [
95 4
            'referer' => $this->referer,
96 4
            'date' => $this->date !== null ? $this->date->toAtomString() : null,
97 4
            'userAgent' => $this->userAgent,
98 4
            'visitLocation' => $this->visitLocation,
99
100
            // Deprecated
101
            'remoteAddr' => null,
102
        ];
103
    }
104
105
    /**
106
     * @internal
107
     */
108 1
    public function getDate(): Chronos
109
    {
110 1
        return $this->date;
111
    }
112
}
113