Test Failed
Push — master ( 54db62...a5394f )
by Terzi
06:48
created

TranslationsManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Terranet\Administrator\Services\Translations\Reader;
8
use Zend\Code\Generator\ValueGenerator;
9
10
class TranslationsManager
11
{
12
    /** @var Collection */
13
    protected $locales;
14
15
    /** @var Translations\Reader */
16
    protected $reader;
17
18
    /** @var Filesystem */
19
    protected $filesystem;
20
21
    /**
22
     * TranslationsManager constructor.
23
     */
24
    public function __construct(Reader $translationsReader, Filesystem $filesystem)
25
    {
26
        $this->reader = $translationsReader;
27
        $this->filesystem = $filesystem;
28
    }
29
30
    /**
31
     * @param Collection $locales
32
     * @return $this
33
     */
34
    public function setLocales(Collection $locales)
35
    {
36
        $this->locales = $locales;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @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...
43
     * @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...
44
     * @param int $page
45
     * @param int $perPage
46
     * @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...
47
     */
48
    public function load($term = null, $only = null, $page = 1, $perPage = 20)
49
    {
50
        return (new Translations\Finder())->find(
51
            $this->reader->read($this->files($only), $this->locales()),
52
            $term, $page, $perPage
53
        );
54
    }
55
56
    /**
57
     * @return Translations\Exporter
58
     */
59
    public function exporter()
60
    {
61
        return new Translations\Exporter();
62
    }
63
64
    /**
65
     * Retrieves public filters.
66
     *
67
     * @return mixed
68
     */
69
    public function filters()
70
    {
71
        $filters = $this->files();
72
73
        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

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

121
                glob(/** @scrutinizer ignore-call */ resource_path("{$path}/*.php"))
Loading history...
122
            );
123
124
            $files = $files->map(function ($file) {
125
                return str_replace_last('.php', '', basename($file));
0 ignored issues
show
Deprecated Code introduced by
The function str_replace_last() has been deprecated: Str::replaceLast() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

125
                return /** @scrutinizer ignore-deprecated */ str_replace_last('.php', '', basename($file));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
126
            }, $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

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

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