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

MessageJsBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 31
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exportJsonp() 0 13 2
A __construct() 0 3 1
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
}