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

Catalog   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 87
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addEntry() 0 8 1
A addHeaders() 0 4 1
A removeEntry() 0 7 2
A getHeaders() 0 4 1
A getEntries() 0 4 1
A getEntry() 0 9 2
A getEntryHash() 0 4 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