Completed
Pull Request — master (#89)
by
unknown
65:00
created

FileImport::import()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\Service\Import;
13
14
use ONGR\TranslationsBundle\Service\LoadersContainer;
15
use Symfony\Component\Finder\SplFileInfo;
16
use Symfony\Component\Translation\MessageCatalogue;
17
18
/**
19
 * Import translations file.
20
 */
21
class FileImport implements ImporterInterface
22
{
23
    /**
24
     * @var LoadersContainer
25
     */
26
    private $loadersContainer;
27
28
    /**
29
     * @param LoadersContainer $loadersContainer
30
     */
31
    public function __construct(LoadersContainer $loadersContainer)
32
    {
33
        $this->loadersContainer = $loadersContainer;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function import(SplFileInfo $file)
40
    {
41
        list($domain, $locale, $extension) = explode('.', $file->getFilename());
42
43
        $translations = [];
44
45
        if ($this->loadersContainer->has($extension)) {
46
            /** @var MessageCatalogue $messageCatalogue */
47
            $messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain);
48
            $domainMessages = $messageCatalogue->all($domain);
49
50
            if (!empty($domainMessages)) {
51
                $path = substr(pathinfo($file->getPathname(), PATHINFO_DIRNAME), strlen(getcwd()) + 1);
52
                $translations[$path][$domain]['format'] = $file->getExtension();
53
                foreach ($domainMessages as $key => $content) {
54
                    $translations[$path][$domain]['translations'][$key][$locale] = $content;
55
                }
56
            }
57
        }
58
59
        return $translations;
60
    }
61
}
62