Passed
Push — master ( c56ef2...593f48 )
by Mārtiņš
02:01
created

UntranslatedMessageZipExporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use InvalidArgumentException;
6
use Printful\GettextCms\Interfaces\MessageConfigInterface;
7
use ZipArchive;
8
9
/**
10
 * Class exports multiple domain untranslated PO files as a zip archive
11
 */
12
class UntranslatedMessageZipExporter
13
{
14
    /** @var UntranslatedMessageExporter */
15
    private $exporter;
16
17
    /** @var MessageConfigInterface */
18
    private $config;
19
20 3
    public function __construct(MessageConfigInterface $config, UntranslatedMessageExporter $exporter)
21
    {
22 3
        $this->exporter = $exporter;
23 3
        $this->config = $config;
24 3
    }
25
26
    /**
27
     * Create a zip archive with messages that require translations
28
     *
29
     * @param string $zipPathname Full pathname to the file where the ZIP archive should be written
30
     * @param string $locale
31
     * @param string[]|null $domains Domains to export. If not provided, export all domains in config
32
     * @return bool
33
     */
34 3
    public function export(string $zipPathname, string $locale, array $domains = null): bool
35
    {
36 3
        $dir = dirname($zipPathname);
37
38 3
        if (!is_dir($dir)) {
39 1
            throw new InvalidArgumentException('Directory does not exist: ' . $dir);
40
        }
41
42 2
        $zip = new ZipArchive;
43 2
        $zip->open($zipPathname, ZipArchive::CREATE);
44
45 2
        if (empty($domains)) {
46 2
            $domains = $this->config->getOtherDomains();
47 2
            $domains[] = $this->config->getDefaultDomain();
48
        }
49
50 2
        foreach ($domains as $domain) {
51 2
            $po = $this->exporter->exportPoString($locale, $domain);
52 2
            if ($po) {
53 2
                $zip->addFromString($locale . '-' . $domain . '.po', $po);
54
            }
55
        }
56
57 2
        $zip->close();
58
59 2
        return true;
60
    }
61
}