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

RuntimeStorage::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
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