Completed
Push — master ( 3f966d...f576a1 )
by Mārtiņš
02:13
created

MessageBuilder::ensureDirectory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use Gettext\Generators\Mo;
6
use Printful\GettextCms\Exceptions\InvalidPathException;
7
use Printful\GettextCms\Interfaces\MessageConfigInterface;
8
9
/**
10
 * Class allows to export translations from repository to a generated .mo file which is used with gettext
11
 */
12
class MessageBuilder
13
{
14
    /** @var MessageConfigInterface */
15
    private $config;
16
17
    /** @var MessageStorage */
18
    private $storage;
19
20
    /** @var MessageRevisions */
21
    private $revisions;
22
23 6
    public function __construct(
24
        MessageConfigInterface $config,
25
        MessageStorage $storage,
26
        MessageRevisions $revisions
27
    ) {
28 6
        $this->config = $config;
29 6
        $this->storage = $storage;
30 6
        $this->revisions = $revisions;
31 6
    }
32
33
    /**
34
     * @param string $locale
35
     * @param string $domain
36
     * @return bool
37
     * @throws InvalidPathException
38
     */
39 6
    public function export(string $locale, string $domain): bool
40
    {
41 6
        $translations = $this->storage->getEnabledTranslated($locale, $domain);
42
43 6
        $revisionedDomain = null;
44 6
        if ($this->config->useRevisions()) {
45 2
            $revisionedDomain = $this->revisions->generateRevisionedDomain($domain, $translations);
46
        }
47
48 6
        $moPathname = $this->ensureDirectoryAndGetMoPathname($locale, $revisionedDomain ?: $domain);
49
50 5
        $wasSaved = Mo::toFile($translations, $moPathname);
51
52 5
        if ($wasSaved && $revisionedDomain) {
53 2
            $this->revisions->saveRevision($locale, $domain, $revisionedDomain);
54
        }
55
56 5
        return $wasSaved;
57
    }
58
59
    /**
60
     * @param string $locale
61
     * @param string $domain
62
     * @return string
63
     * @throws InvalidPathException
64
     */
65 6
    private function ensureDirectoryAndGetMoPathname(string $locale, string $domain): string
66
    {
67 6
        $baseDir = $this->config->getMoDirectory();
68 6
        $pathname = self::getMoPathname($baseDir, $locale, $domain);
69
70 6
        if (!is_dir($baseDir)) {
71 1
            throw new InvalidPathException("Directory '$baseDir' does not exist");
72
        }
73
74 5
        $localeDir = dirname($pathname);
75
76 5
        if (!is_dir($localeDir)) {
77
            // Create the {baseDir}/{locale}/LC_MESSAGES directory
78 5
            mkdir($localeDir, 0777, true);
79
        }
80
81 5
        return $pathname;
82
    }
83
84
    /**
85
     * Get full pathname to mo file
86
     *
87
     * @param string $moDirectory Base directory path for mo files
88
     * @param string $locale
89
     * @param string $domain
90
     * @return string Full pathname to mo file
91
     */
92 6
    public static function getMoPathname($moDirectory, $locale, $domain): string
93
    {
94 6
        return rtrim($moDirectory, '/') . '/' . $locale . '/LC_MESSAGES/' . $domain . '.mo';
95
    }
96
}