Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Service/Command/Exporter/ExportCommandHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

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
Should the return type not be integer|null?

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...
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
57
        return $exporter->export($translations);
58
    }
59
60
    /**
61
     * Convert an exportCommand into an array of translations
62
     *
63
     * @param ExportCommand $exportCommand
64
     *
65
     * @return array an array of translations
66
     */
67
    public function getTranslations(ExportCommand $exportCommand)
68
    {
69
        $locales = $this->determineLocalesToImport($exportCommand);
70
        $domains = $this->determineDomainsToImport($exportCommand);
71
72
        $translations = [];
73
        /** @var Translation $translation */
74
        foreach ($this->translationRepository->getTranslationsByLocalesAndDomains($locales, $domains) as $translation) {
75
            // Sort by translation key.
76
            $translations[$translation->getKeyword()][$translation->getLocale()] = $translation;
77
        }
78
79
        return $translations;
80
    }
81
82
    /**
83
     * Convert an exportCommand into an array of ExportFiles
84
     *
85
     * @param ExportCommand $exportCommand
86
     *
87
     * @return ArrayCollection an array of ExportFiles (without filecontent filled in)
88
     */
89 1
    public function getExportFiles(ExportCommand $exportCommand)
90
    {
91 1
        $locales = $this->determineLocalesToImport($exportCommand);
92 1
        $domains = $this->determineDomainsToImport($exportCommand);
93
94 1
        $translations = $this->translationRepository->getTranslationsByLocalesAndDomains($locales, $domains);
95
96 1
        $translationFiles = new ArrayCollection();
97
98
        /** @var Translation $translation */
99 1
        foreach ($translations as $translation) {
100 1
            $exportFileKey = $translation->getDomain() . '.' . $translation->getLocale() . '.' . $exportCommand->getFormat();
101
102 1
            if (!$translationFiles->containsKey($exportFileKey)) {
103 1
                $exportFile = new ExportFile();
104 1
                $exportFile->setExtension($exportCommand->getFormat());
105 1
                $exportFile->setDomain($translation->getDomain());
106 1
                $exportFile->setLocale($translation->getLocale());
107 1
                $translationFiles->set($exportFileKey, $exportFile);
108
            }
109
110 1
            $translationFiles->get($exportFileKey)->addTranslation($translation);
111
        }
112
113 1
        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 1
    public function determineLocalesToImport(ExportCommand $exportCommand)
137
    {
138 1
        if ($exportCommand->getLocales() === false) {
139
            return $this->managedLocales;
140
        }
141
142 1
        return $this->parseRequestedLocales($exportCommand->getLocales());
143
    }
144
145
    /**
146
     * @param ExportCommand $exportCommand
147
     *
148
     * @return array
149
     */
150 1
    public function determineDomainsToImport(ExportCommand $exportCommand)
151
    {
152 1
        if ($exportCommand->getDomains() === false) {
153
            return [];
154
        }
155
156 1
        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 1
    public function setTranslationRepository($translationRepository)
171
    {
172 1
        $this->translationRepository = $translationRepository;
173 1
    }
174
}
175