Completed
Push — master ( 801e4f...e2a4d0 )
by Craig
07:35
created

LocaleApi   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getSupportedLocales() 0 24 5
A getSupportedLocaleNames() 0 10 2
A addLegacyLocales() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\SettingsModule\Api;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Intl\Intl;
16
17
class LocaleApi
18
{
19
    /**
20
     * Locales with translations present
21
     * @var array
22
     */
23
    private $supportedLocales = [];
24
25
    /**
26
     * Get array of supported locales
27
     *
28
     * @return array
29
     */
30
    public function getSupportedLocales()
31
    {
32
        if (empty($this->supportedLocales)) {
33
            $this->supportedLocales[] = 'en';
34
            $finder = new Finder();
35
            if (is_dir('app/Resources/translations')) {
36
                $files = $finder->files()
37
                    ->in(['app/Resources/translations'])
38
                    ->depth(0)
39
                    ->name('*.po')
40
                    ->notName('*.template.*');
41
                foreach ($files as $file) {
42
                    $fileName = $file->getBasename('.po');
43
                    list($domain, $locale) = explode('.', $fileName);
0 ignored issues
show
Unused Code introduced by
The assignment to $domain is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
44
                    if (!in_array($locale, $this->supportedLocales)) {
45
                        $this->supportedLocales[] = $locale;
46
                    }
47
                }
48
            }
49
            $this->addLegacyLocales(); // @deprecated remove at Core-2.0
0 ignored issues
show
Deprecated Code introduced by
The method Zikula\SettingsModule\Ap...Api::addLegacyLocales() has been deprecated with message: remove at Core-2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
50
        }
51
52
        return $this->supportedLocales;
53
    }
54
55
    /**
56
     * Get array of supported locales with their translated name
57
     *
58
     * @return array
59
     */
60
    public function getSupportedLocaleNames()
61
    {
62
        $locales = $this->getSupportedLocales();
63
        $namedLocales = [];
64
        foreach ($locales as $locale) {
65
            $namedLocales[Intl::getLanguageBundle()->getLanguageName($locale)] = $locale;
66
        }
67
68
        return $namedLocales;
69
    }
70
71
    /**
72
     * Read legacy locale.ini files and add those locales
73
     * @deprecated remove at Core-2.0
74
     */
75
    private function addLegacyLocales()
76
    {
77
        $legacyLocales = \ZLanguage::getInstalledLanguages();
78
        foreach ($legacyLocales as $locale) {
79
            if (!in_array($locale, $this->supportedLocales)) {
80
                $this->supportedLocales[] = $locale;
81
            }
82
        }
83
    }
84
}
85