MessageImporter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
c 1
b 0
f 0
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extractAndSave() 0 27 6
A __construct() 0 5 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 6
    public function __construct(MessageConfigInterface $config, MessageStorage $storage, MessageExtractor $extractor)
23
    {
24 6
        $this->extractor = $extractor;
25 6
        $this->storage = $storage;
26 6
        $this->config = $config;
27 6
    }
28
29
    /**
30
     * Extract messages and save them all to repository
31
     * Careful! Messages for each domain that were not found in this scan will be disabled.
32
     *
33
     * @param ScanItem[] $scanItems
34
     * @param bool $disableUnusedTranslations Should old, unused translations be disabled
35
     * @param array|null $domains Force domains to scan. If null, will scan default domains.
36
     * @throws GettextCmsException
37
     */
38 5
    public function extractAndSave(array $scanItems, bool $disableUnusedTranslations = true, array $domains = null)
39
    {
40 5
        $defaultLocale = $this->config->getDefaultLocale();
41
42 5
        $allDomainTranslations = $this->extractor->extract($scanItems, $domains);
43
44 5
        foreach ($this->config->getLocales() as $locale) {
45 5
            if ($locale === $defaultLocale) {
46
                // We do not save the default locale, because default locale is the gettext fallback
47
                // if no other locale is set
48 2
                continue;
49
            }
50
51 5
            foreach ($allDomainTranslations as $translations) {
52 5
                $translations->setLanguage($locale);
53
54 5
                if ($disableUnusedTranslations) {
55
                    // Set all previous translations as not in files
56 5
                    $this->storage->setAllAsNotInFilesAndInJs($locale, $translations->getDomain());
57
                }
58
59 5
                $this->storage->createOrUpdate($translations);
60
            }
61
        }
62
63 5
        if ($disableUnusedTranslations) {
64 5
            $this->storage->disableUnused();
65
        }
66
    }
67
}