Completed
Push — master ( 5576ee...a16f3a )
by Mārtiņš
02:02
created

importFromTranslations()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 4
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 3
    public function __construct(MessageConfigInterface $config, MessageStorage $storage)
23
    {
24 3
        $this->storage = $storage;
25 3
        $this->config = $config;
26 3
    }
27
28
    /**
29
     * @param Translations $translations
30
     *
31
     * @throws UnsupportedLocaleException
32
     * @throws UnsupportedDomainException
33
     * @throws InvalidTranslationException
34
     */
35 3
    public function importFromTranslations(Translations $translations)
36
    {
37 3
        $locale = $translations->getLanguage();
38 3
        $domain = $translations->getDomain();
39
40 3
        if (!in_array($locale, $this->config->getLocales())) {
41 1
            throw new UnsupportedLocaleException('Locale ' . $locale . ' was not found in config');
42
        }
43
44 2
        if (!in_array($domain, $this->config->getOtherDomains()) && $domain !== $this->config->getDefaultDomain()) {
45 1
            throw new UnsupportedDomainException('Domain ' . $domain . ' was not found in config');
46
        }
47
48 1
        $this->storage->saveTranslated($translations);
49
    }
50
}