Status::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 3
dl 0
loc 16
ccs 0
cts 14
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace GroundSix\Locker;
5
6
class Status
7
{
8
    /** @var string $url */
9
    private $url;
10
11
    /** @var \DateTimeImmutable|null $lockedUntil */
12
    private $lockedUntil;
13
14
    /** @var string|null $lockedBy */
15
    private $lockedBy;
16
17
    /**
18
     * Creates a new Status instance
19
     *
20
     * @param string $url The Identifier of the page to lock. Either the path or some other unique identifier
21
     * @param \DateTimeInterface|null $lockedUntil A DateTimeInterface that holds the date for how long the page should
22
     * be locked for
23
     * @param null|string $lockedBy A unique identifier to show who locked the page.
24
     */
25
    public function __construct(string $url, ?\DateTimeInterface $lockedUntil = null, ?string $lockedBy = null)
26
    {
27
        $this->url = $url;
28
29
        if ($lockedUntil === null) {
30
            $this->lockedUntil = $lockedUntil;
31
        } else {
32
            $lockedTime = \DateTimeImmutable::createFromFormat(
33
                DATE_ATOM,
34
                $lockedUntil->format(DATE_ATOM),
35
                new \DateTimeZone('UTC')
36
            );
37
            $this->lockedUntil = $lockedTime;
38
        }
39
40
        $this->lockedBy = $lockedBy;
41
    }
42
43
    /**
44
     * Is the page currently locked? True if yes, otherwise false
45
     *
46
     * @return bool
47
     */
48
    public function isLocked(): bool
49
    {
50
        if ($this->lockedUntil === null) {
51
            return false;
52
        }
53
54
        $lockedUntil = $this->lockedUntil->format('U');
55
        $now = (new \DateTime('now', new \DateTimeZone('UTC')))->format('U');
56
57
        return $lockedUntil >$now;
58
    }
59
60
    /**
61
     * Returns the pages unique identifier
62
     *
63
     * @return string
64
     */
65
    public function url(): string
66
    {
67
        return $this->url;
68
    }
69
70
    /**
71
     * Returns the how long the page is locked for as a DateTimeImmutable
72
     *
73
     * @return \DateTimeImmutable|null
74
     */
75
    public function lockedUntil(): ?\DateTimeImmutable
76
    {
77
        return $this->lockedUntil;
78
    }
79
80
    /**
81
     * Returns the unique identifier for the person who locked the page
82
     *
83
     * @return null|string
84
     */
85
    public function lockedBy(): ?string
86
    {
87
        return $this->lockedBy;
88
    }
89
}
90