Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Service/Command/Exporter/ExportCommandHandler.php (2 issues)

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
     * @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...
34
     */
35
    public function executeExportCommand(ExportCommand $exportCommand)
36
    {
37
        $exportFiles = $this->getExportFiles($exportCommand);
38
        $this->fillExportFilesContent($exportFiles);
39
    }
40
41
    /**
42
     * Execute an export to CSV command.
43
     *
44
     * @return string|Response
45
     */
46
    public function executeExportCSVCommand(ExportCommand $exportCommand)
47
    {
48
        $translations = $this->getTranslations($exportCommand);
49
        /** @var CSVFileExporter $exporter */
50
        $exporter = $this->exporter->getExporterByExtension($exportCommand->getFormat());
51
        $exporter->setLocales($this->determineLocalesToImport($exportCommand));
52
53
        return $exporter->export($translations);
54
    }
55
56
    /**
57
     * Convert an exportCommand into an array of translations
58
     *
59
     * @return array an array of translations
60
     */
61
    public function getTranslations(ExportCommand $exportCommand)
62
    {
63
        $locales = $this->determineLocalesToImport($exportCommand);
64
        $domains = $this->determineDomainsToImport($exportCommand);
65
66
        $translations = [];
67
        /** @var Translation $translation */
68
        foreach ($this->translationRepository->getTranslationsByLocalesAndDomains($locales, $domains) as $translation) {
69
            // Sort by translation key.
70
            $translations[$translation->getKeyword()][$translation->getLocale()] = $translation;
71
        }
72
73
        return $translations;
74
    }
75
76
    /**
77
     * Convert an exportCommand into an array of ExportFiles
78
     *
79
     * @return ArrayCollection an array of ExportFiles (without filecontent filled in)
80
     */
81 1
    public function getExportFiles(ExportCommand $exportCommand)
82
    {
83 1
        $locales = $this->determineLocalesToImport($exportCommand);
84 1
        $domains = $this->determineDomainsToImport($exportCommand);
85
86 1
        $translations = $this->translationRepository->getTranslationsByLocalesAndDomains($locales, $domains);
87
88 1
        $translationFiles = new ArrayCollection();
89
90
        /** @var Translation $translation */
91 1
        foreach ($translations as $translation) {
92 1
            $exportFileKey = $translation->getDomain() . '.' . $translation->getLocale() . '.' . $exportCommand->getFormat();
93
94 1
            if (!$translationFiles->containsKey($exportFileKey)) {
95 1
                $exportFile = new ExportFile();
96 1
                $exportFile->setExtension($exportCommand->getFormat());
97 1
                $exportFile->setDomain($translation->getDomain());
98 1
                $exportFile->setLocale($translation->getLocale());
99 1
                $translationFiles->set($exportFileKey, $exportFile);
100
            }
101
102 1
            $translationFiles->get($exportFileKey)->addTranslation($translation);
103
        }
104
105 1
        return $translationFiles;
106
    }
107
108
    public function fillExportFilesContent(ArrayCollection $exportFiles)
109
    {
110
        foreach ($exportFiles as $exportFile) {
111
            $exportFile->fillArray();
112
            $content = $this->exporter->getExportedContent($exportFile);
113
            $exportFile->setContent($content);
114
        }
115
    }
116
117
    /**
118
     * Returns an array with all languages that needs to be imported (from the given ExportCommand)
119
     * If non is given, all managed locales will be used (defined in config)
120
     *
121
     * @return array all locales to import by the given ExportCommand
122
     */
123 1
    public function determineLocalesToImport(ExportCommand $exportCommand)
124
    {
125 1
        if ($exportCommand->getLocales() === false) {
126
            return $this->managedLocales;
127
        }
128
129 1
        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...
130
    }
131
132
    /**
133
     * @return array
134
     */
135 1
    public function determineDomainsToImport(ExportCommand $exportCommand)
136
    {
137 1
        if ($exportCommand->getDomains() === false) {
138
            return [];
139
        }
140
141 1
        return $this->parseRequestedDomains($exportCommand->getDomains());
142
    }
143
144
    /**
145
     * @param $exporter
146
     */
147
    public function setExporter($exporter)
148
    {
149
        $this->exporter = $exporter;
150
    }
151
152
    /**
153
     * @param $translationRepository
154
     */
155 1
    public function setTranslationRepository($translationRepository)
156
    {
157 1
        $this->translationRepository = $translationRepository;
158 1
    }
159
}
160