Passed
Push — main ( 7da3b9...7b027b )
by Alexey
03:01
created

RuntimeStorage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 20
ccs 6
cts 8
cp 0.75
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 3 1
A get() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (c) Ne-Lexa
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 *
11
 * @see https://github.com/Ne-Lexa/guzzle-doh-middleware
12
 */
13
14
namespace Nelexa\Doh\Storage;
15
16
class RuntimeStorage implements StorageInterface
17
{
18
    /** @var array<string, \Nelexa\Doh\Storage\StorageItem> */
19
    private $cache = [];
20
21 11
    public function get(string $domainName): ?StorageItem
22
    {
23 11
        $storageItem = $this->cache[$domainName] ?? null;
24
25 11
        if ($storageItem !== null && $storageItem->getExpiredAt() < new \DateTimeImmutable()) {
26
            unset($this->cache[$domainName]);
27
            $storageItem = null;
28
        }
29
30 11
        return $storageItem;
31
    }
32
33 11
    public function save(string $domainName, StorageItem $item): void
34
    {
35 11
        $this->cache[$domainName] = $item;
36
    }
37
}
38