Passed
Pull Request — main (#123)
by Andrey
27:30 queued 12:28
created

Locales   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
eloc 37
c 3
b 0
f 0
dl 0
loc 90
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A in() 0 3 1
A available() 0 9 1
A protects() 0 6 1
A getFallback() 0 3 1
A isAvailable() 0 3 1
A isInstalled() 0 3 1
A findJson() 0 13 1
A resources() 0 3 1
A getDefault() 0 3 2
A findPhp() 0 3 1
A isProtected() 0 3 1
A installed() 0 11 1
1
<?php
2
3
/*
4
 * This file is part of the "andrey-helldar/laravel-lang-publisher" project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author Andrey Helldar <[email protected]>
10
 *
11
 * @copyright 2021 Andrey Helldar
12
 *
13
 * @license MIT
14
 *
15
 * @see https://github.com/andrey-helldar/laravel-lang-publisher
16
 */
17
18
declare(strict_types=1);
19
20
namespace Helldar\LaravelLangPublisher\Helpers;
21
22
use Helldar\LaravelLangPublisher\Concerns\Has;
23
use Helldar\LaravelLangPublisher\Concerns\Paths;
24
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
25
use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigHelper;
26
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
27
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
28
use Helldar\Support\Facades\Helpers\Filesystem\File;
29
use Helldar\Support\Facades\Helpers\Reflection;
30
use Illuminate\Support\Facades\Config as Illuminate;
31
32
class Locales
33
{
34
    use Has;
35
    use Paths;
36
37
    public function available(): array
38
    {
39
        $locales = Reflection::getConstants(LocalesList::class);
40
41
        return Arrayable::of($locales)
42
            ->unique()
43
            ->sort()
44
            ->values()
45
            ->get();
46
    }
47
48
    public function installed(): array
49
    {
50
        return Arrayable::of($this->findJson())
51
            ->addUnique($this->findPhp())
52
            ->filter(function (string $locale) {
53
                return $this->isAvailable($locale);
54
            })
55
            ->unique()
56
            ->sort()
57
            ->values()
58
            ->get();
59
    }
60
61
    public function protects(): array
62
    {
63
        return Arrayable::of([
64
            $this->getDefault(),
65
            $this->getFallback(),
66
        ])->unique()->get();
67
    }
68
69
    public function isAvailable(string $locale): bool
70
    {
71
        return $this->in($locale, $this->available());
72
    }
73
74
    public function isProtected(string $locale): bool
75
    {
76
        return $this->in($locale, $this->protects());
77
    }
78
79
    public function isInstalled(string $locale): bool
80
    {
81
        return $this->in($locale, $this->installed());
82
    }
83
84
    public function getDefault(): string
85
    {
86
        return Illuminate::get('app.locale') ?: $this->getFallback();
87
    }
88
89
    public function getFallback(): string
90
    {
91
        return Illuminate::get('app.fallback_locale', LocalesList::ENGLISH);
92
    }
93
94
    protected function in(string $locale, array $locales): bool
95
    {
96
        return in_array($locale, $locales, true);
97
    }
98
99
    protected function findJson(): array
100
    {
101
        $files = File::names($this->resources(), null, true);
102
103
        return Arrayable::of($files)
104
            ->filter(function (string $filename) {
105
                return $this->hasJson($filename);
106
            })
107
            ->map(function (string $filename) {
108
                return $this->filename($filename);
109
            })
110
            ->values()
111
            ->get();
112
    }
113
114
    protected function findPhp(): array
115
    {
116
        return Directory::names($this->resources());
117
    }
118
119
    protected function resources(): string
120
    {
121
        return ConfigHelper::resources();
122
    }
123
}
124