Passed
Push — master ( bb44e9...3f3f7b )
by Sergey
02:48
created

RecordsStore::isContain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 4/12/16
5
 * @time  : 1:00 PM
6
 */
7
8
namespace LTDBeget\dns\configurator\zoneEntities;
9
10
use LTDBeget\dns\configurator\zoneEntities\record\base\Record;
11
use LTDBeget\dns\enums\eRecordType;
12
13
/**
14
 * Class RecordsStore
15
 *
16
 * @package LTDBeget\dns\configurator\zoneEntities
17
 */
18
class RecordsStore
19
{
20
    /**
21
     * @var Record[]
22
     */
23
    protected $records = [];
24
25
    /**
26
     * @param Record $record
27
     * @return bool
28
     */
29
    public function isContain(Record $record) : bool
30
    {
31
        return isset($this->records[$record->getHash()]);
32
    }
33
34
    /**
35
     * @param Record $record
36
     * @return RecordsStore
37
     */
38
    public function remove(Record $record) : RecordsStore
39
    {
40
        unset($this->records[$record->getHash()]);
41
42
        return $this;
43
    }
44
45
    /**
46
     * @param Record $record
47
     * @return RecordsStore
48
     */
49
    public function change(Record $record) : RecordsStore
50
    {
51
        foreach ($this->records as $hash => $recordStored) {
52
            if ($recordStored->isEqual($record)) {
53
                unset($this->records[$hash]);
54
                $this->append($record);
55
                break;
56
            }
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param Record $record
64
     * @return RecordsStore
65
     */
66 1
    public function append(Record $record) : RecordsStore
67
    {
68 1
        $this->records[$record->getHash()] = $record;
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * @return int
75
     */
76
    public function count() : int
77
    {
78
        return count($this->records);
79
    }
80
81
    /**
82
     * @param eRecordType $type
83
     * @return record\base\Record[]
84
     */
85 1
    public function iterate(eRecordType $type = NULL)
86
    {
87 1
        foreach ($this->records as $record) {
88 1
            if (is_null($type)) {
89 1
                yield $record;
90 1
            } elseif ($record->getType()->is($type)) {
91 1
                yield $record;
92
            }
93
        }
94 1
    }
95
96
    public function sort()
97
    {
98 1
        usort($this->records, function (Record $a, Record $b) {
99 1
            if ($a->getType()->is(eRecordType::SOA)) {
100 1
                return -1;
101 1
            } elseif ($b->getType()->is(eRecordType::SOA)) {
102
                return 1;
103
            }
104
105 1
            return strcmp($a->getType(), $b->getType());
106 1
        });
107
    }
108
}