Passed
Pull Request — main (#84)
by Andrey
196:26 queued 181:26
created

Locales::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Concerns\Logger;
6
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
7
use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException;
8
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade;
9
use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade;
10
use Helldar\LaravelLangPublisher\Facades\Reflection as ReflectionFacade;
11
use Helldar\Support\Facades\Helpers\Arr;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\LaravelLangPublisher\Support\Arr. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
13
use Helldar\Support\Facades\Helpers\Filesystem\File;
14
use Helldar\Support\Facades\Helpers\Str;
15
16
final class Locales
17
{
18
    use Logger;
0 ignored issues
show
Bug introduced by
The trait Helldar\LaravelLangPublisher\Concerns\Logger requires the property $output which is not provided by Helldar\LaravelLangPublisher\Support\Locales.
Loading history...
19
20
    /**
21
     * List of available locations.
22
     *
23
     * @param  bool  $all
24
     *
25
     * @return array
26
     */
27
    public function available(bool $all = false): array
28
    {
29
        $this->log('Getting list of available locations...');
30
31
        $locales = $this->all();
32
33
        return $all ? $locales : $this->filter($locales);
34
    }
35
36
    /**
37
     * List of installed locations.
38
     *
39
     * @return array
40
     */
41
    public function installed(): array
42
    {
43
        $this->log('Getting list of installed locations...');
44
45
        $json = $this->findJsonFiles();
46
        $php  = $this->findPhpFiles();
47
48
        $installed = ArrFacade::unique(array_merge($json, $php));
49
50
        $sorted = Arr::sort($installed);
51
52
        return array_values($sorted);
53
    }
54
55
    /**
56
     * Retrieving a list of protected locales.
57
     *
58
     * @return array
59
     */
60
    public function protects(): array
61
    {
62
        $this->log('Retrieving a list of protected locales...');
63
64
        return ArrFacade::unique([
65
            $this->getDefault(),
66
            $this->getFallback(),
67
        ]);
68
    }
69
70
    /**
71
     * Checks if a language pack is installed.
72
     *
73
     * @param  string  $locale
74
     *
75
     * @return bool
76
     */
77
    public function isAvailable(string $locale): bool
78
    {
79
        $this->log('Checks if a language pack is installed...');
80
81
        return in_array($locale, $this->available(true), true);
82
    }
83
84
    /**
85
     * The checked locale protecting.
86
     *
87
     * @param  string  $locale
88
     *
89
     * @return bool
90
     */
91
    public function isProtected(string $locale): bool
92
    {
93
        $this->log('The checked locale protecting...');
94
95
        return $locale === $this->getDefault() || $locale === $this->getFallback();
96
    }
97
98
    /**
99
     * Checks whether it is possible to install the language pack.
100
     *
101
     * @param  string  $locale
102
     *
103
     * @return bool
104
     */
105
    public function isInstalled(string $locale): bool
106
    {
107
        $this->log('Checks whether it is possible to install the language pack...');
108
109
        return in_array($locale, $this->installed(), true);
110
    }
111
112
    /**
113
     * Getting the default localization name.
114
     *
115
     * @return string
116
     */
117
    public function getDefault(): string
118
    {
119
        $this->log('Getting the default localization name...');
120
121
        return ConfigFacade::defaultLocale();
122
    }
123
124
    /**
125
     * Getting the fallback localization name.
126
     *
127
     * @return string
128
     */
129
    public function getFallback(): string
130
    {
131
        $this->log('Getting the fallback localization name...');
132
133
        return ConfigFacade::fallbackLocale();
134
    }
135
136
    /**
137
     * Checking for localization existence.
138
     *
139
     * @param  string  $locale
140
     */
141
    public function validate(string $locale): void
142
    {
143
        $this->log('Checking for localization existence: ' . $locale);
144
145
        if (! $this->isAvailable($locale)) {
146
            throw new SourceLocaleDoesntExistsException($locale);
147
        }
148
    }
149
150
    protected function all(): array
151
    {
152
        $this->log('Getting a list of all available localizations without filtering...');
153
154
        return array_values(ReflectionFacade::getConstants(LocalesList::class));
155
    }
156
157
    protected function filter(array $locales): array
158
    {
159
        $this->log('Filtering localizations...');
160
161
        $unique = ArrFacade::unique($locales);
162
        $ignore = ConfigFacade::ignores();
163
164
        return array_values(array_filter($unique, static function ($locale) use ($ignore) {
165
            return ! in_array($locale, $ignore, true);
166
        }));
167
    }
168
169
    protected function findJsonFiles(): array
170
    {
171
        $this->log('Getting a list of localizations from json files...');
172
173
        $files = File::names($this->resourcesPath());
174
175
        return Arr::map($files, static function ($filename) {
176
            return Str::before($filename, '.');
177
        });
178
    }
179
180
    protected function findPhpFiles(): array
181
    {
182
        $this->log('Getting a list of localizations from php files...');
183
184
        return Directory::names($this->resourcesPath());
185
    }
186
187
    protected function resourcesPath(): string
188
    {
189
        $this->log('Getting the path to application resources...');
190
191
        return ConfigFacade::resourcesPath();
192
    }
193
}
194