Exporter::load()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
c 2
b 0
f 1
nc 5
nop 1
dl 0
loc 19
ccs 0
cts 11
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace Terranet\Administrator\Services\Translations;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
8
class Exporter
9
{
10
    /**
11
     * @var Filesystem
12
     */
13
    protected $fs;
14
15
    /**
16
     * @var Collection
17
     */
18
    protected $locales;
19
20
    public function __construct()
21
    {
22
        $this->fs = app('files');
0 ignored issues
show
Bug introduced by
The function app 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

22
        $this->fs = /** @scrutinizer ignore-call */ app('files');
Loading history...
23
    }
24
25
    public function toJS(Collection $locales, $to = null): array
26
    {
27
        $exported = [];
28
29
        if ($translations = $this->load($locales)) {
30
            $translations = $this->replaceAttributes($translations);
31
32
            $to = $to ?: public_path('js'.\DIRECTORY_SEPARATOR.'translations');
0 ignored issues
show
Bug introduced by
The function public_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

32
            $to = $to ?: /** @scrutinizer ignore-call */ public_path('js'.\DIRECTORY_SEPARATOR.'translations');
Loading history...
33
34
            if (!$this->fs->exists($to)) {
35
                $this->fs->makeDirectory($to);
36
            }
37
38
            foreach ($translations as $lang => $translation) {
39
                $content = 'window.messages = {"'.$lang.'":'.json_encode($translation).'};';
40
                file_put_contents($file = $to.\DIRECTORY_SEPARATOR.$lang.'.js', $content);
41
                $exported[] = $file;
42
            }
43
        }
44
45
        return $exported;
46
    }
47
48
    protected function load(Collection $locales)
49
    {
50
        $data = [];
51
        $path = resource_path('lang'.\DIRECTORY_SEPARATOR);
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

51
        $path = /** @scrutinizer ignore-call */ resource_path('lang'.\DIRECTORY_SEPARATOR);
Loading history...
52
53
        foreach ($locales as $locale) {
54
            if (!$this->fs->exists($path.$locale->iso6391())) {
55
                continue;
56
            }
57
58
            if ($files = $this->fs->files($path.$locale->iso6391())) {
59
                foreach ($files as $file) {
60
                    $fileName = str_replace('.php', '', $file->getFilename());
61
                    $data[$locale->iso6391()][$fileName] = trans($fileName, [], $locale->iso6391());
0 ignored issues
show
Bug introduced by
The function trans 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

61
                    $data[$locale->iso6391()][$fileName] = /** @scrutinizer ignore-call */ trans($fileName, [], $locale->iso6391());
Loading history...
62
                }
63
            }
64
        }
65
66
        return $data;
67
    }
68
69
    protected function replaceAttributes($translations)
70
    {
71
        foreach ($translations as &$content) {
72
            if (\is_array($content)) {
73
                $content = $this->replaceAttributes($content);
74
75
                continue;
76
            }
77
78
            $content = preg_replace('%:([a-z\_]+)%m', '{$1}', $content);
79
        }
80
81
        return $translations;
82
    }
83
}
84