Passed
Push — master ( c87b5f...863f16 )
by Mārtiņš
02:35
created

LocaleLoader::bindDomains()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 13
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use Printful\GettextCms\Exceptions\UnsupportedLocaleException;
6
use Printful\GettextCms\Interfaces\MessageConfigInterface;
7
8
/**
9
 * Class sets locales and binds domains for current request
10
 */
11
class LocaleLoader
12
{
13
    /** @var MessageConfigInterface */
14
    private $config;
15
16
    /** @var MessageRevisions */
17
    private $revisions;
18
19 1
    public function __construct(MessageConfigInterface $config)
20
    {
21 1
        $this->revisions = new MessageRevisions($config);
22 1
        $this->config = $config;
23 1
    }
24
25
    /**
26
     * Globally set current locale and bind gettext domains
27
     *
28
     * @param string $locale
29
     * @return bool If false is returned, some domains may not have been bound and will fail
30
     * @throws UnsupportedLocaleException
31
     */
32 1
    public function load(string $locale): bool
33
    {
34 1
        putenv("LANG=" . $locale);
35
36 1
        if (!setlocale(LC_ALL, $locale)) {
37 1
            throw new UnsupportedLocaleException('Locale is not supported: ' . $locale);
38
        }
39
40
        return $this->bindDomains($locale);
41
    }
42
43
    /**
44
     * Bind all domains for locale
45
     *
46
     * @param string $locale
47
     * @return bool Returns false if binding fails, translations may not work correctly
48
     */
49
    private function bindDomains(string $locale): bool
50
    {
51
        $domainBound = true;
52
53
        $domainDir = rtrim($this->config->getMoDirectory(), '/');
54
55
        $defaultDomain = $this->config->getDefaultDomain();
56
57
        $allDomains = $this->config->getOtherDomains();
58
        $allDomains[] = $defaultDomain;
59
60
        foreach ($allDomains as $domain) {
61
            $actualDomain = $this->getActualDomain($locale, $domain);
62
63
            // File structure for a translation is: mo-directory/{locale}/LC_MESSAGES/{domain}.mo
64
            if (!bindtextdomain($actualDomain, $domainDir)) {
65
                // If text domain binding fails, something is wrong with the paths
66
                $domainBound = false;
67
            }
68
69
            bind_textdomain_codeset($actualDomain, 'utf8');
70
        }
71
72
        // Bind the default domain for _() calls (and other non-domain specific calls)
73
        textdomain($this->getActualDomain($locale, $defaultDomain));
74
75
        return $domainBound;
76
    }
77
78
    /**
79
     * Get actual domain name. If revisions are enabled, this will return something like "domain_xxxxxx"
80
     *
81
     * @param string $locale
82
     * @param string $domain
83
     * @return string
84
     */
85
    private function getActualDomain(string $locale, string $domain): string
86
    {
87
        if ($this->config->useRevisions()) {
88
            return $this->revisions->getRevisionedDomain($locale, $domain);
89
        }
90
91
        return $domain;
92
    }
93
}