MessageJsBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 2
    public function __construct(MessageStorage $storage)
16
    {
17 2
        $this->storage = $storage;
18 2
    }
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 2
    public function exportJsonp(string $locale, array $domains, string $jsonpCallback)
29
    {
30 2
        $js = '';
31
32 2
        foreach ($domains as $domain) {
33 2
            $translations = $this->storage->getEnabledTranslatedJs($locale, $domain);
34
35 2
            if (!count($translations)) {
36 1
                continue;
37
            }
38
39 1
            $jed = Jed::toString($translations);
40
41 1
            $js .= $jsonpCallback . '(' . $jed . ");\n";
42
        }
43
44 2
        return $js;
45
    }
46
}