Passed
Push — main ( 55f929...127e84 )
by Andrey
161:41 queued 158:10
created

Locales   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 176
ccs 50
cts 58
cp 0.8621
rs 10
wmc 17

14 Methods

Rating   Name   Duplication   Size   Complexity  
A findJsonFiles() 0 8 1
A findPhpFiles() 0 5 1
A available() 0 7 2
A validate() 0 6 2
A isAvailable() 0 5 1
A getFallback() 0 5 1
A all() 0 5 1
A resourcesPath() 0 5 1
A getDefault() 0 5 1
A installed() 0 12 1
A isInstalled() 0 5 1
A protects() 0 7 1
A filter() 0 9 1
A isProtected() 0 5 2
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 16
    public function available(bool $all = false): array
28
    {
29 16
        $this->log('Getting list of available locations...');
30
31 16
        $locales = $this->all();
32
33 16
        return $all ? $locales : $this->filter($locales);
34
    }
35
36
    /**
37
     * List of installed locations.
38
     *
39
     * @return array
40
     */
41 7
    public function installed(): array
42
    {
43 7
        $this->log('Getting list of installed locations...');
44
45 7
        $json = $this->findJsonFiles();
46 7
        $php  = $this->findPhpFiles();
47
48 7
        $installed = ArrFacade::unique(array_merge($json, $php));
49
50 7
        $sorted = Arr::sort($installed);
51
52 7
        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 13
    public function isAvailable(string $locale): bool
78
    {
79 13
        $this->log('Checks if a language pack is installed...');
80
81 13
        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 4
    public function isProtected(string $locale): bool
92
    {
93 4
        $this->log('The checked locale protecting...');
94
95 4
        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 4
    public function getDefault(): string
118
    {
119 4
        $this->log('Getting the default localization name...');
120
121 4
        return ConfigFacade::defaultLocale();
122
    }
123
124
    /**
125
     * Getting the fallback localization name.
126
     *
127
     * @return string
128
     */
129 3
    public function getFallback(): string
130
    {
131 3
        $this->log('Getting the fallback localization name...');
132
133 3
        return ConfigFacade::fallbackLocale();
134
    }
135
136
    /**
137
     * Checking for localization existence.
138
     *
139
     * @param  string  $locale
140
     */
141 13
    public function validate(string $locale): void
142
    {
143 13
        $this->log('Checking for localization existence: ' . $locale);
144
145 13
        if (! $this->isAvailable($locale)) {
146 1
            throw new SourceLocaleDoesntExistsException($locale);
147
        }
148 12
    }
149
150 16
    protected function all(): array
151
    {
152 16
        $this->log('Getting a list of all available localizations without filtering...');
153
154 16
        return array_values(ReflectionFacade::getConstants(LocalesList::class));
155
    }
156
157 10
    protected function filter(array $locales): array
158
    {
159 10
        $this->log('Filtering localizations...');
160
161 10
        $unique = ArrFacade::unique($locales);
162 10
        $ignore = ConfigFacade::ignores();
163
164 10
        return array_values(array_filter($unique, static function ($locale) use ($ignore) {
165 10
            return ! in_array($locale, $ignore, true);
166 10
        }));
167
    }
168
169 7
    protected function findJsonFiles(): array
170
    {
171 7
        $this->log('Getting a list of localizations from json files...');
172
173 7
        $files = File::names($this->resourcesPath());
174
175 7
        return Arr::map($files, static function ($filename) {
176 7
            return Str::before($filename, '.');
177 7
        });
178
    }
179
180 7
    protected function findPhpFiles(): array
181
    {
182 7
        $this->log('Getting a list of localizations from php files...');
183
184 7
        return Directory::names($this->resourcesPath());
185
    }
186
187 7
    protected function resourcesPath(): string
188
    {
189 7
        $this->log('Getting the path to application resources...');
190
191 7
        return ConfigFacade::resourcesPath();
192
    }
193
}
194