ArrayStore::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
namespace CheatCodes\GuzzleHsts;
3
4
use DateInterval;
5
use DateTime;
6
7
class ArrayStore implements StoreInterface
8
{
9
    /**
10
     * List of the known HSTS hosts
11
     *
12
     * @var array[]
13
     */
14
    private $store = [];
15
16
    /**
17
     * Set the given domain name as a known HSTS host
18
     *
19
     * @param string $domainName
20
     * @param int    $expirySeconds
21
     * @param array  $policy
22
     */
23 15
    public function set($domainName, $expirySeconds, array $policy)
24
    {
25 15
        $expiryDate = new DateTime();
26 15
        $expiryDate->add(new DateInterval('PT' . $expirySeconds . 'S'));
27
28 15
        $this->store[$domainName] = [
29 15
            'expiry' => $expiryDate,
30 15
            'policy' => $policy,
31
        ];
32 15
    }
33
34
    /**
35
     * Check if the given domain name is a known HSTS host
36
     *
37
     * @param string $domainName
38
     * @return bool|array
39
     */
40 20
    public function get($domainName)
41
    {
42 20
        if (isset($this->store[$domainName])) {
43 15
            if ($this->store[$domainName]['expiry'] > new DateTime()) {
44 15
                return $this->store[$domainName]['policy'];
45
            } else {
46 2
                $this->delete($domainName);
47
            }
48
        }
49
50 13
        return false;
51
    }
52
53
    /**
54
     * Forget the given domain name as a known HSTS host
55
     *
56
     * @param string $domainName
57
     */
58 4
    public function delete($domainName)
59
    {
60 4
        unset($this->store[$domainName]);
61 4
    }
62
}
63