Completed
Push — master ( a6f7c5...c8c835 )
by Tobias
14:48 queued 13:39
created

TranslationLoader::loadMessages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 3.0067
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\Converter\Loader;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Translation\Loader\LoaderInterface;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Translation\SymfonyStorage\TranslationLoader as TranslationLoaderInterface;
18
19
class TranslationLoader implements TranslationLoaderInterface
20
{
21
    /**
22
     * @var LoaderInterface
23
     */
24
    private $loader;
25
26
    /**
27
     * @var string
28
     */
29
    private $format;
30
31
    /**
32
     * @param LoaderInterface $loader
33
     * @param string          $format
34
     */
35 2
    public function __construct(LoaderInterface $loader, $format)
36
    {
37 2
        $this->loader = $loader;
38 2
        $this->format = $format;
39 2
    }
40
41
    /**
42
     * Loads translation messages from a directory to the catalogue.
43
     *
44
     * @param string           $directory the directory to look into
45
     * @param MessageCatalogue $catalogue the catalogue
46
     */
47 2
    public function loadMessages($directory, MessageCatalogue $catalogue)
48
    {
49 2
        if (!is_dir($directory)) {
50
            return;
51
        }
52
53
        // load any existing translation files
54 2
        $finder = new Finder();
55 2
        $extension = $catalogue->getLocale().'.'.$this->format;
56 2
        $files = $finder->files()->name('*.'.$extension)->in($directory);
57 2
        foreach ($files as $file) {
58 2
            $domain = substr($file->getFilename(), 0, -1 * strlen($extension) - 1);
59 2
            $catalogue->addCatalogue($this->loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
60 2
        }
61 2
    }
62
}
63