Completed
Pull Request — master (#69)
by Raúl
02:30 queued 01:15
created

Catalog::addEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Sepia\PoParser;
4
5
use Sepia\PoParser\Catalog\Entry;
6
7
class Catalog
8
{
9
    /** @var array */
10
    protected $headers;
11
12
    /** @var array */
13
    protected $entries;
14
15
    /**
16
     * @param Entry[] $entries
17
     */
18
    public function __construct(array $entries = array())
19
    {
20
        foreach ($entries as $entry) {
21
            $this->addEntry($entry);
22
        }
23
    }
24
25
    public function addEntry(Entry $entry)
26
    {
27
        $key = $this->getEntryHash(
28
            $entry->getMsgId(),
29
            $entry->getMsgCtxt()
30
        );
31
        $this->entries[$key] = $entry;
32
    }
33
34
    public function addHeaders(array $headers)
35
    {
36
        $this->headers = $headers;
37
    }
38
39
    /**
40
     * @param string      $msgid
41
     * @param string|null $msgctxt
42
     */
43
    public function removeEntry($msgid, $msgctxt = null)
44
    {
45
        $key = $this->getEntryHash($msgid, $msgctxt);
46
        if (isset($this->entries[$key])) {
47
            unset($this->entries[$key]);
48
        }
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getHeaders()
55
    {
56
        return $this->headers;
57
    }
58
59
    /**
60
     * @return Entry[]
61
     */
62
    public function getEntries()
63
    {
64
        return $this->entries;
65
    }
66
67
    /**
68
     * @param string      $msgId
69
     * @param string|null $context
70
     *
71
     * @return Entry|null
72
     */
73
    public function getEntry($msgId, $context = null)
74
    {
75
        $key = $this->getEntryHash($msgId, $context);
76
        if (!isset($this->entries[$key])) {
77
            return null;
78
        }
79
80
        return $this->entries[$key];
81
    }
82
83
    /**
84
     * @param string      $msgId
85
     * @param string|null $context
86
     *
87
     * @return string
88
     */
89
    private function getEntryHash($msgId, $context = null)
90
    {
91
        return md5($msgId.$context);
92
    }
93
}
94