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

Catalog   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 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
        $this->entries = [];
21
        foreach ($entries as $entry) {
22
            $this->addEntry($entry);
23
        }
24
    }
25
26
    public function addEntry(Entry $entry)
27
    {
28
        $key = $this->getEntryHash(
29
            $entry->getMsgId(),
30
            $entry->getMsgCtxt()
31
        );
32
        $this->entries[$key] = $entry;
33
    }
34
35
    public function addHeaders(array $headers)
36
    {
37
        $this->headers = $headers;
38
    }
39
40
    /**
41
     * @param string      $msgid
42
     * @param string|null $msgctxt
43
     */
44
    public function removeEntry($msgid, $msgctxt = null)
45
    {
46
        $key = $this->getEntryHash($msgid, $msgctxt);
47
        if (isset($this->entries[$key])) {
48
            unset($this->entries[$key]);
49
        }
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getHeaders()
56
    {
57
        return $this->headers;
58
    }
59
60
    /**
61
     * @return Entry[]
62
     */
63
    public function getEntries()
64
    {
65
        return $this->entries;
66
    }
67
68
    /**
69
     * @param string      $msgId
70
     * @param string|null $context
71
     *
72
     * @return Entry|null
73
     */
74
    public function getEntry($msgId, $context = null)
75
    {
76
        $key = $this->getEntryHash($msgId, $context);
77
        if (!isset($this->entries[$key])) {
78
            return null;
79
        }
80
81
        return $this->entries[$key];
82
    }
83
84
    /**
85
     * @param string      $msgId
86
     * @param string|null $context
87
     *
88
     * @return string
89
     */
90
    private function getEntryHash($msgId, $context = null)
91
    {
92
        return md5($msgId.$context);
93
    }
94
}
95