ApiKey   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 14
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isExpired() 0 3 2
A disable() 0 4 1
A isValid() 0 3 2
A __toString() 0 3 1
A __construct() 0 5 1
A getExpirationDate() 0 3 1
A isEnabled() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Rest\Entity;
6
7
use Cake\Chronos\Chronos;
8
use Ramsey\Uuid\Uuid;
9
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
10
11
class ApiKey extends AbstractEntity
12
{
13
    private string $key;
14
    private ?Chronos $expirationDate = null;
15
    private bool $enabled;
16
17 10
    public function __construct(?Chronos $expirationDate = null)
18
    {
19 10
        $this->key = Uuid::uuid4()->toString();
20 10
        $this->expirationDate = $expirationDate;
21 10
        $this->enabled = true;
22 10
    }
23
24 4
    public function getExpirationDate(): ?Chronos
25
    {
26 4
        return $this->expirationDate;
27
    }
28
29 5
    public function isExpired(): bool
30
    {
31 5
        return $this->expirationDate !== null && $this->expirationDate->lt(Chronos::now());
32
    }
33
34 7
    public function isEnabled(): bool
35
    {
36 7
        return $this->enabled;
37
    }
38
39 2
    public function disable(): self
40
    {
41 2
        $this->enabled = false;
42 2
        return $this;
43
    }
44
45
    /**
46
     * Tells if this api key is enabled and not expired
47
     */
48 4
    public function isValid(): bool
49
    {
50 4
        return $this->isEnabled() && ! $this->isExpired();
51
    }
52
53 4
    public function __toString(): string
54
    {
55 4
        return $this->key;
56
    }
57
}
58