EncryptedTime   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 54
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A __construct() 0 12 2
A isFuture() 0 4 1
A __toString() 0 4 1
A isValidTimeStamp() 0 16 4
1
<?php
2
3
namespace Spatie\Honeypot;
4
5
use DateTimeInterface;
6
use Illuminate\Support\Facades\Date;
7
8
class EncryptedTime
9
{
10
    protected $carbon;
11
12
    /** @var string */
13
    protected $encryptedTime;
14
15
    public static function create(DateTimeInterface $dateTime)
16
    {
17
        $encryptedTime = app('encrypter')->encrypt($dateTime->getTimestamp());
18
19
        return new static($encryptedTime);
20
    }
21
22
    public function __construct(string $encryptedTime)
23
    {
24
        $this->encryptedTime = $encryptedTime;
25
26
        $timestamp = app('encrypter')->decrypt($encryptedTime);
27
28
        if (! $this->isValidTimeStamp($timestamp)) {
29
            throw new \Exception(sprintf('Timestamp %s is invalid', $timestamp));
30
        }
31
32
        $this->carbon = Date::createFromTimestamp($timestamp);
33
    }
34
35
    public function isFuture(): bool
36
    {
37
        return $this->carbon->isFuture();
38
    }
39
40
    public function __toString()
41
    {
42
        return $this->encryptedTime;
43
    }
44
45
    private function isValidTimeStamp(string $timestamp): bool
46
    {
47
        if ((string) (int) $timestamp !== $timestamp) {
48
            return false;
49
        }
50
51
        if ($timestamp <= 0) {
52
            return false;
53
        }
54
55
        if ($timestamp >= PHP_INT_MAX) {
56
            return false;
57
        }
58
59
        return true;
60
    }
61
}
62