TranslationsManager   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 190
rs 10
c 0
b 0
f 0
wmc 22

12 Methods

Rating   Name   Duplication   Size   Complexity  
A files() 0 18 3
A save() 0 18 4
A locales() 0 3 1
A loadTranslationFile() 0 9 2
A filters() 0 13 3
A keyToArray() 0 6 2
A exporter() 0 3 1
A arrayToString() 0 6 1
A setLocales() 0 5 1
A makeFile() 0 11 2
A load() 0 5 1
A __construct() 0 4 1
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use Terranet\Administrator\Services\Translations\Reader;
9
use Zend\Code\Generator\ValueGenerator;
10
11
class TranslationsManager
12
{
13
    /** @var Collection */
14
    protected $locales;
15
16
    /** @var Translations\Reader */
17
    protected $reader;
18
19
    /** @var Filesystem */
20
    protected $filesystem;
21
22
    /**
23
     * TranslationsManager constructor.
24
     */
25
    public function __construct(Reader $translationsReader, Filesystem $filesystem)
26
    {
27
        $this->reader = $translationsReader;
28
        $this->filesystem = $filesystem;
29
    }
30
31
    /**
32
     * @param Collection $locales
33
     * @return $this
34
     */
35
    public function setLocales(Collection $locales)
36
    {
37
        $this->locales = $locales;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param null $term
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $term is correct as it would always require null to be passed?
Loading history...
44
     * @param null $only
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $only is correct as it would always require null to be passed?
Loading history...
45
     * @param int $page
46
     * @param int $perPage
47
     * @return \Illuminate\Pagination\LengthAwarePaginator
0 ignored issues
show
Bug introduced by
The type Illuminate\Pagination\LengthAwarePaginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
     */
49
    public function load($term = null, $only = null, $page = 1, $perPage = 20)
50
    {
51
        return (new Translations\Finder())->find(
52
            $this->reader->read($this->files($only), $this->locales()),
53
            $term, $page, $perPage
54
        );
55
    }
56
57
    /**
58
     * @return Translations\Exporter
59
     */
60
    public function exporter()
61
    {
62
        return new Translations\Exporter();
63
    }
64
65
    /**
66
     * Retrieves public filters.
67
     *
68
     * @return mixed
69
     */
70
    public function filters()
71
    {
72
        $filters = $this->files();
73
74
        if ($only = config('administrator.translations.filters.only', [])) {
0 ignored issues
show
Bug introduced by
The function config 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

74
        if ($only = /** @scrutinizer ignore-call */ config('administrator.translations.filters.only', [])) {
Loading history...
75
            $filters = $filters->intersect($only);
76
        }
77
78
        if ($except = config('administrator.translations.filters.except', [])) {
79
            $filters = $filters->diff($except);
80
        }
81
82
        return $filters;
83
    }
84
85
    /**
86
     * @param $translation
87
     * @param $locale
88
     */
89
    public function save($translation, $locale)
90
    {
91
        $translations = [];
92
93
        foreach ($translation as $key => $value) {
94
            $this->keyToArray($translations, $key, $value[$locale]);
95
        }
96
97
        foreach ($translations as $key => $value) {
98
            if (!$translations = $this->loadTranslationFile($key, $locale)) {
99
                continue;
100
            }
101
102
            $data = array_replace_recursive($translations['content'], $value);
103
104
            $content = '<?php'.str_repeat(PHP_EOL, 2).'return '.$this->arrayToString($data).';';
105
106
            $this->filesystem->put($translations['path'], $content, true);
107
        }
108
    }
109
110
    /**
111
     * @param null $only
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $only is correct as it would always require null to be passed?
Loading history...
112
     * @return Collection
113
     */
114
    protected function files($only = null): Collection
115
    {
116
        static $files = null;
117
118
        if (null === $files) {
119
            $path = 'lang'.\DIRECTORY_SEPARATOR.\localizer\locale()->iso6391();
120
121
            $files = collect(
122
                glob(resource_path("{$path}/*.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

122
                glob(/** @scrutinizer ignore-call */ resource_path("{$path}/*.php"))
Loading history...
123
            );
124
125
            $files = $files->map(function ($file) {
126
                return Str::replaceLast('.php', '', basename($file));
127
            }, $files);
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Support\Collection::map() has too many arguments starting with $files. ( Ignorable by Annotation )

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

127
            /** @scrutinizer ignore-call */ 
128
            $files = $files->map(function ($file) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
128
        }
129
130
        return $files->when($only, function (Collection $files) use ($only) {
131
            return $files->intersect(\is_array($only) ? $only : [$only]);
0 ignored issues
show
introduced by
The condition is_array($only) is always false.
Loading history...
132
        });
133
    }
134
135
    /**
136
     * @param $file
137
     * @param $locale
138
     * @return array
139
     */
140
    protected function loadTranslationFile($file, $locale)
141
    {
142
        if (!file_exists($path = $this->reader->pathToFile($file, $locale))) {
143
            $this->makeFile($file, $locale);
144
        }
145
146
        return [
147
            'path' => $path,
148
            'content' => include_once $path,
149
        ];
150
    }
151
152
    /**
153
     * @param $file
154
     * @param $locale
155
     */
156
    protected function makeFile($file, $locale)
157
    {
158
        $directoryTranslationsPath = resource_path('lang'.\DIRECTORY_SEPARATOR.$locale);
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

158
        $directoryTranslationsPath = /** @scrutinizer ignore-call */ resource_path('lang'.\DIRECTORY_SEPARATOR.$locale);
Loading history...
159
160
        if (!$this->filesystem->exists($directoryTranslationsPath)) {
161
            $this->filesystem->makeDirectory($directoryTranslationsPath);
162
        }
163
164
        $content = '<?php'.str_repeat(PHP_EOL, 2).'return [];';
165
166
        $this->filesystem->put($directoryTranslationsPath.\DIRECTORY_SEPARATOR.$file.'.php', $content, true);
167
    }
168
169
    /**
170
     * @param $arr
171
     * @param $path
172
     * @param $value
173
     * @param string $separator
174
     */
175
    protected function keyToArray(&$arr, $path, $value, $separator = '.')
176
    {
177
        foreach ($keys = explode($separator, $path) as $key) {
0 ignored issues
show
Unused Code introduced by
The assignment to $keys is dead and can be removed.
Loading history...
178
            $arr = &$arr[$key];
179
        }
180
        $arr = $value;
181
    }
182
183
    /**
184
     * @param $data
185
     * @return string
186
     */
187
    protected function arrayToString($data): string
188
    {
189
        $generator = new ValueGenerator($data, ValueGenerator::TYPE_ARRAY_SHORT);
190
        $generator->setIndentation('    '); // 4 spaces
191
192
        return $generator->generate();
193
    }
194
195
    /**
196
     * @return Collection
197
     */
198
    protected function locales(): Collection
199
    {
200
        return collect($this->locales);
201
    }
202
}
203