Completed
Push — master ( f4d1f5...04c37a )
by Tobias
08:58
created

CatalogueFetcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 15.79%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 43
ccs 3
cts 19
cp 0.1579
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getCatalogues() 0 19 5
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\Bundle\FrameworkBundle\Translation\TranslationLoader;
15
use Symfony\Component\Translation\MessageCatalogue;
16
use Translation\Bundle\Model\Configuration;
17
18
/**
19
 * Fetches catalogues from source files. This will only work with local file storage
20
 * and the actions are read only.
21
 *
22
 * This should be considered as a "ReadFromCache" service.
23
 *
24
 * @author Tobias Nyholm <[email protected]>
25
 */
26
final class CatalogueFetcher
27
{
28
    /**
29
     * @var TranslationLoader
30
     */
31
    private $loader;
32
33
    /**
34
     * @param TranslationLoader $loader
35
     */
36 1
    public function __construct(TranslationLoader $loader)
37
    {
38 1
        $this->loader = $loader;
39 1
    }
40
41
    /**
42
     * load any existing messages from the translation files.
43
     *
44
     * @param Configuration $config
45
     * @param array         $locales
46
     *
47
     * @return MessageCatalogue[]
48
     */
49
    public function getCatalogues(Configuration $config, array $locales = [])
50
    {
51
        $dirs = $config->getPathsToTranslationFiles();
52
        if (empty($locales)) {
53
            $locales = $config->getLocales();
54
        }
55
        $catalogues = [];
56
        foreach ($locales as $locale) {
57
            $currentCatalogue = new MessageCatalogue($locale);
58
            foreach ($dirs as $path) {
59
                if (is_dir($path)) {
60
                    $this->loader->loadMessages($path, $currentCatalogue);
61
                }
62
            }
63
            $catalogues[] = $currentCatalogue;
64
        }
65
66
        return $catalogues;
67
    }
68
}
69