Passed
Push — master ( 2dbdae...9bc85d )
by Mārtiņš
01:45
created

MessageImporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
4
namespace Printful\GettextCms;
5
6
7
use Printful\GettextCms\Exceptions\GettextCmsException;
8
use Printful\GettextCms\Interfaces\MessageConfigInterface;
9
use Printful\GettextCms\Structures\ScanItem;
10
11
class MessageImporter
12
{
13
    /** @var MessageExtractor */
14
    private $extractor;
15
16
    /** @var MessageStorage */
17
    private $storage;
18
19
    /** @var MessageConfigInterface */
20
    private $config;
21
22 1
    public function __construct(MessageConfigInterface $config, MessageStorage $storage, MessageExtractor $extractor)
23
    {
24 1
        $this->extractor = $extractor;
25 1
        $this->storage = $storage;
26 1
        $this->config = $config;
27 1
    }
28
29
    /**
30
     * Extract translations and save them all to repository
31
     *
32
     * @param ScanItem[] $scanItems
33
     * @throws GettextCmsException
34
     */
35 1
    public function extractAndSave(array $scanItems)
36
    {
37 1
        $defaultLocale = $this->config->getDefaultLocale();
38
39 1
        $allDomainTranslations = $this->extractor->extract($scanItems);
40
41 1
        foreach ($this->config->getLocales() as $locale) {
42 1
            if ($locale === $defaultLocale) {
43
                // We do not save the default locale, because default locale is the gettext fallback
44
                // if no other locale is set
45 1
                continue;
46
            }
47
48 1
            foreach ($allDomainTranslations as $translations) {
49
                // Disable all previous translations and save the new ones
50 1
                $this->storage->disableAll($locale, $translations->getDomain());
51 1
                $translations->setLanguage($locale);
52 1
                $this->storage->createOrUpdate($translations);
53
            }
54
        }
55
    }
56
}