Completed
Push — master ( 130d29...c9c04e )
by Tobias
187:22 queued 181:29
created

CatalogueCounter::getNumberOfDefinedMessages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
use Translation\Bundle\Model\Metadata;
17
18
/**
19
 * Calculate the number of messages in a catalogue.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class CatalogueCounter
24
{
25
    /**
26
     * @param MessageCatalogueInterface $catalogue
27
     *
28
     * @return int
29
     */
30
    public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue)
31
    {
32
        $total = 0;
33
        foreach ($catalogue->getDomains() as $domain) {
34
            $total += count($catalogue->all($domain));
35
        }
36
37
        return $total;
38
    }
39
40
    /**
41
     * @param MessageCatalogueInterface $catalogue
42
     *
43
     * @return array
44
     */
45
    public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
46
    {
47
        $result = [];
48
        $domains = $catalogue->getDomains();
49
        foreach ($domains as $domain) {
50
            $result[$domain]['defined'] = count($catalogue->all($domain));
51
        }
52
53
        // Sum the number of defined messages.
54
        $result['_total']['defined'] = 0;
55
        foreach ($domains as $domain) {
56
            $result['_total']['defined'] += $result[$domain]['defined'];
57
        }
58
59
        if (!$catalogue instanceof MetadataAwareInterface) {
60
            return $result;
61
        }
62
63
        // For each domain check if the message is new or undefined.
64
        foreach ($domains as $domain) {
65
            $result[$domain]['new'] = 0;
66
            $result[$domain]['obsolete'] = 0;
67
            $result[$domain]['approved'] = 0;
68
69
            foreach ($catalogue->all($domain) as $key => $text) {
70
                $metadata = new Metadata($catalogue->getMetadata($key, $domain));
71
                $state = $metadata->getState();
72
                if ('new' === $state) {
73
                    ++$result[$domain]['new'];
74
                }
75
76
                if ('obsolete' === $state) {
77
                    ++$result[$domain]['obsolete'];
78
                }
79
80
                if ($metadata->isApproved()) {
81
                    ++$result[$domain]['approved'];
82
                }
83
            }
84
        }
85
86
        // Sum the number of new and obsolete messages.
87
        $result['_total']['new'] = 0;
88
        $result['_total']['obsolete'] = 0;
89
        $result['_total']['approved'] = 0;
90
        foreach ($domains as $domain) {
91
            $result['_total']['new'] += $result[$domain]['new'];
92
            $result['_total']['obsolete'] += $result[$domain]['obsolete'];
93
            $result['_total']['approved'] += $result[$domain]['approved'];
94
        }
95
96
        return $result;
97
    }
98
}
99