Passed
Push — 10.x ( e0b1ce...c0ee15 )
by Andrey
14:47
created

Locales::in()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
6
use Helldar\LaravelLangPublisher\Facades\Config as ConfigSupport;
7
use Helldar\LaravelLangPublisher\Facades\Path as PathSupport;
8
use Helldar\LaravelLangPublisher\Facades\Reflection as ReflectionSupport;
9
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
10
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
11
use Helldar\Support\Facades\Helpers\Filesystem\File;
12
use Helldar\Support\Facades\Helpers\Str;
13
14
final class Locales
15
{
16
    /**
17
     * List of available locations.
18
     *
19
     * @return array
20
     */
21
    public function available(): array
22
    {
23
        $locales = $this->all();
24
25
        return $this->filter($locales);
26 19
    }
27
28 19
    /**
29
     * List of installed locations.
30 19
     *
31
     * @return array
32 19
     */
33
    public function installed(): array
34
    {
35
        return Arrayable::of()
36
            ->merge($this->findJson(), $this->findPhp())
37
            ->unique()
38
            ->sort()
39
            ->values()
40 9
            ->get();
41
    }
42 9
43
    /**
44 9
     * Retrieving a list of protected locales.
45 9
     *
46
     * @return array
47 9
     */
48
    public function protects(): array
49 9
    {
50
        return Arrayable::of([
51 9
            $this->getDefault(),
52
            $this->getFallback(),
53
        ])
54
            ->unique()
55
            ->get();
56
    }
57
58
    /**
59 1
     * Getting a complete list of available localizations.
60
     *
61 1
     * @return array
62
     */
63 1
    public function all(): array
64 1
    {
65 1
        $locales = ReflectionSupport::getConstants(LocalesList::class);
66
67
        return Arrayable::of($locales)
68
            ->sort()
69
            ->values()
70
            ->get();
71
    }
72
73
    /**
74
     * Checks if a localization is available.
75
     *
76 16
     * @param  string  $locale
77
     *
78 16
     * @return bool
79
     */
80 16
    public function isAvailable(string $locale): bool
81
    {
82
        return $this->in($locale, $this->all());
83
    }
84
85
    /**
86
     * Checks if a localization is protected.
87
     *
88
     * @param  string  $locale
89
     *
90 4
     * @return bool
91
     */
92 4
    public function isProtected(string $locale): bool
93
    {
94 4
        return $this->in($locale, $this->protects());
95
    }
96
97
    /**
98
     * Checks if a localization is installed.
99
     *
100
     * @param  string  $locale
101
     *
102
     * @return bool
103
     */
104 1
    public function isInstalled(string $locale): bool
105
    {
106 1
        return $this->in($locale, $this->installed());
107
    }
108 1
109
    /**
110
     * Getting the default localization name.
111
     *
112
     * @return string
113
     */
114
    public function getDefault(): string
115
    {
116 5
        return ConfigSupport::defaultLocale();
117
    }
118 5
119
    /**
120 5
     * Getting the fallback localization name.
121
     *
122
     * @return string
123
     */
124
    public function getFallback(): string
125
    {
126
        return ConfigSupport::fallbackLocale();
127
    }
128 4
129
    protected function filter(array $locales): array
130 4
    {
131
        $ignores = ConfigSupport::ignores();
132 4
133
        return Arrayable::of($locales)
134
            ->unique()
135 19
            ->filter(function ($locale) use ($ignores) {
136
                return ! $this->in($locale, $ignores);
137 19
            })
138
            ->values()
139 19
            ->get();
140
    }
141
142 13
    protected function in(string $locale, array $locales): bool
143
    {
144 13
        return in_array($locale, $locales, true);
145
    }
146 13
147 13
    protected function findJson(): array
148
    {
149 13
        $files = File::names($this->resourcesPath(), null, true);
150 13
151 13
        return Arrayable::of($files)
152
            ->filter(static function (string $filename) {
153
                $extension = PathSupport::extension($filename);
154 9
155
                return Str::lower($extension) === 'json';
156 9
            })
157
            ->map(static function (string $filename) {
158 9
                return PathSupport::filename($filename);
159
            })
160 9
            ->get();
161 9
    }
162 9
163
    protected function findPhp(): array
164
    {
165 9
        return Directory::names($this->resourcesPath(), function ($name) {
166
            return $this->isAvailable($name);
167 9
        });
168
    }
169 9
170
    protected function resourcesPath(): string
171
    {
172 9
        return ConfigSupport::resources();
173
    }
174
}
175