Completed
Push — master ( 298f7d...b54616 )
by Simonas
63:50
created

ExportManager::export()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\TranslationsBundle\Service\Export;
13
14
use ONGR\TranslationsBundle\Document\Message;
15
use ONGR\TranslationsBundle\Document\Translation;
16
use ONGR\TranslationsBundle\Service\TranslationManager;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\HttpFoundation\ParameterBag;
19
use Symfony\Component\Yaml\Parser as YamlParser;
20
/**
21
 * Class Export.
22
 */
23
class ExportManager
24
{
25
    /**
26
     * @var TranslationManager
27
     */
28
    private $translationManager;
29
30
    /**
31
     * @var YmlExport
32
     */
33
    private $exporter;
34
35
    /**
36
     * @var array
37
     */
38
    private $locales;
39
40
    /**
41
     * @var Translation[]
42
     */
43
    private $refresh = [];
44
45
    /**
46
     * @var YamlParser
47
     */
48
    private $parser;
49
50
    /**
51
     * @param ParameterBag   $loadersContainer
0 ignored issues
show
Bug introduced by
There is no parameter named $loadersContainer. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
     * @param TranslationManager $translationManager
53
     * @param YmlExport          $exporter
54
     */
55
    public function __construct(
56
        TranslationManager $translationManager,
57
        YmlExport $exporter
58
    ) {
59
        $this->parser = new YamlParser();
60
        $this->translationManager = $translationManager;
61
        $this->exporter = $exporter;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getLocales()
68
    {
69
        return $this->locales;
70
    }
71
72
    /**
73
     * @param array $locales
74
     */
75
    public function setLocales($locales)
76
    {
77
        $this->locales = $locales;
78
    }
79
80
    /**
81
     * Exports translations from ES to files.
82
     *
83
     * @param array $domains To export.
84
     * @param bool  $force
85
     */
86
    public function export($domains = [], $force = null)
87
    {
88
        foreach ($this->formExportList($domains, $force) as $file => $translations) {
0 ignored issues
show
Bug introduced by
It seems like $force defined by parameter $force on line 86 can also be of type null; however, ONGR\TranslationsBundle\...nager::formExportList() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
89
            if (!file_exists($file)) {
90
                (new Filesystem())->touch($file);
91
            }
92
93
            $currentTranslations = $this->parser->parse(file_get_contents($file)) ?? [];
94
95
            $translations = array_merge_recursive($currentTranslations, $translations);
96
97
            $this->exporter->export($file, $translations);
98
        }
99
100
        $this->translationManager->save($this->refresh);
101
        $this->refresh = [];
102
    }
103
104
    /**
105
     * Get translations for export.
106
     *
107
     * @param array $domains To read from storage.
108
     * @param bool  $force   Determines if the message status is relevant.
109
     *
110
     * @return array
111
     */
112
    private function formExportList($domains, $force)
113
    {
114
        $output = [];
115
        $filters = array_filter([
116
            'messages.locale' => $this->getLocales(),
117
            'domain' => $domains
118
        ]);
119
120
        $translations = $this->translationManager->getAll($filters);
121
122
        /** @var Translation $translation */
123
        foreach ($translations as $translation) {
124
            $messages = $translation->getMessages();
125
126
            foreach ($messages as $key => $message) {
127
                if ($message->getStatus() === Message::DIRTY || $force) {
128
                    $path = sprintf(
129
                        '%s' . DIRECTORY_SEPARATOR . '%s.%s.%s',
130
                        $translation->getPath(),
131
                        'messages',
132
                        $message->getLocale(),
133
                        $translation->getFormat()
134
                    );
135
                    $output[$path][$translation->getKey()] = $message->getMessage();
136
137
                    $message->setStatus(Message::FRESH);
138
                    $this->refresh[] = $translation;
139
                }
140
            }
141
        }
142
143
        return $output;
144
    }
145
}
146