Completed
Push — master ( 13f2dd...a39436 )
by Tobias
05:16
created

CatalogueCounter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Catalogue;
13
14
use Symfony\Component\Translation\MessageCatalogueInterface;
15
use Symfony\Component\Translation\MetadataAwareInterface;
16
17
/**
18
 * Calculate the number of messages in a catalogue.
19
 *
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class CatalogueCounter
23
{
24
    /**
25
     * @var NoteParser
26
     */
27
    private $noteParser;
28
29
    /**
30
     * @param NoteParser $noteParser
31
     */
32
    public function __construct(NoteParser $noteParser)
33
    {
34
        $this->noteParser = $noteParser;
35
    }
36
37
    /**
38
     * @param MessageCatalogueInterface $catalogue
39
     *
40
     * @return int
41
     */
42
    public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue)
43
    {
44
        $total = 0;
45
        foreach ($catalogue->getDomains() as $domain) {
46
            $total += count($catalogue->all($domain));
47
        }
48
49
        return $total;
50
    }
51
52
    /**
53
     * @param MessageCatalogueInterface $catalogue
54
     *
55
     * @return array
56
     */
57
    public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
58
    {
59
        $result = [];
60
        $domains = $catalogue->getDomains();
61
        foreach ($domains as $domain) {
62
            $result[$domain]['defined'] = count($catalogue->all($domain));
63
        }
64
65
        // Sum the number of defined messages.
66
        $result['_total']['defined'] = 0;
67
        foreach ($domains as $domain) {
68
            $result['_total']['defined'] += $result[$domain]['defined'];
69
        }
70
71
        if (!$catalogue instanceof MetadataAwareInterface) {
72
            return $result;
73
        }
74
75
        // For each domain check if the message is new or undefined.
76
        foreach ($domains as $domain) {
77
            $result[$domain]['new'] = 0;
78
            $result[$domain]['obsolete'] = 0;
79
80
            foreach ($catalogue->all($domain) as $key => $text) {
81
                $notes = $this->noteParser->getNotes($domain, $key, $catalogue);
82
                if ($this->noteParser->hasNoteNew($notes)) {
83
                    ++$result[$domain]['new'];
84
                }
85
86
                if ($this->noteParser->hasNoteObsolete($notes)) {
87
                    ++$result[$domain]['obsolete'];
88
                }
89
            }
90
        }
91
92
        // Sum the number of new and obsolete messages.
93
        $result['_total']['new'] = 0;
94
        $result['_total']['obsolete'] = 0;
95
        foreach ($domains as $domain) {
96
            $result['_total']['new'] += $result[$domain]['new'];
97
            $result['_total']['obsolete'] += $result[$domain]['obsolete'];
98
        }
99
100
        return $result;
101
    }
102
}
103