Completed
Push — master ( e245d2...58026a )
by Mārtiņš
02:18
created

MessageExporter::getMoPathname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Printful\GettextCms;
5
6
7
use Gettext\Generators\Mo;
8
use Printful\GettextCms\Exceptions\InvalidPathException;
9
use Printful\GettextCms\Interfaces\MessageConfigInterface;
10
11
class MessageExporter
12
{
13
    /** @var MessageConfigInterface */
14
    private $config;
15
16
    /** @var MessageStorage */
17
    private $storage;
18
19 2
    public function __construct(
20
        MessageConfigInterface $config,
21
        MessageStorage $storage
22
    ) {
23 2
        $this->config = $config;
24 2
        $this->storage = $storage;
25 2
    }
26
27
    /**
28
     * @param string $locale
29
     * @param string $domain
30
     * @return bool
31
     * @throws InvalidPathException
32
     */
33 2
    public function export(string $locale, string $domain)
34
    {
35 2
        $translations = $this->storage->getTranslations($locale, $domain);
36
37 2
        $pathname = $this->getMoPathname($locale, $domain);
38
39 1
        return Mo::toFile($translations, $pathname);
40
    }
41
42
    /**
43
     * @param string $locale
44
     * @param string $domain
45
     * @return string
46
     * @throws InvalidPathException
47
     */
48 2
    private function getMoPathname(string $locale, string $domain)
49
    {
50 2
        return $pathname = $this->ensureDirectory($locale) . '/' . $domain . '.mo';
0 ignored issues
show
Unused Code introduced by
The assignment to $pathname is dead and can be removed.
Loading history...
51
    }
52
53
    /**
54
     * @param string $locale
55
     * @return string
56
     * @throws InvalidPathException
57
     */
58 2
    private function ensureDirectory(string $locale)
59
    {
60 2
        $path = rtrim($this->config->getMoDirectory(), '/');
61
62 2
        if (!is_dir($path)) {
63 1
            throw new InvalidPathException("Directory '$path' does not exist");
64
        }
65
66 1
        $path = $path . '/' . $locale . '/LC_MESSAGES';
67
68 1
        if (!is_dir($path)) {
69 1
            mkdir($path, 0777, true);
70
        }
71
72 1
        return $path;
73
    }
74
}