Completed
Push — master ( 740372...dc5b6e )
by Tobias
10:09
created

CatalogueFetcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 43
ccs 19
cts 19
cp 1
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 6
    public function __construct(TranslationLoader $loader)
37
    {
38 6
        $this->loader = $loader;
39 6
    }
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 2
    public function getCatalogues(Configuration $config, array $locales = [])
50
    {
51 2
        $dirs = $config->getPathsToTranslationFiles();
52 2
        if (empty($locales)) {
53 2
            $locales = $config->getLocales();
54 2
        }
55 2
        $catalogues = [];
56 2
        foreach ($locales as $locale) {
57 2
            $currentCatalogue = new MessageCatalogue($locale);
58 2
            foreach ($dirs as $path) {
59 2
                if (is_dir($path)) {
60 2
                    $this->loader->loadMessages($path, $currentCatalogue);
61 2
                }
62 2
            }
63 2
            $catalogues[] = $currentCatalogue;
64 2
        }
65
66 2
        return $catalogues;
67
    }
68
}
69