Completed
Push — master ( a09b4a...382e94 )
by Tobias
08:18
created

XliffDumper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 53
ccs 13
cts 19
cp 0.6842
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 4 1
C formatCatalogue() 0 34 7
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\SymfonyStorage\Dumper;
13
14
use Nyholm\NSA;
15
use Symfony\Component\Translation\Dumper\XliffFileDumper;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Symfony\Component\Translation\Exception\InvalidArgumentException;
18
use Translation\SymfonyStorage\Dumper\Port\SymfonyPort;
19
20
/**
21
 * XliffFileDumper generates xliff files from a message catalogue.
22
 *
23
 * This class provides support for both SF2.7 and SF3.0+
24
 *
25
 * @author Tobias Nyholm <[email protected]>
26
 * @author Michel Salib <[email protected]>
27
 */
28
final class XliffDumper extends XliffFileDumper
29
{
30
    /**
31
     * @var SymfonyPort|null
32
     */
33
    private $sfPort;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
39
    {
40 3
        $xliffVersion = '1.2';
41 3
        if (array_key_exists('xliff_version', $options)) {
42 3
            $xliffVersion = $options['xliff_version'];
43
        }
44
45 3
        if (array_key_exists('default_locale', $options)) {
46
            $defaultLocale = $options['default_locale'];
47
        } else {
48 3
            $defaultLocale = \Locale::getDefault();
49
        }
50
51 3
        if ('1.2' === $xliffVersion) {
52 1
            if (method_exists($this, 'dumpXliff1')) {
53 1
                return NSA::invokeMethod($this, 'dumpXliff1', $defaultLocale, $messages, $domain, $options);
54
            } else {
55
                // Symfony 2.7
56
                return $this->format($messages, $domain);
57
            }
58
        }
59
60 2
        if ('2.0' === $xliffVersion) {
61 2
            if (null === $this->sfPort) {
62 2
                $this->sfPort = new SymfonyPort();
63
            }
64
65 2
            return $this->sfPort->dumpXliff2($defaultLocale, $messages, $domain, $options);
66
        }
67
68
        throw new InvalidArgumentException(
69
            sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion)
70
        );
71
    }
72
73
    /**
74
     * To support Symfony 2.7.
75
     */
76
    protected function format(MessageCatalogue $messages, $domain)
77
    {
78
        return $this->formatCatalogue($messages, $domain, ['xliff_version' => '2.0']);
79
    }
80
}
81