Completed
Push — master ( f71998...9528c4 )
by Eridan
04:07
created

AbstractTranslatorProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 66
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslator() 0 24 4
A getPreferredTranslator() 0 20 4
1
<?php
2
3
/* MIT License
4
5
Copyright (c) 2018 Eridan Domoratskiy
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
SOFTWARE. */
24
25
namespace MaintenanceScreen\TranslatorProvider;
26
27
use MaintenanceScreen\Translator;
28
29
/**
30
 * Abstract provider of Translator instances
31
 *
32
 * @author ProgMiner
33
 */
34
abstract class AbstractTranslatorProvider implements ITranslatorProvider {
35
36
    /**
37
     * Makes Translator for language.
38
     *
39
     * All arguments of {@see ITranslatorProvider::getTranslator}
40
     * will be received to this function.
41
     *
42
     * @param string $lang Language name
43
     *
44
     * @return Translator
45
     */
46
    protected abstract function _getTranslator(string $lang): Translator;
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 14
    public function getTranslator(string $lang): Translator {
52 14
        $lang = str_replace('-', '_', $lang);
53 14
        $args = func_get_args();
54
55 14
        $exception = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $exception is dead and can be removed.
Loading history...
56
        try {
57 14
            return call_user_func_array([$this, '_getTranslator'], $args);
58 4
        } catch (\Throwable $e) {
59 4
            $exception = $e;
60
        }
61
62
        // If $lang is includes country code, try call self only for language code
63 4
        if (false !== ($pos = strpos($lang, '_'))) {
64
            try {
65 2
                array_shift($args);
66 2
                array_unshift($args, substr($lang, 0, $pos));
67
68 2
                return call_user_func_array([$this, 'getTranslator'], $args);
69 2
            } catch (\Throwable $e) {
70
                // Uninformative exception
71
            }
72
        }
73
74 4
        throw $exception;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 8
    public function getPreferredTranslator(array $langs, ?string $defaultLang = null): Translator {
81 8
        if (!is_null($defaultLang)) {
82 8
            $langs[] = $defaultLang;
83
        }
84
85 8
        $args = func_get_args();
86 8
        array_shift($args);
87
88 8
        foreach ($langs as $lang) {
89
            try {
90 8
                array_shift($args);
91 8
                array_unshift($args, $lang);
92
93 8
                return call_user_func_array([$this, 'getTranslator'], $args);
94 2
            } catch (\Throwable $e) {
95
                // A lot of uninformative exceptions
96
            }
97
        }
98
99 2
        throw new \RuntimeException('No one language exists');
100
    }
101
}
102