These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\TranslatorBundle\Service\Command\Exporter; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Kunstmaan\TranslatorBundle\Entity\Translation; |
||
7 | use Kunstmaan\TranslatorBundle\Model\Export\ExportCommand; |
||
8 | use Kunstmaan\TranslatorBundle\Model\Export\ExportFile; |
||
9 | use Kunstmaan\TranslatorBundle\Repository\TranslationRepository; |
||
10 | use Kunstmaan\TranslatorBundle\Service\Command\AbstractCommandHandler; |
||
11 | use Symfony\Component\HttpFoundation\Response; |
||
12 | |||
13 | /** |
||
14 | * Parses an ExportCommand |
||
15 | */ |
||
16 | class ExportCommandHandler extends AbstractCommandHandler |
||
17 | { |
||
18 | /** |
||
19 | * @var Exporter |
||
20 | */ |
||
21 | private $exporter; |
||
22 | |||
23 | /** |
||
24 | * @var TranslationRepository |
||
25 | */ |
||
26 | private $translationRepository; |
||
27 | |||
28 | /** |
||
29 | * Execute an export command. |
||
30 | * Get all translations per domain and sort on file. |
||
31 | * A File has a domain locale, an extensions, with keywords and a ArrayCollection with translations. |
||
32 | * |
||
33 | * @param ExportCommand $exportCommand |
||
34 | * |
||
35 | * @return int total number of files imported |
||
0 ignored issues
–
show
|
|||
36 | */ |
||
37 | public function executeExportCommand(ExportCommand $exportCommand) |
||
38 | { |
||
39 | $exportFiles = $this->getExportFiles($exportCommand); |
||
40 | $this->fillExportFilesContent($exportFiles); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Execute an export to CSV command. |
||
45 | * |
||
46 | * @param ExportCommand $exportCommand |
||
47 | * |
||
48 | * @return string|Response |
||
49 | */ |
||
50 | public function executeExportCSVCommand(ExportCommand $exportCommand) |
||
51 | { |
||
52 | $translations = $this->getTranslations($exportCommand); |
||
53 | /** @var CSVFileExporter $exporter */ |
||
54 | $exporter = $this->exporter->getExporterByExtension($exportCommand->getFormat()); |
||
55 | $exporter->setLocales($this->determineLocalesToImport($exportCommand)); |
||
56 | return $exporter->export($translations); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Convert an exportCommand into an array of translations |
||
61 | * |
||
62 | * @param ExportCommand $exportCommand |
||
63 | * |
||
64 | * @return array an array of translations |
||
65 | */ |
||
66 | public function getTranslations(ExportCommand $exportCommand) |
||
67 | { |
||
68 | $locales = $this->determineLocalesToImport($exportCommand); |
||
69 | $domains = $this->determineDomainsToImport($exportCommand); |
||
70 | |||
71 | $translations = []; |
||
72 | /** @var Translation $translation */ |
||
73 | foreach ($this->translationRepository->getTranslationsByLocalesAndDomains($locales, $domains) as $translation) { |
||
74 | // Sort by translation key. |
||
75 | $translations[$translation->getKeyword()][$translation->getLocale()] = $translation; |
||
76 | } |
||
77 | |||
78 | return $translations; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Convert an exportCommand into an array of ExportFiles |
||
83 | * |
||
84 | * @param ExportCommand $exportCommand |
||
85 | * |
||
86 | * @return ArrayCollection an array of ExportFiles (without filecontent filled in) |
||
87 | */ |
||
88 | public function getExportFiles(ExportCommand $exportCommand) |
||
89 | { |
||
90 | $locales = $this->determineLocalesToImport($exportCommand); |
||
91 | $domains = $this->determineDomainsToImport($exportCommand); |
||
92 | |||
93 | $translations = $this->translationRepository->getTranslationsByLocalesAndDomains($locales, $domains); |
||
94 | |||
95 | $translationFiles = new ArrayCollection; |
||
96 | |||
97 | /** @var Translation $translation */ |
||
98 | foreach ($translations as $translation) { |
||
99 | $exportFileKey = $translation->getDomain() . '.' . $translation->getLocale() . '.' . $exportCommand->getFormat(); |
||
100 | |||
101 | if (!$translationFiles->containsKey($exportFileKey)) { |
||
102 | $exportFile = new ExportFile; |
||
103 | $exportFile->setExtension($exportCommand->getFormat()); |
||
104 | $exportFile->setDomain($translation->getDomain()); |
||
105 | $exportFile->setLocale($translation->getLocale()); |
||
106 | $translationFiles->set($exportFileKey, $exportFile); |
||
107 | } |
||
108 | |||
109 | $translationFiles->get($exportFileKey)->addTranslation($translation); |
||
110 | |||
111 | } |
||
112 | |||
113 | return $translationFiles; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param ArrayCollection $exportFiles |
||
118 | */ |
||
119 | public function fillExportFilesContent(ArrayCollection $exportFiles) |
||
120 | { |
||
121 | foreach ($exportFiles as $exportFile) { |
||
122 | $exportFile->fillArray(); |
||
123 | $content = $this->exporter->getExportedContent($exportFile); |
||
124 | $exportFile->setContent($content); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Returns an array with all languages that needs to be imported (from the given ExportCommand) |
||
130 | * If non is given, all managed locales will be used (defined in config) |
||
131 | * |
||
132 | * @param ExportCommand $exportCommand |
||
133 | * |
||
134 | * @return array all locales to import by the given ExportCommand |
||
135 | */ |
||
136 | public function determineLocalesToImport(ExportCommand $exportCommand) |
||
137 | { |
||
138 | if ($exportCommand->getLocales() === false) { |
||
139 | return $this->managedLocales; |
||
140 | } |
||
141 | |||
142 | return $this->parseRequestedLocales($exportCommand->getLocales()); |
||
0 ignored issues
–
show
$exportCommand->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...
|
|||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param ExportCommand $exportCommand |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function determineDomainsToImport(ExportCommand $exportCommand) |
||
151 | { |
||
152 | if ($exportCommand->getDomains() === false) { |
||
153 | return []; |
||
154 | } |
||
155 | |||
156 | return $this->parseRequestedDomains($exportCommand->getDomains()); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param $exporter |
||
161 | */ |
||
162 | public function setExporter($exporter) |
||
163 | { |
||
164 | $this->exporter = $exporter; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param $translationRepository |
||
169 | */ |
||
170 | public function setTranslationRepository($translationRepository) |
||
171 | { |
||
172 | $this->translationRepository = $translationRepository; |
||
173 | } |
||
174 | } |
||
175 |
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.