Completed
Pull Request — master (#89)
by
unknown
65:00
created

Export   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 127
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B export() 0 18 5
A setManagedLocales() 0 4 1
A getManagedLocales() 0 4 1
B formExportList() 0 33 5
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\LoadersContainer;
17
use ONGR\TranslationsBundle\Service\TranslationManager;
18
use Symfony\Component\Filesystem\Filesystem;
19
20
/**
21
 * Class Export.
22
 */
23
class Export
24
{
25
    /**
26
     * @var TranslationManager
27
     */
28
    private $translationManager;
29
30
    /**
31
     * @var ExporterInterface
32
     */
33
    private $exporter;
34
35
    /**
36
     * @var LoadersContainer
37
     */
38
    private $loadersContainer;
39
40
    /**
41
     * @var array
42
     */
43
    private $managedLocales = [];
44
45
    /**
46
     * @var Translation[]
47
     */
48
    private $refresh = [];
49
50
    /**
51
     * @param LoadersContainer   $loadersContainer
52
     * @param TranslationManager $translationManager
53
     * @param ExporterInterface  $exporter
54
     */
55
    public function __construct(
56
        LoadersContainer $loadersContainer,
57
        TranslationManager $translationManager,
58
        ExporterInterface $exporter
59
    ) {
60
        $this->loadersContainer = $loadersContainer;
61
        $this->translationManager = $translationManager;
62
        $this->exporter = $exporter;
63
    }
64
65
    /**
66
     * Exports translations from ES to files.
67
     *
68
     * @param array $domains To export.
69
     * @param bool  $force
70
     */
71
    public function export($domains = [], $force = null)
72
    {
73
        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 71 can also be of type null; however, ONGR\TranslationsBundle\...xport::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...
74
            if (!file_exists($file)) {
75
                (new Filesystem())->touch($file);
76
            }
77
            list($domain, $locale, $extension) = explode('.', $file);
78
            if ($this->loadersContainer && $this->loadersContainer->has($extension)) {
79
                $messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain);
80
                $translations = array_merge($messageCatalogue->all($domain), $translations);
81
            }
82
83
            $this->exporter->export($file, $translations);
84
        }
85
86
        $this->translationManager->saveTranslations($this->refresh);
87
        $this->refresh = [];
88
    }
89
90
    /**
91
     * Sets managed locales.
92
     *
93
     * @param array $managedLocales
94
     */
95
    public function setManagedLocales($managedLocales)
96
    {
97
        $this->managedLocales = $managedLocales;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getManagedLocales()
104
    {
105
        return $this->managedLocales;
106
    }
107
108
    /**
109
     * Get translations for export.
110
     *
111
     * @param array $domains To read from storage.
112
     * @param bool  $force   Determines if the message status is relevant.
113
     *
114
     * @return array
115
     */
116
    private function formExportList($domains, $force)
117
    {
118
        $data = [];
119
        $filters = array_filter([
120
            'messages.locale' => $this->getManagedLocales(),
121
            'domain' => $domains
122
        ]);
123
124
        $translations = $this->translationManager->getTranslations($filters);
125
126
        /** @var Translation $translation */
127
        foreach ($translations as $translation) {
128
            $messages = $translation->getMessages();
129
130
            foreach ($messages as $key => $message) {
131
                if ($message->getStatus() === Message::DIRTY || $force) {
132
                    $path = sprintf(
133
                        '%s' . DIRECTORY_SEPARATOR . '%s.%s.%s',
134
                        $translation->getPath(),
135
                        $translation->getDomain(),
136
                        $message->getLocale(),
137
                        $translation->getFormat()
138
                    );
139
                    $data[$path][$translation->getKey()] = $message->getMessage();
140
141
                    $message->setStatus(Message::FRESH);
142
                    $this->refresh[] = $translation;
143
                }
144
            }
145
        }
146
147
        return $data;
148
    }
149
}
150