Completed
Push — master ( 5c4f66...f201ec )
by Mārtiņš
01:55
created

MessageJsBuilder::exportJsonp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use Gettext\Generators\Jed;
6
7
/**
8
 * Class exports JS translations to JSONP callback files in JED translation format
9
 */
10
class MessageJsBuilder
11
{
12
    /** @var MessageStorage */
13
    private $storage;
14
15 1
    public function __construct(MessageStorage $storage)
16
    {
17 1
        $this->storage = $storage;
18 1
    }
19
20
    /**
21
     * Export one or multiple domains in a single JS file with JSONP callback that will load JED format translations
22
     *
23
     * @param string $locale
24
     * @param string[] $domains
25
     * @param string $jsonpCallback Function name, that will be called with domain translations
26
     * @return string
27
     */
28 1
    public function exportJsonp(string $locale, array $domains, string $jsonpCallback)
29
    {
30 1
        $js = '';
31
32 1
        foreach ($domains as $domain) {
33 1
            $translations = $this->storage->getEnabledTranslatedJs($locale, $domain);
34
35 1
            $jed = Jed::toString($translations);
36
37 1
            $js .= $jsonpCallback . '(' . $jed . ");\n";
38
        }
39
40 1
        return $js;
41
    }
42
}