Completed
Push — master ( 4e8bc9...f46df8 )
by Tobias
13:01 queued 05:03
created

CatalogueCounter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 0
loc 76
ccs 42
cts 45
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getNumberOfDefinedMessages() 0 9 2
C getCatalogueStatistics() 0 53 10
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 1
    public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue)
31
    {
32 1
        $total = 0;
33 1
        foreach ($catalogue->getDomains() as $domain) {
34 1
            $total += count($catalogue->all($domain));
35 1
        }
36
37 1
        return $total;
38
    }
39
40
    /**
41
     * @param MessageCatalogueInterface $catalogue
42
     *
43
     * @return array
44
     */
45 1
    public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
46
    {
47 1
        $result = [];
48 1
        $domains = $catalogue->getDomains();
49 1
        foreach ($domains as $domain) {
50 1
            $result[$domain]['defined'] = count($catalogue->all($domain));
51 1
        }
52
53
        // Sum the number of defined messages.
54 1
        $result['_total']['defined'] = 0;
55 1
        foreach ($domains as $domain) {
56 1
            $result['_total']['defined'] += $result[$domain]['defined'];
57 1
        }
58
59 1
        if (!$catalogue instanceof MetadataAwareInterface) {
60
            return $result;
61
        }
62
63
        // For each domain check if the message is new or undefined.
64 1
        foreach ($domains as $domain) {
65 1
            $result[$domain]['new'] = 0;
66 1
            $result[$domain]['obsolete'] = 0;
67 1
            $result[$domain]['approved'] = 0;
68
69 1
            foreach ($catalogue->all($domain) as $key => $text) {
70 1
                $metadata = new Metadata($catalogue->getMetadata($key, $domain));
71 1
                $state = $metadata->getState();
72 1
                if ($state === 'new') {
73 1
                    ++$result[$domain]['new'];
74 1
                }
75
76 1
                if ($state === 'obsolete') {
77
                    ++$result[$domain]['obsolete'];
78
                }
79
80 1
                if ($metadata->isApproved()) {
81 1
                    ++$result[$domain]['approved'];
82 1
                }
83 1
            }
84 1
        }
85
86
        // Sum the number of new and obsolete messages.
87 1
        $result['_total']['new'] = 0;
88 1
        $result['_total']['obsolete'] = 0;
89 1
        $result['_total']['approved'] = 0;
90 1
        foreach ($domains as $domain) {
91 1
            $result['_total']['new'] += $result[$domain]['new'];
92 1
            $result['_total']['obsolete'] += $result[$domain]['obsolete'];
93 1
            $result['_total']['approved'] += $result[$domain]['approved'];
94 1
        }
95
96 1
        return $result;
97
    }
98
}
99