Passed
Pull Request — main (#123)
by Andrey
33:51 queued 18:50
created

Locales::resources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\Exceptions\SourceLocaleDoesntExistsException;
26
use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigHelper;
27
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
28
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
29
use Helldar\Support\Facades\Helpers\Filesystem\File;
30
use Helldar\Support\Facades\Helpers\Reflection;
31
use Illuminate\Support\Facades\Config as Illuminate;
32
33
class Locales
34
{
35
    use Has;
36
    use Paths;
37
38
    public function available(): array
39
    {
40
        $locales = Reflection::getConstants(LocalesList::class);
41
42
        return Arrayable::of($locales)
43
            ->unique()
44
            ->sort()
45
            ->values()
46
            ->get();
47
    }
48
49
    public function installed(): array
50
    {
51
        return Arrayable::of($this->findJson())
52
            ->addUnique($this->findPhp())
53
            ->filter(function (string $locale) {
54
                return $this->isAvailable($locale);
55
            })
56
            ->unique()
57
            ->sort()
58
            ->values()
59
            ->get();
60
    }
61
62
    public function protects(): array
63
    {
64
        return Arrayable::of([
65
            $this->getDefault(),
66
            $this->getFallback(),
67
        ])->unique()->get();
68
    }
69
70
    public function isAvailable(string $locale): bool
71
    {
72
        return $this->in($locale, $this->available());
73
    }
74
75
    public function isProtected(string $locale): bool
76
    {
77
        return $this->in($locale, $this->protects());
78
    }
79
80
    public function isInstalled(string $locale): bool
81
    {
82
        return $this->in($locale, $this->installed());
83
    }
84
85
    public function getDefault(): string
86
    {
87
        return Illuminate::get('app.locale') ?: $this->getFallback();
88
    }
89
90
    public function getFallback(): string
91
    {
92
        return Illuminate::get('app.fallback_locale', LocalesList::ENGLISH);
93
    }
94
95
    public function validate(string $locale): void
96
    {
97
        if (! $this->isAvailable($locale)) {
98
            throw new SourceLocaleDoesntExistsException($locale);
99
        }
100
    }
101
102
    protected function in(string $locale, array $locales): bool
103
    {
104
        return in_array($locale, $locales, true);
105
    }
106
107
    protected function findJson(): array
108
    {
109
        $files = File::names($this->resources(), null, true);
110
111
        return Arrayable::of($files)
112
            ->filter(function (string $filename) {
113
                return $this->hasJson($filename);
114
            })
115
            ->map(function (string $filename) {
116
                return $this->filename($filename);
117
            })
118
            ->values()
119
            ->get();
120
    }
121
122
    protected function findPhp(): array
123
    {
124
        return Directory::names($this->resources());
125
    }
126
127
    protected function resources(): string
128
    {
129
        return ConfigHelper::resources();
130
    }
131
}
132