Passed
Push — master ( a21ca3...043ecc )
by Sergey
02:54
created

RecordsStore::change()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12

1 Method

Rating   Name   Duplication   Size   Complexity  
A RecordsStore::count() 0 4 1
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 RecordsStore
28
     */
29
    public function remove(Record $record) : RecordsStore
30
    {
31
        foreach ($this->records as $key => $recordStored) {
32
            if ($recordStored === $record) {
33
                unset($this->records[$key]);
34
                break;
35
            }
36
        }
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param Record $record
43
     * @return RecordsStore
44
     */
45 1
    public function append(Record $record) : RecordsStore
46
    {
47 1
        $this->records[] = $record;
48
49 1
        return $this;
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function count() : int
56
    {
57
        return count($this->records);
58
    }
59
60
    /**
61
     * @param eRecordType $type
62
     * @return record\base\Record[]
63
     */
64 1
    public function iterate(eRecordType $type = NULL)
65
    {
66 1
        foreach ($this->records as $record) {
67 1
            if (NULL === $type) {
68 1
                yield $record;
69 1
            } elseif ($record->getType()->is($type)) {
70 1
                yield $record;
71
            }
72
        }
73 1
    }
74
75
    public function sort()
76
    {
77 1
        usort($this->records, function (Record $a, Record $b) {
78 1
            if ($a->getType()->is(eRecordType::SOA)) {
79 1
                return -1;
80 1
            } elseif ($b->getType()->is(eRecordType::SOA)) {
81
                return 1;
82
            }
83
84 1
            return strcmp($a->getType(), $b->getType());
85 1
        });
86
    }
87
}