Completed
Pull Request — master (#1)
by Sascha-Oliver
04:47
created

XliffFileDumper::dumpXliff1()   C

Complexity

Conditions 12
Paths 36

Size

Total Lines 74
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 0
cts 57
cp 0
rs 5.3992
c 0
b 0
f 0
cc 12
eloc 45
nc 36
nop 4
crap 156

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Translation\PlatformAdapter\PhraseApp\Bridge\Symfony;
4
5
use Symfony\Component\Translation\Dumper\FileDumper;
6
use Symfony\Component\Translation\MessageCatalogue;
7
8
/**
9
 * Custom XliffFileDumper for use with phrase app
10
 */
11
class XliffFileDumper extends FileDumper
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
17
    {
18
        $xliffVersion = '1.2';
19
        if (array_key_exists('xliff_version', $options)) {
20
            $xliffVersion = $options['xliff_version'];
21
        }
22
23
        if (array_key_exists('default_locale', $options)) {
24
            $defaultLocale = $options['default_locale'];
25
        } else {
26
            $defaultLocale = \Locale::getDefault();
27
        }
28
29
        if ('1.2' === $xliffVersion) {
30
            return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
31
        }
32
        if ('2.0' === $xliffVersion) {
33
            return $this->dumpXliff2($defaultLocale, $messages, $domain, $options);
34
        }
35
36
        throw new \InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function format(MessageCatalogue $messages, $domain)
43
    {
44
        @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the formatCatalogue() method instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
46
        return $this->formatCatalogue($messages, $domain);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function getExtension()
53
    {
54
        return 'xlf';
55
    }
56
57
    private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
58
    {
59
        $toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
60
        if (array_key_exists('tool_info', $options)) {
61
            $toolInfo = array_merge($toolInfo, $options['tool_info']);
62
        }
63
64
        $dom = new \DOMDocument('1.0', 'utf-8');
65
        $dom->formatOutput = true;
66
67
        $xliff = $dom->appendChild($dom->createElement('xliff'));
68
        $xliff->setAttribute('version', '1.2');
69
        $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
70
71
        $xliffFile = $xliff->appendChild($dom->createElement('file'));
72
        $xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
73
        $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
74
        $xliffFile->setAttribute('datatype', 'plaintext');
75
        $xliffFile->setAttribute('original', 'file.ext');
76
77
        $xliffHead = $xliffFile->appendChild($dom->createElement('header'));
78
        $xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
79
        foreach ($toolInfo as $id => $value) {
80
            $xliffTool->setAttribute($id, $value);
81
        }
82
83
        $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
84
        foreach ($messages->all($domain) as $source => $target) {
85
            $translation = $dom->createElement('trans-unit');
86
87
            // changed for phrase app
88
            $translation->setAttribute('id', "$domain::$source");
89
            $translation->setAttribute('resname', $source);
90
91
            $s = $translation->appendChild($dom->createElement('source'));
92
            $s->appendChild($dom->createTextNode($source));
93
94
            // Does the target contain characters requiring a CDATA section?
95
            $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
96
97
            $targetElement = $dom->createElement('target');
98
            $metadata = $messages->getMetadata($source, $domain);
99
            if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
100
                foreach ($metadata['target-attributes'] as $name => $value) {
101
                    $targetElement->setAttribute($name, $value);
102
                }
103
            }
104
            $t = $translation->appendChild($targetElement);
105
            $t->appendChild($text);
106
107
            if ($this->hasMetadataArrayInfo('notes', $metadata)) {
108
                foreach ($metadata['notes'] as $note) {
109
                    if (!isset($note['content'])) {
110
                        continue;
111
                    }
112
113
                    $n = $translation->appendChild($dom->createElement('note'));
114
                    $n->appendChild($dom->createTextNode($note['content']));
115
116
                    if (isset($note['priority'])) {
117
                        $n->setAttribute('priority', $note['priority']);
118
                    }
119
120
                    if (isset($note['from'])) {
121
                        $n->setAttribute('from', $note['from']);
122
                    }
123
                }
124
            }
125
126
            $xliffBody->appendChild($translation);
127
        }
128
129
        return $dom->saveXML();
130
    }
131
132
    private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    {
134
        $dom = new \DOMDocument('1.0', 'utf-8');
135
        $dom->formatOutput = true;
136
137
        $xliff = $dom->appendChild($dom->createElement('xliff'));
138
        $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
139
        $xliff->setAttribute('version', '2.0');
140
        $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
141
        $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
142
143
        $xliffFile = $xliff->appendChild($dom->createElement('file'));
144
        $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
145
146
        foreach ($messages->all($domain) as $source => $target) {
147
            $translation = $dom->createElement('unit');
148
149
            // changed for phrase app
150
            $translation->setAttribute('id', "$domain::$source");
151
152
            $segment = $translation->appendChild($dom->createElement('segment'));
153
154
            $s = $segment->appendChild($dom->createElement('source'));
155
            $s->appendChild($dom->createTextNode($source));
156
157
            // Does the target contain characters requiring a CDATA section?
158
            $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
159
160
            $targetElement = $dom->createElement('target');
161
            $metadata = $messages->getMetadata($source, $domain);
162
            if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
163
                foreach ($metadata['target-attributes'] as $name => $value) {
164
                    $targetElement->setAttribute($name, $value);
165
                }
166
            }
167
            $t = $segment->appendChild($targetElement);
168
            $t->appendChild($text);
169
170
            $xliffFile->appendChild($translation);
171
        }
172
173
        return $dom->saveXML();
174
    }
175
176
    /**
177
     * @param string     $key
178
     * @param array|null $metadata
179
     *
180
     * @return bool
181
     */
182
    private function hasMetadataArrayInfo($key, $metadata = null)
183
    {
184
        return null !== $metadata && array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || is_array($metadata[$key]));
185
    }
186
}
187