TranslatedMessageImporter::importFromPo()   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 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use Gettext\Translations;
6
use Printful\GettextCms\Exceptions\InvalidTranslationException;
7
use Printful\GettextCms\Exceptions\UnsupportedDomainException;
8
use Printful\GettextCms\Exceptions\UnsupportedLocaleException;
9
use Printful\GettextCms\Interfaces\MessageConfigInterface;
10
11
/**
12
 * Class used for importing translated messages
13
 */
14
class TranslatedMessageImporter
15
{
16
    /** @var MessageStorage */
17
    private $storage;
18
19
    /** @var MessageConfigInterface */
20
    private $config;
21
22 5
    public function __construct(MessageConfigInterface $config, MessageStorage $storage)
23
    {
24 5
        $this->storage = $storage;
25 5
        $this->config = $config;
26 5
    }
27
28
    /**
29
     * Import translations from translation object
30
     *
31
     * @param Translations $translations
32
     * @throws UnsupportedLocaleException
33
     * @throws UnsupportedDomainException
34
     * @throws InvalidTranslationException
35
     */
36 4
    public function importFromTranslations(Translations $translations)
37
    {
38 4
        $locale = $translations->getLanguage();
39 4
        $domain = $translations->getDomain();
40
41 4
        if (!in_array($locale, $this->config->getLocales())) {
42 1
            throw new UnsupportedLocaleException('Locale ' . $locale . ' was not found in config');
43
        }
44
45 3
        if (!in_array($domain, $this->config->getOtherDomains()) && $domain !== $this->config->getDefaultDomain()) {
46 1
            throw new UnsupportedDomainException('Domain ' . $domain . ' was not found in config');
47
        }
48
49 2
        $this->storage->saveTranslated($translations);
50 2
    }
51
52
    /**
53
     * Import Translations from PO string
54
     *
55
     * @param string $po
56
     * @throws InvalidTranslationException
57
     * @throws UnsupportedDomainException
58
     * @throws UnsupportedLocaleException
59
     */
60 1
    public function importFromPo(string $po)
61
    {
62 1
        $this->importFromTranslations(Translations::fromPoString($po));
63
    }
64
}