Reader::read()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 7
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 20
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Services\Translations;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Terranet\Localizer\Locale;
8
9
class Reader
10
{
11
    /**
12
     * Reads the translation file's contents.
13
     *
14
     * @param Collection $files
15
     * @param Collection $locales
16
     *
17
     * @return mixed
18
     */
19
    public function read(Collection $files, Collection $locales)
20
    {
21
        return $locales->reduce(function ($translations, $locale) use ($files) {
22
            $files->each(function ($file) use ($locale, &$translations) {
23
                if (file_exists($path = $this->pathToFile($file, $locale))) {
24
                    $content[$file] = include_once $path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$content was never initialized. Although not strictly required by PHP, it is generally a good practice to add $content = array(); before regardless.
Loading history...
25
26
                    foreach (Arr::dot($content) as $key => $value) {
27
                        $translations[$key][$locale->iso6391()] = $value ? $value : '';
28
                    }
29
                }
30
            });
31
32
            return $translations;
33
        }, []);
34
    }
35
36
    /**
37
     * Retrieves the real path to a translation file.
38
     *
39
     * @param $file
40
     * @param $locale
41
     *
42
     * @return string
43
     */
44
    public function pathToFile($file, $locale): string
45
    {
46
        return resource_path('lang'.\DIRECTORY_SEPARATOR.(is_a($locale, Locale::class) ? $locale->iso6391() : $locale).\DIRECTORY_SEPARATOR.$file.'.php');
0 ignored issues
show
Bug introduced by
The function resource_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return /** @scrutinizer ignore-call */ resource_path('lang'.\DIRECTORY_SEPARATOR.(is_a($locale, Locale::class) ? $locale->iso6391() : $locale).\DIRECTORY_SEPARATOR.$file.'.php');
Loading history...
47
    }
48
}
49