Completed
Push — master ( f576a1...b86af8 )
by Mārtiņš
01:55
created

MessageBuilder::removePreviousRevisionedDomain()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 3
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 10
    public function __construct(
24
        MessageConfigInterface $config,
25
        MessageStorage $storage,
26
        MessageRevisions $revisions
27
    ) {
28 10
        $this->config = $config;
29 10
        $this->storage = $storage;
30 10
        $this->revisions = $revisions;
31 10
    }
32
33
    /**
34
     * @param string $locale
35
     * @param string $domain
36
     * @return bool
37
     * @throws InvalidPathException
38
     */
39 8
    public function export(string $locale, string $domain): bool
40
    {
41 8
        $translations = $this->storage->getEnabledTranslated($locale, $domain);
42
43 8
        $revisionedDomain = null;
44 8
        if ($this->config->useRevisions()) {
45 4
            $revisionedDomain = $this->revisions->generateRevisionedDomain($domain, $translations);
46
        }
47
48 8
        $moPathname = $this->ensureDirectoryAndGetMoPathname($locale, $revisionedDomain ?: $domain);
49
50 7
        $wasSaved = Mo::toFile($translations, $moPathname);
51
52 7
        if ($wasSaved && $revisionedDomain) {
53 4
            $previousRevisionedDomain = $this->revisions->getRevisionedDomain($locale, $domain);
54
55 4
            $wasRevisionSaved = $this->revisions->saveRevision($locale, $domain, $revisionedDomain);
56
57 4
            if ($wasRevisionSaved) {
58 4
                $this->removePreviousRevisionedDomain($locale, $revisionedDomain, $previousRevisionedDomain);
59
            }
60
        }
61
62 7
        return $wasSaved;
63
    }
64
65
    /**
66
     * Remove previous domain file
67
     *
68
     * @param string $locale
69
     * @param string $currentDomain
70
     * @param string $previousDomain
71
     * @return bool True if cleanup was successful
72
     */
73 4
    private function removePreviousRevisionedDomain(
74
        string $locale,
75
        string $currentDomain,
76
        string $previousDomain
77
    ): bool {
78 4
        if ($currentDomain === $previousDomain) {
79
            // Same revisioned domain, no need to remove
80 1
            return true;
81
        }
82
83 4
        $previousPathname = self::getMoPathname($locale, $previousDomain);
0 ignored issues
show
Bug Best Practice introduced by
The method Printful\GettextCms\Mess...uilder::getMoPathname() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        /** @scrutinizer ignore-call */ 
84
        $previousPathname = self::getMoPathname($locale, $previousDomain);
Loading history...
84
85 4
        if (is_file($previousPathname)) {
86 3
            return @unlink($previousPathname);
87
        }
88
89 3
        return false;
90
    }
91
92
    /**
93
     * @param string $locale
94
     * @param string $domain
95
     * @return string
96
     * @throws InvalidPathException
97
     */
98 8
    private function ensureDirectoryAndGetMoPathname(string $locale, string $domain): string
99
    {
100 8
        $baseDir = rtrim($this->config->getMoDirectory(), '/');
101 8
        $pathname = $this->getMoPathname($locale, $domain);
102
103 8
        if (!is_dir($baseDir)) {
104 1
            throw new InvalidPathException("Directory '$baseDir' does not exist");
105
        }
106
107 7
        $localeDir = dirname($pathname);
108
109 7
        if (!is_dir($localeDir)) {
110
            // Create the {baseDir}/{locale}/LC_MESSAGES directory
111 7
            mkdir($localeDir, 0777, true);
112
        }
113
114 7
        return $pathname;
115
    }
116
117
    /**
118
     * Get full pathname to mo file
119
     *
120
     * @param string $locale
121
     * @param string $domain
122
     * @return string Full pathname to mo file
123
     */
124 8
    private function getMoPathname($locale, $domain): string
125
    {
126 8
        return rtrim($this->config->getMoDirectory(), '/') . '/' . $locale . '/LC_MESSAGES/' . $domain . '.mo';
127
    }
128
}