Completed
Push — develop ( 1bc5bd...8e829c )
by Neomerx
02:13
created

L10nSettings   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 46
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 18 2
A getSettings() 0 6 1
1
<?php namespace Limoncello\Application\Packages\L10n;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Settings\SettingsInterface;
20
use Limoncello\l10n\Messages\FileBundleEncoder;
21
22
/**
23
 * @package Limoncello\Application
24
 */
25
abstract class L10nSettings implements SettingsInterface
26
{
27
    /** Settings key */
28
    const KEY_DEFAULT_LOCALE = 0;
29
30
    /** Settings key */
31
    const KEY_LOCALES_FOLDER = self::KEY_DEFAULT_LOCALE + 1;
32
33
    /** Settings key */
34
    const KEY_LOCALES_DATA = self::KEY_LOCALES_FOLDER + 1;
35
36
    /** Settings key */
37
    const KEY_LAST = self::KEY_LOCALES_DATA;
38
39
    /**
40
     * @inheritdoc
41
     */
42 1
    final public function get(): array
43
    {
44 1
        $defaults = $this->getSettings();
45
46 1
        $defaultLocale = $defaults[static::KEY_DEFAULT_LOCALE] ?? null;
47 1
        assert(empty($defaultLocale) === false, "Invalid default locale `$defaultLocale`.");
48
49 1
        $localesFolder = $defaults[static::KEY_LOCALES_FOLDER] ?? null;
50 1
        assert(
51 1
            $localesFolder !== null && empty(glob($localesFolder)) === false,
52 1
            "Invalid Locales folder `$localesFolder`."
53
        );
54
55
56
        return $defaults + [
57 1
                static::KEY_LOCALES_DATA => (new FileBundleEncoder($localesFolder))->getStorageData($defaultLocale),
58
            ];
59
    }
60
61
    /**
62
     * @return array
63
     */
64 1
    protected function getSettings(): array
65
    {
66
        return [
67 1
            static::KEY_DEFAULT_LOCALE => 'en',
68
        ];
69
    }
70
}
71