Completed
Push — master ( 7258d4...a441d7 )
by Simonas
65:43
created

ExportManager::flatten()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
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
            $currentTranslations = $this->flatten($currentTranslations);
96
97
            $translations = array_replace_recursive($currentTranslations, $translations);
98
99
            $this->exporter->export($file, $translations);
100
        }
101
102
        $this->translationManager->save($this->refresh);
103
        $this->refresh = [];
104
    }
105
106
107
    /**
108
     * Flatten multidimensional array and concatenating keys
109
     *
110
     * @param $array
111
     * @return array
112
     */
113 View Code Duplication
    private function flatten($array, $prefix = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
        $result = [];
115
        foreach($array as $key=>$value) {
116
            if(is_array($value)) {
117
                $result = $result + $this->flatten($value, $prefix . $key . '.');
118
            }
119
            else {
120
                $result[$prefix . $key] = $value;
121
            }
122
        }
123
        return $result;
124
    }
125
126
    /**
127
     * Get translations for export.
128
     *
129
     * @param array $domains To read from storage.
130
     * @param bool  $force   Determines if the message status is relevant.
131
     *
132
     * @return array
133
     */
134
    private function formExportList($domains, $force)
135
    {
136
        $output = [];
137
        $filters = array_filter([
138
            'messages.locale' => $this->getLocales(),
139
            'domain' => $domains
140
        ]);
141
142
        $translations = $this->translationManager->getAll($filters);
143
144
        /** @var Translation $translation */
145
        foreach ($translations as $translation) {
146
            $messages = $translation->getMessages();
147
148
            foreach ($messages as $key => $message) {
149
                if ($message->getStatus() === Message::DIRTY || $force) {
150
                    $path = sprintf(
151
                        '%s' . DIRECTORY_SEPARATOR . '%s.%s.%s',
152
                        $translation->getPath(),
153
                        'messages',
154
                        $message->getLocale(),
155
                        $translation->getFormat()
156
                    );
157
                    $output[$path][$translation->getKey()] = $message->getMessage();
158
159
                    $message->setStatus(Message::FRESH);
160
                    $this->refresh[] = $translation;
161
                }
162
            }
163
        }
164
165
        return $output;
166
    }
167
}
168