Completed
Push — master ( 5a826a...3b609b )
by Mike
06:09
created

DatabaseLoader::getValueForLocale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A DatabaseLoader::getFileLoader() 0 4 1
1
<?php
2
3
namespace MikeZange\LaravelDatabaseTranslation\Loaders;
4
5
use Illuminate\Cache\Repository as CacheRepository;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use Illuminate\Translation\FileLoader;
9
use Illuminate\Translation\LoaderInterface;
10
use MikeZange\LaravelDatabaseTranslation\Models\Translation;
11
use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository;
12
13
/**
14
 * Class DatabaseLoader.
15
 */
16
class DatabaseLoader implements LoaderInterface
17
{
18
    /**
19
     * The filesystem instance.
20
     *
21
     * @var \Illuminate\Filesystem\Filesystem
22
     */
23
    protected $files;
24
25
    /**
26
     * The default path for the loader.
27
     *
28
     * @var string
29
     */
30
    protected $path;
31
32
    /**
33
     * The cache repository.
34
     *
35
     * @var CacheRepository
36
     */
37
    protected $cache;
38
39
    /**
40
     * Is cache enabled.
41
     *
42
     * @var bool
43
     */
44
    protected $cacheEnabled;
45
46
47
    /**
48
     * All of the namespace hints.
49
     *
50
     * @var array
51
     */
52
    protected $hints = [];
53
54
    /**
55
     * The cache repository.
56
     *
57
     * @var TranslationRepository
58
     */
59
    protected $translationRepository;
60
61
    /**
62
     * The cache repository.
63
     *
64
     * @var FileLoader
65
     */
66
    protected $laravelFileLoader;
67
68
    /**
69
     * DatabaseLoader constructor.
70
     *
71
     * @param Filesystem $filesystem
72
     * @param $path
73
     * @param TranslationRepository $translationRepository
74
     * @param CacheRepository       $cache
75
     */
76
    public function __construct(
77
        Filesystem $filesystem,
78
        $path,
79
        TranslationRepository $translationRepository,
80
        CacheRepository $cache
81
    ) {
82
        $this->files = $filesystem;
83
        $this->path = $path;
84
85
        $this->translationRepository = $translationRepository;
86
87
        $this->laravelFileLoader = new FileLoader($this->files, $this->path);
88
89
        $this->cache = $cache;
90
        $this->cacheEnabled = config('database.translations.cache_enabled');
91
    }
92
93
    /**
94
     * Load the messages for the given locale.
95
     *
96
     * @param string $locale
97
     * @param string $group
98
     * @param string $namespace
99
     *
100
     * @return array
101
     */
102
    public function load($locale, $group, $namespace = null) : array
103
    {
104
        $cacheKey = "translations.{$locale}.{$namespace}.{$group}";
105
106
        if (!$this->cacheEnabled) {
107
            $translations = $this->loadCombinedTranslations($locale, $group, $namespace);
108
        } else {
109
            $translations = $this->cache->remember($cacheKey, 1440, function () use ($locale, $namespace, $group) {
110
                return $this->loadCombinedTranslations($locale, $group, $namespace);
111
            });
112
        }
113
114
        return $translations;
115
    }
116
117
    /**
118
     * Get the messages for the given locale from the database and merge
119
     * them with the file based ones as a fallback for a given locale.
120
     *
121
     * @param string $locale
122
     * @param string $group
123
     * @param string $namespace
124
     *
125
     * @return array
126
     */
127
    protected function loadCombinedTranslations($locale, $group, $namespace) : array
128
    {
129
        return array_replace_recursive(
130
            $this->laravelFileLoader->load($locale, $group, $namespace),
131
            $this->loadFromDatabase($locale, $group, $namespace)
132
        );
133
    }
134
135
    /**
136
     * Add a new namespace to the loader.
137
     *
138
     * @param string $namespace
139
     * @param string $hint
140
     *
141
     * @return void
142
     */
143
    public function addNamespace($namespace, $hint)
144
    {
145
        $this->hints[$namespace] = $hint;
146
        $this->laravelFileLoader->addNamespace($namespace, $hint);
147
    }
148
149
    /**
150
     * Get an array of all the registered namespaces.
151
     *
152
     * @return array
153
     */
154
    public function namespaces() : array
155
    {
156
        return $this->hints;
157
    }
158
159
    /**
160
     * Load the required translations from the database.
161
     *
162
     * @param string $locale
163
     * @param string $group
164
     * @param string $namespace
165
     *
166
     * @return array
167
     */
168
    protected function loadFromDatabase($locale, $group, $namespace) : array
169
    {
170
        if (is_null($namespace) || $namespace == '*') {
171
            return $this->loadGroup($locale, $group);
172
        }
173
174
        return $this->loadNamespaced($locale, $namespace, $group);
175
    }
176
177
    /**
178
     * Load a non-namespaced translation group.
179
     *
180
     * @param string $locale
181
     * @param string $group
182
     *
183
     * @return array
184
     */
185
    protected function loadGroup($locale, $group) : array
186
    {
187
        $translations = $this->translationRepository->getItems(null, $group);
188
189
        return $this->createFormattedArray($translations, $locale);
190
    }
191
192
    /**
193
     * Load a namespaced translation group.
194
     *
195
     * @param string $locale
196
     * @param string $group
197
     * @param string $namespace
198
     *
199
     * @return array
200
     */
201
    protected function loadNamespaced($locale, $namespace, $group) : array
202
    {
203
        $translations = $this->translationRepository->getItems($namespace, $group);
204
205
        return $this->createFormattedArray($translations, $locale);
206
    }
207
208
    /**
209
     * Create a formatted array as if it was coming from the default loader.
210
     *
211
     * @param Collection $translations
212
     * @param string     $locale
213
     *
214
     * @return array
215
     */
216
    protected function createFormattedArray($translations, $locale) : array
217
    {
218
        $array = [];
219
220
        if ($translations) {
221
            foreach ($translations as $translation) {
222
                $value = $translation->getTranslation('values', $locale, true);
223
224
                if (! empty($value)) {
225
                    array_set($array, $translation->key, $value);
226
                }
227
            }
228
        }
229
230
        return $array;
231
    }
232
233
    /**
234
     * Get the Laravel File loader.
235
     *
236
     * @return FileLoader
237
     */
238
    public function getFileLoader() : FileLoader
239
    {
240
        return $this->laravelFileLoader;
241
    }
242
}
243