Passed
Push — master ( 9223d2...163f52 )
by Christian
55s queued 10s
created

Cached::unwrapResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace RemotelyLiving\PHPDNS\Resolvers;
3
4
use Psr\Cache\CacheItemPoolInterface;
5
use RemotelyLiving\PHPDNS\Entities\DNSRecordCollection;
6
use RemotelyLiving\PHPDNS\Entities\DNSRecordType;
7
use RemotelyLiving\PHPDNS\Entities\Hostname;
8
use RemotelyLiving\PHPDNS\Resolvers\Traits\Time;
9
use RemotelyLiving\PHPDNS\Resolvers\Interfaces\Resolver;
10
11
class Cached extends ResolverAbstract
12
{
13
    protected const DEFAULT_CACHE_TTL = 600;
14
    private const CACHE_KEY_TEMPLATE = '%s:%s:%s';
15
16
    use Time;
17
18
    /**
19
     * @var \Psr\Cache\CacheItemPoolInterface
20
     */
21
    private $cache;
22
23
    /**
24
     * @var \RemotelyLiving\PHPDNS\Resolvers\Interfaces\Resolver
25
     */
26
    private $resolver;
27
28
    /**
29
     * @var string
30
     */
31
    private $namespace = 'php-dns-v1';
32
33
    /**
34
     * @var int|null
35
     */
36
    private $ttlSeconds;
37
38
    public function __construct(CacheItemPoolInterface $cache, Resolver $resolver, int $ttlSeconds = null)
39
    {
40
        $this->cache = $cache;
41
        $this->resolver = $resolver;
42
        $this->ttlSeconds = $ttlSeconds;
43
    }
44
45
    public function flush(): void
46
    {
47
        $this->cache->clear();
48
    }
49
50
    protected function doQuery(Hostname $hostname, DNSRecordType $recordType): DNSRecordCollection
51
    {
52
        $cachedResult = $this->cache->getItem($this->buildCacheKey($hostname, $recordType));
53
54
        if ($cachedResult->isHit()) {
55
            return $this->unwrapResults($cachedResult->get());
56
        }
57
58
        $dnsRecords = $this->resolver->getRecords((string)$hostname, (string)$recordType);
59
        $ttlSeconds = $this->ttlSeconds ?? $this->extractLowestTTL($dnsRecords);
60
        $cachedResult->expiresAfter($ttlSeconds);
61
        $cachedResult->set(['recordCollection' => $dnsRecords, 'timestamp' => $this->getTimeStamp()]);
62
        $this->cache->save($cachedResult);
63
64
        return $dnsRecords;
65
    }
66
67
    private function buildCacheKey(Hostname $hostname, DNSRecordType $recordType): string
68
    {
69
        return md5(sprintf(self::CACHE_KEY_TEMPLATE, $this->namespace, (string)$hostname, (string)$recordType));
70
    }
71
72
    private function extractLowestTTL(DNSRecordCollection $recordCollection): int
73
    {
74
        $ttls = [];
75
76
        if ($recordCollection->isEmpty()) {
77
            return self::DEFAULT_CACHE_TTL;
78
        }
79
80
        /** @var \RemotelyLiving\PHPDNS\Entities\DNSRecord $record */
81
        foreach ($recordCollection as $record) {
82
            /** @scrutinizer ignore-call */
83
            $ttls[] = $record->getTTL();
84
        }
85
86
        return min($ttls);
87
    }
88
89
    /**
90
     * @param array $results ['recordCollection' => $recordCollection, 'timestamp' => $timeStamp]
91
     */
92
    private function unwrapResults(array $results) : DNSRecordCollection
93
    {
94
        $records = $results['recordCollection'];
95
        foreach ($records as $key => $record) {
96
            $records[$key] = $record
97
                ->setTTL($record->getTTL() - ($this->getTimeStamp() - (int)$results['timestamp']));
98
        }
99
100
        return $records;
101
    }
102
}
103