Completed
Push — master ( d7e919...298f7d )
by Simonas
64:16
created

ExportManager::getLocales()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
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
     * @param ParameterBag   $loadersContainer
47
     * @param TranslationManager $translationManager
48
     * @param YmlExport          $exporter
49
     */
50
    public function __construct(
51
        ParameterBag $loadersContainer,
52
        TranslationManager $translationManager,
53
        YmlExport $exporter
54
    ) {
55
        $this->loadersContainer = $loadersContainer;
0 ignored issues
show
Bug introduced by
The property loadersContainer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
        $this->translationManager = $translationManager;
57
        $this->exporter = $exporter;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getLocales()
64
    {
65
        return $this->locales;
66
    }
67
68
    /**
69
     * @param array $locales
70
     */
71
    public function setLocales($locales)
72
    {
73
        $this->locales = $locales;
74
    }
75
76
    /**
77
     * Exports translations from ES to files.
78
     *
79
     * @param array $domains To export.
80
     * @param bool  $force
81
     */
82
    public function export($domains = [], $force = null)
83
    {
84
        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 82 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...
85
            if (!file_exists($file)) {
86
                (new Filesystem())->touch($file);
87
            }
88
            list($domain, $locale, $extension) = explode('.', $file);
89
            if ($this->loadersContainer && $this->loadersContainer->has($extension)) {
90
                $messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain);
91
                $translations = array_merge($messageCatalogue->all($domain), $translations);
92
            }
93
94
            $this->exporter->export($file, $translations);
95
        }
96
97
        $this->translationManager->save($this->refresh);
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<ONGR\Translations...ice\TranslationManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        $this->refresh = [];
99
    }
100
101
    /**
102
     * Get translations for export.
103
     *
104
     * @param array $domains To read from storage.
105
     * @param bool  $force   Determines if the message status is relevant.
106
     *
107
     * @return array
108
     */
109
    private function formExportList($domains, $force)
110
    {
111
        $output = [];
112
        $filters = array_filter([
113
            'messages.locale' => $this->getLocales(),
114
            'domain' => $domains
115
        ]);
116
117
        $translations = $this->translationManager->getAll($filters);
0 ignored issues
show
Bug introduced by
The method getAll() does not seem to exist on object<ONGR\Translations...ice\TranslationManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
        /** @var Translation $translation */
120
        foreach ($translations as $translation) {
121
            $messages = $translation->getMessages();
122
123
            foreach ($messages as $key => $message) {
124
                if ($message->getStatus() === Message::DIRTY || $force) {
125
                    $path = sprintf(
126
                        '%s' . DIRECTORY_SEPARATOR . '%s.%s.%s',
127
                        $translation->getPath(),
128
                        'messages',
129
                        $message->getLocale(),
130
                        $translation->getFormat()
131
                    );
132
                    $data[$path][$translation->getKey()] = $message->getMessage();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
133
134
                    $message->setStatus(Message::FRESH);
135
                    $this->refresh[] = $translation;
136
                }
137
            }
138
        }
139
140
        return $output;
141
    }
142
}
143