Completed
Push — master ( c8c410...b53e98 )
by Tobias
05:48
created

CatalogueFetcher::__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\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
    public function __construct(TranslationLoader $loader)
37
    {
38
        $this->loader = $loader;
39
    }
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