Completed
Pull Request — master (#89)
by
unknown
452:27 queued 387:21
created

FileImport::import()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
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\HttpFoundation\ParameterBag;
17
use Symfony\Component\Translation\MessageCatalogue;
18
19
/**
20
 * Import translations file.
21
 */
22
class FileImport
23
{
24
    /**
25
     * @var ParameterBag
26
     */
27
    private $loadersContainer;
28
29
    /**
30
     * @param ParameterBag $loadersContainer
31
     */
32
    public function __construct(ParameterBag $loadersContainer)
33
    {
34
        $this->loadersContainer = $loadersContainer;
35
    }
36
37
    /**
38
     * @param SplFileInfo $file
39
     *
40
     * @return array
41
     */
42
    public function import(SplFileInfo $file)
43
    {
44
        list($domain, $locale, $extension) = explode('.', $file->getFilename());
45
46
        $translations = [];
47
48
        if ($this->loadersContainer->has($extension)) {
49
            /** @var MessageCatalogue $messageCatalogue */
50
            $messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain);
51
            $domainMessages = $messageCatalogue->all($domain);
52
53
            if (!empty($domainMessages)) {
54
                $path = substr(pathinfo($file->getPathname(), PATHINFO_DIRNAME), strlen(getcwd()) + 1);
55
                foreach ($domainMessages as $key => $content) {
56
                    $translations[$domain][$key]['messages'][$locale] = $content;
57
                    $translations[$domain][$key]['path'] = $path;
58
                    $translations[$domain][$key]['format'] = $file->getExtension();
59
                }
60
            }
61
        }
62
63
        return $translations;
64
    }
65
}
66