TransChecker   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 192
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultLocale() 0 4 1
A getLocales() 0 4 1
A getIgnoredTranslations() 0 4 1
A check() 0 17 2
A __construct() 0 6 1
A getTranslations() 0 13 2
A diffMissing() 0 11 3
A addMissing() 0 6 2
A hasMissing() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelLang;
6
7
use Arcanedev\LaravelLang\Contracts\TransChecker as TransCheckerInterface;
8
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerInterface;
9
use Illuminate\Support\{Arr, Str};
10
use Illuminate\Translation\Translator;
11
12
/**
13
 * Class     TransChecker
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class TransChecker implements TransCheckerInterface
18
{
19
    /* -----------------------------------------------------------------
20
     |  Properties
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Config values.
26
     *
27
     * @var array
28
     */
29
    private $configs;
30
31
    /**
32
     * The translator instance.
33
     *
34
     * @var \Illuminate\Translation\Translator
35
     */
36
    private $translator;
37
38
    /**
39
     * The translator manager instance.
40
     *
41
     * @var \Arcanedev\LaravelLang\Contracts\TransManager
42
     */
43
    private $manager;
44
45
    /**
46
     * The missing translations.
47
     *
48
     * @var array
49
     */
50
    private $missing = [];
51
52
    /* -----------------------------------------------------------------
53
     |  Constructor
54
     | -----------------------------------------------------------------
55
     */
56
57
    /**
58
     * Make TransChecker instance.
59
     *
60
     * @param  \Illuminate\Translation\Translator             $translator
61
     * @param  \Arcanedev\LaravelLang\Contracts\TransManager  $manager
62
     * @param  array                                          $configs
63
     */
64 48
    public function __construct(Translator $translator, TransManagerInterface $manager, array $configs)
65
    {
66 48
        $this->translator = $translator;
67 48
        $this->manager    = $manager;
68 48
        $this->configs    = $configs;
69 48
    }
70
71
    /* -----------------------------------------------------------------
72
     |  Getters & Setter
73
     | -----------------------------------------------------------------
74
     */
75
76
    /**
77
     * Get the default locale being used.
78
     *
79
     * @return string
80
     */
81 6
    public function getDefaultLocale(): string
82
    {
83 6
        return $this->translator->getLocale();
84
    }
85
86
    /**
87
     * Get the locales to check.
88
     *
89
     * @return array
90
     */
91 6
    public function getLocales(): array
92
    {
93 6
        return Arr::get($this->configs, 'locales', []);
94
    }
95
96
    /**
97
     * Get the ignored translation attributes.
98
     *
99
     * @return array
100
     */
101 6
    public function getIgnoredTranslations(): array
102
    {
103 6
        return Arr::get($this->configs, 'check.ignore', []);
104
    }
105
106
    /* -----------------------------------------------------------------
107
     |  Main Methods
108
     | -----------------------------------------------------------------
109
     */
110
111
    /**
112
     * Check the missing translations.
113
     *
114
     * @return array
115
     */
116 6
    public function check(): array
117
    {
118 6
        $this->missing = [];
119 6
        $from          = $this->getDefaultLocale();
120 6
        $locales       = $this->getLocales();
121 6
        $ignored       = $this->getIgnoredTranslations();
122 6
        $fromTrans     = $this->getTranslations($from, $ignored);
123
124 6
        foreach ($locales as $to) {
125 6
            $toTrans = $this->getTranslations($to, $ignored);
126
127 6
            $this->diffMissing($toTrans, $fromTrans, $from);
128 6
            $this->diffMissing($fromTrans, $toTrans, $to);
129
        }
130
131 6
        return $this->missing;
132
    }
133
134
    /* -----------------------------------------------------------------
135
     |  Other Methods
136
     | -----------------------------------------------------------------
137
     */
138
139
    /**
140
     * Get locale translations from multiple groups.
141
     *
142
     * @param  string  $locale
143
     * @param  array   $ignored
144
     *
145
     * @return array
146
     */
147 6
    private function getTranslations(string $locale, array $ignored): array
148
    {
149 6
        $appLocale    = $this->manager->getFrom('app', $locale);
150 6
        $vendorLocale = $this->manager->getFrom('vendor-php', $locale);
151
152 6
        $translations = is_null($appLocale)
153 6
            ? $vendorLocale->mergeTranslations($appLocale, $ignored)
154 6
            : $appLocale->mergeTranslations($vendorLocale, $ignored);
155
156 6
        return array_filter($translations, function ($key) {
157 6
            return ! Str::startsWith($key, ['validation-inline.']);
158 6
        }, ARRAY_FILTER_USE_KEY);
159
    }
160
161
    /**
162
     * Diff the missing translations.
163
     *
164
     * @param  array   $toTranslations
165
     * @param  array   $fromTranslations
166
     * @param  string  $locale
167
     */
168 6
    private function diffMissing(array $toTranslations, array $fromTranslations, string $locale): void
169
    {
170 6
        $diff = array_diff_key($toTranslations, $fromTranslations);
171
172 6
        if (count($diff) === 0)
173 6
            return;
174
175 6
        foreach ($diff as $transKey => $transValue) {
176 6
            $this->addMissing($locale, $transKey);
177
        }
178 6
    }
179
180
    /**
181
     * Adding missing translation to collection.
182
     *
183
     * @param  string  $locale
184
     * @param  string  $transKey
185
     */
186 6
    private function addMissing(string $locale, string $transKey)
187
    {
188 6
        if ( ! $this->hasMissing($locale, $transKey)) {
189 6
            $this->missing[$locale][] = $transKey;
190
        }
191 6
    }
192
193
    /**
194
     * Check if a missing translation exists in collection.
195
     *
196
     * @param  string  $locale
197
     * @param  string  $transKey
198
     *
199
     * @return bool
200
     */
201 6
    private function hasMissing(string $locale, string $transKey): bool
202
    {
203 6
        if ( ! isset($this->missing[$locale]))
204 6
            return false;
205
206 6
        return in_array($transKey, $this->missing[$locale]);
207
    }
208
}
209