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

TranslatedMessageImporter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 35
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A importFromTranslations() 0 14 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
}