Completed
Pull Request — 5.2 (#2397)
by Jeroen
18:37 queued 03:12
created

ImportCommandHandler::importSf4TranslationFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Service\Command\Importer;
4
5
use Kunstmaan\TranslatorBundle\Model\Import\ImportCommand;
6
use Kunstmaan\TranslatorBundle\Service\Command\AbstractCommandHandler;
7
use Kunstmaan\TranslatorBundle\Service\Exception\TranslationsNotFoundException;
8
use Kunstmaan\TranslatorBundle\Service\TranslationFileExplorer;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\HttpKernel\Kernel;
11
12
/**
13
 * Parses an ImportCommand
14
 */
15
class ImportCommandHandler extends AbstractCommandHandler
16
{
17
    /**
18
     * TranslationFileExplorer
19
     *
20
     * @var TranslationFileExplorer
21
     */
22
    private $translationFileExplorer;
23
24
    /**
25
     * Importer
26
     *
27
     * @var Importer
28
     */
29
    private $importer;
30
31
    /**
32
     * Execute an import command
33
     *
34
     * @param ImportCommand $importCommand
35
     *
36
     * @return int total number of files imported
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
37
     */
38
    public function executeImportCommand(ImportCommand $importCommand)
39
    {
40
        if ($importCommand->getGlobals() || $importCommand->getDefaultBundle() === false || $importCommand->getDefaultBundle() === null) {
41
            return $this->importGlobalTranslationFiles($importCommand);
42
        }
43
44
        return $this->importBundleTranslationFiles($importCommand);
45
    }
46
47
    /**
48
     * Import all translation files from app resources
49
     *
50
     * @param ImportCommand $importCommand
51
     *
52
     * @return int total number of files imported
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
53
     */
54
    private function importGlobalTranslationFiles(ImportCommand $importCommand)
55
    {
56
        $baseDir = Kernel::VERSION_ID >= 40000 ? '' : '/app';
57
        $translationsDir = Kernel::VERSION_ID >= 40000 ? 'translations' : null;
58
        $locales = $this->determineLocalesToImport($importCommand);
59
        $finder = $this->translationFileExplorer->find($this->kernel->getProjectDir() . $baseDir, $locales, $translationsDir);
60
61
        return $this->importTranslationFiles($finder, $importCommand->getForce());
62
    }
63
64
    /**
65
     * Import all translation files from a specific bundle, bundle name will be lowercased so cases don't matter
66
     *
67
     * @param ImportCommand $importCommand
68
     *
69
     * @return int total number of files imported
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
70
     */
71
    public function importBundleTranslationFiles(ImportCommand $importCommand)
72
    {
73
        $importBundle = strtolower($importCommand->getDefaultBundle());
74
75
        if ($importBundle == 'all') {
76
            return $this->importAllBundlesTranslationFiles($importCommand);
77
        } elseif ($importBundle == 'custom') {
78
            return $this->importCustomBundlesTranslationFiles($importCommand);
79
        } else {
80
            return $this->importOwnBundlesTranslationFiles($importCommand);
81
        }
82
    }
83
84
    /**
85
     * Import all translation files from all registered bundles (in AppKernel)
86
     *
87
     * @param ImportCommand $importCommand
88
     *
89
     * @return int total number of files imported
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    private function importAllBundlesTranslationFiles(ImportCommand $importCommand)
92
    {
93
        $bundles = array_map('strtolower', array_keys($this->kernel->getBundles()));
94
        $imported = 0;
95
96
        if (Kernel::VERSION_ID >= 40000) {
97
            $imported += $this->importSf4TranslationFiles($importCommand);
98
        }
99
100
        foreach ($bundles as $bundle) {
101
            $importCommand->setDefaultBundle($bundle);
102
103
            try {
104
                $imported += $this->importSingleBundleTranslationFiles($importCommand);
105
            } catch (TranslationsNotFoundException $e) {
106
                continue;
107
            }
108
        }
109
110
        return $imported;
111
    }
112
113
    /**
114
     * Import all translation files from your own registered bundles (in src/ directory)
115
     *
116
     * @param ImportCommand $importCommand
117
     *
118
     * @return int The total number of imported files
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|false|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
119
     */
120
    private function importOwnBundlesTranslationFiles(ImportCommand $importCommand)
121
    {
122
        if (Kernel::VERSION_ID >= 40000) {
123
            return $this->importSf4TranslationFiles($importCommand);
124
        }
125
126
        $imported = 0;
127
        $srcDir = dirname($this->kernel->getRootDir()) . '/src';
128
129
        foreach ($this->kernel->getBundles() as $name => $bundle) {
130
            if (strpos($bundle->getPath(), $srcDir) !== false) {
131
                $importCommand->setDefaultBundle(strtolower($name));
132
133
                try {
134
                    $imported += $this->importSingleBundleTranslationFiles($importCommand);
135
                } catch (TranslationsNotFoundException $e) {
136
                    continue;
137
                }
138
            }
139
        }
140
141
        return $imported;
142
    }
143
144
    /**
145
     * Import the translation files from the defined bundles.
146
     *
147
     * @param ImportCommand $importCommand
148
     *
149
     * @return int The total number of imported files
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
150
     */
151
    private function importCustomBundlesTranslationFiles(ImportCommand $importCommand)
152
    {
153
        $imported = 0;
154
155
        foreach ($importCommand->getBundles() as $bundle) {
156
            $importCommand->setDefaultBundle(strtolower($bundle));
157
158
            try {
159
                $imported += $this->importSingleBundleTranslationFiles($importCommand);
160
            } catch (TranslationsNotFoundException $e) {
161
                continue;
162
            }
163
        }
164
165
        return $imported;
166
    }
167
168
    /**
169
     * Import all translation files from a single bundle
170
     *
171
     * @param ImportCommand $importCommand
172
     *
173
     * @return int total number of files imported
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
174
     */
175
    private function importSingleBundleTranslationFiles(ImportCommand $importCommand)
176
    {
177
        $this->validateBundleName($importCommand->getDefaultBundle());
0 ignored issues
show
Documentation introduced by
$importCommand->getDefaultBundle() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
        $bundles = array_change_key_case($this->kernel->getBundles(), CASE_LOWER);
179
        $finder = $this->translationFileExplorer->find($bundles[strtolower($importCommand->getDefaultBundle())]->getPath(), $this->determineLocalesToImport($importCommand));
180
181
        if ($finder === null) {
182
            return 0;
183
        }
184
185
        return $this->importTranslationFiles($finder, $importCommand->getForce());
186
    }
187
188
    /**
189
     * Import translation files from a specific Finder object
190
     * The finder object shoud already have the files to look for defined;
191
     * Forcing the import will override all existing translations in the stasher
192
     *
193
     * @param Finder $finder
194
     * @param bool   $force  override identical translations in the stasher (domain/locale and keyword combination)
195
     *
196
     * @return int total number of files imported
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
197
     */
198
    private function importTranslationFiles(Finder $finder, $force = flase)
199
    {
200
        if (!$finder instanceof Finder) {
201
            return false;
202
        }
203
204
        $imported = 0;
205
206
        foreach ($finder as $file) {
207
            $imported += $this->importer->import($file, $force);
208
        }
209
210
        return $imported;
211
    }
212
213
    /**
214
     * Validates that a bundle is registered in the AppKernel
215
     *
216
     * @param string $bundle
217
     *
218
     * @return bool bundle is valid or not
219
     *
220
     * @throws \Exception If the bundlename isn't valid
221
     */
222
    public function validateBundleName($bundle)
223
    {
224
        // strtolower all bundle names
225
        $bundles = array_map('strtolower', array_keys($this->kernel->getBundles()));
226
227
        if (in_array(strtolower(trim($bundle)), $bundles)) {
228
            return true;
229
        }
230
231
        throw new \Exception(sprintf('bundle "%s" not found in available bundles: %s', $bundle, implode(', ', $bundles)));
232
    }
233
234
    /**
235
     * Gives an array with all languages that needs to be imported (from the given ImportCommand)
236
     * If non is given, all managed locales will be used (defined in config)
237
     *
238
     * @param ImportCommand $importCommand
239
     *
240
     * @return array all locales to import by the given ImportCommand
241
     */
242
    public function determineLocalesToImport(ImportCommand $importCommand)
243
    {
244
        if ($importCommand->getLocales() === false || $importCommand->getLocales() === null) {
245
            return $this->managedLocales;
246
        }
247
248
        return $this->parseRequestedLocales($importCommand->getLocales());
0 ignored issues
show
Documentation introduced by
$importCommand->getLocales() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
    }
250
251
    public function setTranslationFileExplorer($translationFileExplorer)
252
    {
253
        $this->translationFileExplorer = $translationFileExplorer;
254
    }
255
256
    public function setImporter($importer)
257
    {
258
        $this->importer = $importer;
259
    }
260
261
    private function importSf4TranslationFiles($importCommand)
262
    {
263
        $finder = $this->translationFileExplorer->find($this->kernel->getProjectDir(), $this->determineLocalesToImport($importCommand), 'translations');
264
265
        if ($finder === null) {
266
            return 0;
267
        }
268
269
        return $this->importTranslationFiles($finder, $importCommand->getForce());
270
    }
271
}
272