Completed
Pull Request — master (#38)
by ARCANEDEV
11:00
created

TransManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 250
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getPaths() 0 4 1
A load() 0 8 3
A loadJsonDirectory() 0 12 2
A loadDirectories() 0 16 3
A loadLocaleFiles() 0 18 2
A getCollection() 0 4 1
A getFrom() 0 10 2
A keys() 0 15 2
A count() 0 4 1
A hasCollection() 0 4 1
A isExcluded() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelLang;
6
7
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerContract;
8
use Arcanedev\LaravelLang\Entities\{Locale, LocaleCollection};
9
use Illuminate\Filesystem\Filesystem;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class     TransManager
14
 *
15
 * @package  Arcanedev\LaravelLang
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
class TransManager implements TransManagerContract
19
{
20
    /* -----------------------------------------------------------------
21
     |  Properties
22
     | -----------------------------------------------------------------
23
     */
24
25
    /**
26
     * Lang directories paths.
27
     *
28
     * @var array
29
     */
30
    private $paths   = [];
31
32
    /**
33
     * Translations collection.
34
     *
35
     * @var array
36
     */
37
    private $locales = [];
38
39
    /**
40
     * The filesystem instance.
41
     *
42
     * @var \Illuminate\Filesystem\Filesystem
43
     */
44
    private $filesystem;
45
46
    /**
47
     * Excluded folders.
48
     *
49
     * @var array
50
     */
51
    private $excludedFolders = [
52
        'script',
53
        'vendor',
54
    ];
55
56
    /* -----------------------------------------------------------------
57
     |  Constructor
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Make TransManager instance.
63
     *
64 126
     * @param  \Illuminate\Filesystem\Filesystem  $filesystem
65
     * @param  array                              $paths
66 126
     */
67 126
    public function __construct(Filesystem $filesystem, array $paths)
68
    {
69 126
        $this->filesystem = $filesystem;
70 126
        $this->paths      = $paths;
71
72
        $this->load();
73
    }
74
75
    /* -----------------------------------------------------------------
76
     |  Getters & Setters
77
     | -----------------------------------------------------------------
78
     */
79
80
    /**
81
     * Get the translation paths.
82 126
     *
83
     * @return array
84 126
     */
85
    public function getPaths(): array
86
    {
87
        return $this->paths;
88
    }
89
90
    /* -----------------------------------------------------------------
91
     |  Main Methods
92
     | -----------------------------------------------------------------
93
     */
94
95 126
    /**
96
     * Load lang files.
97 126
     */
98 126
    private function load(): void
99
    {
100 126
        foreach ($this->getPaths() as $group => $path) {
101
            $this->locales[$group] = $group === 'vendor-json'
102
                ? $this->loadJsonDirectory($path)
103
                : $this->loadDirectories($path);
104
        }
105
    }
106
107
    /**
108
     * @param  string  $path
109 126
     *
110
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection
111 126
     */
112
    private function loadJsonDirectory(string $path): LocaleCollection
113 126
    {
114 126
        $locales = new LocaleCollection;
115 126
116
        foreach ($this->filesystem->files($path) as $file) {
117
            $locales->addOne(
118 126
                new Locale($file->getBasename('.json'), $file->getRealPath())
119 126
            );
120
        }
121
122
        return $locales;
123 126
    }
124
125
    /**
126
     * Load directories.
127
     *
128
     * @param  string  $dirPath
129
     *
130
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection
131
     */
132
    private function loadDirectories(string $dirPath): LocaleCollection
133 126
    {
134
        $locales = new LocaleCollection;
135 126
136
        foreach ($this->filesystem->directories($dirPath) as $path) {
137 126
            if ($this->isExcluded($path)) {
138
                continue;
139 126
            }
140 126
141
            $locales->addOne(
142
                new Locale(basename($path), $path, $this->loadLocaleFiles($path))
143 126
            );
144 126
        }
145 126
146
        return $locales;
147
    }
148
149 126
    /**
150
     * Load the locale translation files.
151
     *
152
     * @param  string  $path
153
     *
154
     * @return array
155
     */
156
    private function loadLocaleFiles(string $path): array
157
    {
158
        $files = [];
159
160 96
        foreach ($this->filesystem->allFiles($path) as $file) {
161
            /** @var \Symfony\Component\Finder\SplFileInfo $file */
162 96
            $key = str_replace(
163
                ['.php', DIRECTORY_SEPARATOR], ['', '.'], $file->getRelativePathname()
164
            );
165
166
            $files[$key] = [
167
                'path'    => $file->getRealPath(),
168
                'content' => $this->filesystem->getRequire($file),
169
            ];
170
        }
171
172
        return $files;
173
    }
174 18
175
    /**
176 18
     * Get locale collection by group location.
177 6
     *
178
     * @param  string      $group
179
     * @param  mixed|null  $default
180 12
     *
181
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection|null
182 12
     */
183
    public function getCollection(string $group, $default = null): ?LocaleCollection
184
    {
185
        return Arr::get($this->locales, $group, $default);
186
    }
187
188
    /**
189
     * Get a locale translations from a group.
190 12
     *
191
     * @param  string  $group
192
     * @param  string  $locale
193 12
     * @param  null    $default
194 12
     *
195 12
     * @return \Arcanedev\LaravelLang\Entities\Locale|null
196
     */
197 12
    public function getFrom(string $group, string $locale, $default = null): ?Locale
198
    {
199 12
        if ( ! $this->hasCollection($group)) {
200 12
            return $default;
201
        }
202
203 12
        $locales = $this->getCollection($group);
204
205
        return $locales->get($locale, $default);
206
    }
207
208
    /**
209
     * Get locale keys.
210
     *
211 6
     * @return array
212
     */
213 6
    public function keys(): array
214
    {
215
        $locales = array_map(function (LocaleCollection $locales) {
216
            $keys = $locales->keys()->toArray();
217
            return array_combine($keys, $keys);
218
        }, $this->locales);
219
220
        $all = [];
221
222
        foreach ($locales as $keys) {
223
            $all = array_merge($all, $keys);
224
        }
225
226
        return $all;
227
    }
228 24
229
    /**
230 24
     * Get locales count.
231
     *
232
     * @return int
233
     */
234
    public function count(): int
235
    {
236
        return count($this->keys());
237
    }
238
239
    /* -----------------------------------------------------------------
240 126
     |  Check Methods
241
     | -----------------------------------------------------------------
242 126
     */
243
244
    /**
245
     * Check if a translation group exists.
246
     *
247
     * @param  string  $group
248
     *
249
     * @return bool
250
     */
251
    public function hasCollection(string $group): bool
252
    {
253
        return Arr::has($this->locales, $group);
254
    }
255
256
    /**
257
     * Check if the given path is excluded.
258
     *
259
     * @param  string  $path
260
     *
261
     * @return bool
262
     */
263
    private function isExcluded(string $path): bool
264
    {
265
        return in_array($key = basename($path), $this->excludedFolders);
266
    }
267
}
268