Completed
Push — master ( ca46fd...1d0b8e )
by
unknown
19:27 queued 09:34
created

NovaTranslation::translatableModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BBSLab\NovaTranslation;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Cache;
8
9
class NovaTranslation
10
{
11
    const LOCALES_CACHE_KEY = 'nova-translation-locales';
12
13
    /**
14
     * Forget caches locales.
15
     *
16
     * @return void
17
     */
18
    public static function forgetLocales(): void
19
    {
20
        Cache::forget(static::LOCALES_CACHE_KEY);
21
    }
22
23
    /**
24
     * @return \BBSLab\NovaTranslation\Models\Locale
25
     *
26
     * @throws \Exception
27
     */
28
    public static function currentLocale(): Locale
29
    {
30
        $locale = Locale::havingIso($iso = app()->getLocale());
31
32
        if (empty($locale)) {
33
            throw new \Exception("No such locale [{$iso}]");
34
        }
35
36
        return $locale;
37
    }
38
39
    /**
40
     * @return \Illuminate\Support\Collection
41
     */
42
    public static function locales(): Collection
43
    {
44
        return Cache::rememberForever(static::LOCALES_CACHE_KEY, function () {
45
            return Locale::query()
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
                ->orderBy('label')
47
                ->get();
48
        });
49
    }
50
51
    /**
52
     * @param  \BBSLab\NovaTranslation\Models\Locale|null  $current
53
     * @return \Illuminate\Support\Collection
54
     * @throws \Exception
55
     */
56
    public static function otherLocales(?Locale $current = null): Collection
57
    {
58
        $current = $current ?? static::currentLocale();
59
60
        return static::locales()->reject(function (Locale $locale) use ($current) {
61
            return $locale->is($current);
62
        });
63
    }
64
65
    public static function translatableModels(): array
66
    {
67
        return config(NovaTranslationServiceProvider::PACKAGE_ID.'.auto_synced_models', []) ?? [];
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public static function localeSessionKey(): string
74
    {
75
        return config('nova-translation.locale_session_key');
76
    }
77
}
78