Passed
Push — master ( 7f25ec...97f7f8 )
by Joël
02:26
created

Gettext   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 183
Duplicated Lines 13.11 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 24
loc 183
ccs 65
cts 65
cp 1
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A changeDomain() 0 13 1
A _bindTextDomainCodeSet() 0 13 2
A __construct() 0 15 1
B _setConfig() 0 16 5
A _bindTextDomain() 13 13 2
A _textDomain() 0 11 2
A _setLocale() 0 16 3
A _putEnv() 0 11 2
A _checkLocaleFile() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
// @codeCoverageIgnoreStart
3
defined('BASEPATH') || exit('No direct script access allowed');
4
// @codeCoverageIgnoreEnd
5
6
/**
7
 * Codeigniter PHP framework library class for dealing with gettext.
8
 *
9
 * @package     CodeIgniter
10
 * @subpackage  Libraries
11
 * @category    Language
12
 * @author      Joël Gaujard <[email protected]>
13
 * @author      Marko Martinović <[email protected]>
14
 * @link        https://github.com/joel-depiltech/codeigniter-gettext
15
 */
16
class Gettext
17
{
18
    /** @var string domain name match with file contains translation */
19
    private $_textDomain;
20
21
    /** @var string character encoding in which the messages are */
22
    private $_catalogCodeSet;
23
24
    /** @var string the path for a domain */
25
    private $_localeDir;
26
27
    /** @var string|array locale or array of locales */
28
    private $_locale;
29
30
    /** @var int constant specifying the category of the functions affected by the locale setting */
31
    private $_category;
32
33
    /**
34
     * Initialize Codeigniter PHP framework and get configuration
35
     *
36
     * @codeCoverageIgnore
37
     * @param array $config Override default configuration
38
     */
39
    public function __construct(array $config = array())
40
    {
41
        log_message('info', 'Gettext Library Class Initialized');
42
43
        $this->_setConfig($config);
44
45
        $this
46
            ->_bindTextDomainCodeSet()
47
            ->_bindTextDomain()
48
            ->_textDomain()
49
            ->_setLocale()
50
            ->_putEnv()
51
            ->_checkLocaleFile()
52
        ;
53
    }
54
55
    /**
56
     * Load a domain
57
     * @param string $domain
58
     */
59 3
    public function changeDomain($domain)
60
    {
61 3
        log_message('info', 'Gettext Library Class -> Change domain');
62
63 3
        $this->_textDomain = $domain;
64
65 3
        $this
66 3
            ->_bindTextDomainCodeSet()
67 3
            ->_bindTextDomain()
68 3
            ->_textDomain()
69 3
            ->_checkLocaleFile()
70
        ;
71 3
    }
72
73
    /**
74
     * Merge config as parameter and default config (config/gettext.php file)
75
     * @param array $config
76
     */
77 19
    private function _setConfig(array $config = array())
78
    {
79 19
        $this->_localeDir = isset($config['gettext_locale_dir'])
80 19
            ? $config['gettext_locale_dir'] : config_item('gettext_locale_dir');
81
82 19
        $this->_textDomain = isset($config['gettext_text_domain'])
83 19
            ? $config['gettext_text_domain'] : config_item('gettext_text_domain');
84
85 19
        $this->_catalogCodeSet = isset($config['gettext_catalog_codeset'])
86 19
            ? $config['gettext_catalog_codeset'] : config_item('gettext_catalog_codeset');
87
88 19
        $this->_locale = isset($config['gettext_locale'])
89 19
            ? $config['gettext_locale'] : config_item('gettext_locale');
90
91 19
        $this->_category = LC_ALL;
92 19
    }
93
94
    /**
95
     * Gettext catalog codeset
96
     * @return $this
97
     */
98 19
    private function _bindTextDomainCodeSet()
99
    {
100 19
        $isBindTextDomainCodeSet = bind_textdomain_codeset($this->_textDomain, $this->_catalogCodeSet);
101
102 19
        log_message(
103 19
            (is_string($isBindTextDomainCodeSet) ? 'info' : 'error'),
104
            'Gettext Library -> Bind ' .
105 19
            'text domain: ' . $this->_textDomain . ' - ' .
106 19
            'with code set: ' . $this->_catalogCodeSet
107 19
        );
108
109 19
        return $this;
110
    }
111
112
    /**
113
     * Path to gettext locales directory relative to APPPATH
114
     * @return $this
115
     */
116 19 View Code Duplication
    private function _bindTextDomain()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118 19
        $isBindTextDomain = bindtextdomain($this->_textDomain, APPPATH . $this->_localeDir);
119
120 19
        log_message(
121 19
            (is_string($isBindTextDomain) ? 'info' : 'error'),
122
            'Gettext Library -> Bind ' .
123 19
            'text domain: ' . $this->_textDomain . ' - ' .
124 19
            'with directory: ' . APPPATH . $this->_localeDir
125 19
        );
126
127 19
        return $this;
128
    }
129
130
    /**
131
     * Gettext domain
132
     * @return $this
133
     */
134 19
    private function _textDomain()
135
    {
136 19
        $isSetTextDomain = textdomain($this->_textDomain);
137
138 19
        log_message(
139 19
            (is_string($isSetTextDomain) ? 'info' : 'error'),
140 19
            'Gettext Library -> Set text domain: ' . $this->_textDomain
141 19
        );
142
143 19
        return $this;
144
    }
145
146
    /**
147
     * Gettext locale
148
     * @return $this
149
     */
150 19
    private function _setLocale()
151
    {
152 19
        $isSetLocale = setlocale($this->_category, $this->_locale);
153
154 19
        log_message(
155 19
            (is_string($isSetLocale) ? 'info' : 'error'),
156
            'Gettext Library -> ' .
157 19
            'Set locale: ' . (is_array($this->_locale) ? print_r($this->_locale, TRUE) : $this->_locale). ' ' .
158 19
            'for category: ' . $this->_category
159 19
        );
160
161
        // the new current locale, or FALSE if the locale is not implemented on your platform
162 19
        $this->_locale = $isSetLocale;
163
164 19
        return $this;
165
    }
166
167
    /**
168
     * Change environment language for CLI
169
     * @return $this
170
     */
171 19
    private function _putEnv()
172
    {
173 19
        $isPutEnv = putenv('LANGUAGE=' . $this->_locale);
174
175 19
        log_message(
176 19
            ($isPutEnv === TRUE ? 'info' : 'error'),
177 19
            'Gettext Library -> Set environment language: ' . $this->_locale
178 19
        );
179
180 19
        return $this;
181
    }
182
183
    /**
184
     * MO file exists for locale
185
     * @return $this
186
     */
187 19 View Code Duplication
    private function _checkLocaleFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189 19
        $file = APPPATH . $this->_localeDir . '/' . $this->_locale . '/LC_MESSAGES/' . $this->_textDomain . '.mo';
190
191 19
        log_message(
192 19
            (is_file($file) === TRUE ? 'info' : 'error'),
193
            'Gettext Library -> Check MO file exists: ' . $file
194 19
        );
195
196 19
        return $this;
197
    }
198
}
199
200
/* End of file Gettext.php */
201
/* Location: ./libraries/gettext.php */