Completed
Pull Request — master (#33)
by ARCANEDEV
14:20
created

TransManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 227
rs 10
c 0
b 0
f 0
ccs 47
cts 47
cp 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getPaths() 0 4 1
A load() 0 6 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 = ['script', 'vendor'];
52
53
    /* -----------------------------------------------------------------
54
     |  Constructor
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59 40
     * Make TransManager instance.
60
     *
61 40
     * @param  \Illuminate\Filesystem\Filesystem  $filesystem
62 40
     * @param  array                              $paths
63
     */
64 40
    public function __construct(Filesystem $filesystem, array $paths)
65 40
    {
66
        $this->filesystem = $filesystem;
67
        $this->paths      = $paths;
68
69
        $this->load();
70
    }
71
72
    /* -----------------------------------------------------------------
73
     |  Getters & Setters
74
     | -----------------------------------------------------------------
75
     */
76
77 40
    /**
78
     * Get the translation paths.
79 40
     *
80
     * @return array
81
     */
82
    public function getPaths(): array
83
    {
84
        return $this->paths;
85
    }
86
87
    /* -----------------------------------------------------------------
88
     |  Main Methods
89
     | -----------------------------------------------------------------
90 40
     */
91
92 40
    /**
93 40
     * Load lang files.
94
     */
95 40
    private function load(): void
96
    {
97
        foreach ($this->getPaths() as $group => $path) {
98
            $this->locales[$group] = $this->loadDirectories($path);
99
        }
100
    }
101
102
    /**
103
     * Load directories.
104 40
     *
105
     * @param  string  $dirPath
106 40
     *
107
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection
108 40
     */
109 40
    private function loadDirectories($dirPath): LocaleCollection
110
    {
111 40
        $locales = new LocaleCollection;
112 40
113 40
        foreach ($this->filesystem->directories($dirPath) as $path) {
114
            if ($this->isExcluded($path)) {
115
                continue;
116
            }
117
118 40
            $locales->addOne(
119
                new Locale(basename($path), $path, $this->loadLocaleFiles($path))
120
            );
121
        }
122
123
        return $locales;
124
    }
125
126
    /**
127
     * Load the locale translation files.
128 40
     *
129
     * @param  string  $path
130 40
     *
131
     * @return array
132 40
     */
133
    private function loadLocaleFiles(string $path): array
134 40
    {
135 40
        $files = [];
136
137
        foreach ($this->filesystem->allFiles($path) as $file) {
138 40
            /** @var \Symfony\Component\Finder\SplFileInfo $file */
139 40
            $key = str_replace(
140 40
                ['.php', DS], ['', '.'], $file->getRelativePathname()
141
            );
142
143
            $files[$key] = [
144 40
                'path'    => $file->getRealPath(),
145
                'content' => $this->filesystem->getRequire($file),
146
            ];
147
        }
148
149
        return $files;
150
    }
151
152
    /**
153
     * Get locale collection by group location.
154
     *
155 30
     * @param  string      $group
156
     * @param  mixed|null  $default
157 30
     *
158
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection|null
159
     */
160
    public function getCollection(string $group, $default = null): ?LocaleCollection
161
    {
162
        return Arr::get($this->locales, $group, $default);
163
    }
164
165
    /**
166
     * Get a locale translations from a group.
167
     *
168
     * @param  string  $group
169 6
     * @param  string  $locale
170
     * @param  null    $default
171 6
     *
172 2
     * @return \Arcanedev\LaravelLang\Entities\Locale|null
173
     */
174 4
    public function getFrom(string $group, string $locale, $default = null): ?Locale
175
    {
176 4
        if ( ! $this->hasCollection($group)) {
177
            return $default;
178
        }
179
180
        $locales = $this->getCollection($group);
181
182
        return $locales->get($locale, $default);
183
    }
184 4
185
    /**
186
     * Get locale keys.
187 4
     *
188 4
     * @return array
189 4
     */
190
    public function keys(): array
191 4
    {
192
        $locales = array_map(function (LocaleCollection $locales) {
193 4
            $keys = $locales->keys()->toArray();
194 4
            return array_combine($keys, $keys);
195
        }, $this->locales);
196
197 4
        $all = [];
198
199
        foreach ($locales as $keys) {
200
            $all = array_merge($all, $keys);
201
        }
202
203
        return $all;
204
    }
205 2
206
    /**
207 2
     * Get locales count.
208
     *
209
     * @return int
210
     */
211
    public function count(): int
212
    {
213
        return count($this->keys());
214
    }
215
216
    /* -----------------------------------------------------------------
217
     |  Check Methods
218
     | -----------------------------------------------------------------
219
     */
220
221
    /**
222 8
     * Check if a translation group exists.
223
     *
224 8
     * @param  string  $group
225
     *
226
     * @return bool
227
     */
228
    public function hasCollection(string $group): bool
229
    {
230
        return Arr::has($this->locales, $group);
231
    }
232
233
    /**
234
     * Check if the given path is excluded.
235
     *
236
     * @param  string  $path
237
     *
238
     * @return bool
239
     */
240
    private function isExcluded(string $path): bool
241
    {
242
        return in_array($key = basename($path), $this->excludedFolders);
243
    }
244
}
245