RecordCache::serializeRecordToIdentity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Thruster\Component\Dns;
4
5
use Thruster\Component\Promise\FulfilledPromise;
6
use Thruster\Component\Promise\RejectedPromise;
7
8
/**
9
 * Class RecordCache
10
 *
11
 * @package Thruster\Component\Dns
12
 * @author  Aurimas Niekis <[email protected]>
13
 */
14
class RecordCache
15
{
16
    /**
17
     * @var RecordBag[]
18
     */
19
    private $records;
20
21
    /**
22
     * @var int
23
     */
24
    private $expiredAt;
25
26
    public function __construct()
27
    {
28
        $this->records     = [];
29
        $this->expirations = [];
0 ignored issues
show
Bug introduced by
The property expirations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
    }
31
32
    public function lookup(Query $query)
33
    {
34
        $id = $this->serializeQueryToIdentity($query);
35
36
        $expiredAt = $this->expiredAt;
37
38
        if (false === isset($this->records[$id])) {
39
            return new RejectedPromise();
40
        }
41
42
        $recordBag = $this->records[$id];
43
44
        if (null !== $expiredAt && $expiredAt <= $query->getCurrentTime()) {
45
            return new RejectedPromise();
46
        }
47
48
        return new FulfilledPromise($recordBag->all());
49
    }
50
51
    public function storeResponseMessage($currentTime, Message $message)
52
    {
53
        foreach ($message->answers as $record) {
54
            $this->storeRecord($currentTime, $record);
55
        }
56
    }
57
58
    public function expire($currentTime)
59
    {
60
        $this->expiredAt = $currentTime;
61
    }
62
63
    private function storeRecord($currentTime, Record $record)
64
    {
65
        $id = $this->serializeRecordToIdentity($record);
66
67
        if (isset($this->records[$id])) {
68
            $recordBag = $this->records[$id];
69
        } else {
70
            $recordBag = new RecordBag();
71
        }
72
73
        $recordBag->set($currentTime, $record);
74
75
        $this->records[$id] = $recordBag;
76
    }
77
78
    private function serializeQueryToIdentity(Query $query)
79
    {
80
        return $query->getName() . ':' . $query->getType() . ':' . $query->getClass();
81
    }
82
83
    private function serializeRecordToIdentity(Record $record)
84
    {
85
        return $record->getName() . ':' . $record->getType() . ':' . $record->getClass();
86
    }
87
}
88