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

RecordsStore   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 65.38%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 70
ccs 17
cts 26
cp 0.6538
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 11 3
A append() 0 6 1
A count() 0 4 1
A iterate() 0 10 4
A sort() 0 12 3
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
}