Passed
Pull Request — master (#403)
by Arman
03:09
created

Translator::loadFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\Libraries\Lang;
16
17
use Quantum\Libraries\Lang\Exceptions\LangException;
18
use Quantum\Config\Exceptions\ConfigException;
19
use Quantum\App\Exceptions\BaseException;
20
use Quantum\Di\Exceptions\DiException;
21
use Dflydev\DotAccessData\Data;
22
use ReflectionException;
23
24
/**
25
 * Class Translator
26
 * @package Quantum\Libraries\Lang
27
 */
28
class Translator
29
{
30
    protected $lang;
31
32
    /**
33
     * @var Data|null
34
     */
35
    private $translations = null;
36
37
    /**
38
     * @param string $lang
39
     */
40
    public function __construct(string $lang)
41
    {
42
        $this->lang = $lang;
43
    }
44
45
    /**
46
     * Load translation files
47
     * @throws BaseException
48
     * @throws ConfigException
49
     * @throws DiException
50
     * @throws LangException
51
     * @throws ReflectionException
52
     */
53
    public function loadTranslations(): void
54
    {
55
        if ($this->translations !== null) {
56
            return;
57
        }
58
59
        $this->translations = new Data();
60
        $loaded = false;
61
62
        $sharedDir = base_dir() . DS . 'shared' . DS . 'resources' . DS . 'lang' . DS . $this->lang;
63
        $sharedFiles = fs()->glob($sharedDir . DS . '*.php');
64
65
        if (is_array($sharedFiles) && count($sharedFiles)) {
66
            $this->loadFiles($sharedFiles);
67
            $loaded = true;
68
        }
69
70
        $moduleDir = modules_dir() . DS . current_module() . DS . 'resources' . DS . 'lang' . DS . $this->lang;
71
        $moduleFiles = fs()->glob($moduleDir . DS . '*.php');
72
73
        if (is_array($moduleFiles) && count($moduleFiles)) {
74
            $this->loadFiles($moduleFiles);
75
            $loaded = true;
76
        }
77
78
        if (!$loaded) {
79
            throw LangException::translationsNotFound();
80
        }
81
    }
82
83
    /**
84
     * Load translations
85
     * @param array $files
86
     * @throws BaseException
87
     * @throws ConfigException
88
     * @throws DiException
89
     * @throws ReflectionException
90
     */
91
    private function loadFiles(array $files): void
92
    {
93
        foreach ($files as $file) {
94
            $fileName = fs()->fileName($file);
95
96
            $this->translations->import([
0 ignored issues
show
Bug introduced by
The method import() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            $this->translations->/** @scrutinizer ignore-call */ 
97
                                 import([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
                $fileName => fs()->require($file),
0 ignored issues
show
Unused Code introduced by
The call to Quantum\Libraries\Storage\FileSystem::require() has too many arguments starting with $file. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
                $fileName => fs()->/** @scrutinizer ignore-call */ require($file),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
98
            ]);
99
        }
100
    }
101
102
    /**
103
     * Get translation by key
104
     * @param string $key
105
     * @param array|string|null $params
106
     * @return string
107
     */
108
    public function get(string $key, $params = null): string
109
    {
110
        if ($this->translations && $this->translations->has($key)) {
111
            $message = $this->translations->get($key);
112
            return $params ? _message($message, $params) : $message;
113
        }
114
115
        return $key;
116
    }
117
118
    /**
119
     * Reset translations
120
     */
121
    public function flush(): void
122
    {
123
        $this->translations = null;
124
    }
125
}
126