Completed
Pull Request — master (#89)
by
unknown
62:49
created

Export   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 91.49%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 153
ccs 43
cts 47
cp 0.9149
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C export() 0 25 7
A setManagedLocales() 0 4 1
A getManagedLocales() 0 4 1
B readStorage() 0 28 4
A getFilesystem() 0 4 1
A read() 0 14 3
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;
13
14
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
15
use ONGR\ElasticsearchDSL\Query\TermsQuery;
16
use ONGR\TranslationsBundle\Document\Message;
17
use ONGR\TranslationsBundle\Document\Translation;
18
use ONGR\ElasticsearchBundle\Service\Repository;
19
use ONGR\TranslationsBundle\Translation\Export\ExporterInterface;
20
use Symfony\Component\Filesystem\Filesystem;
21
22
/**
23
 * Class Export.
24
 */
25
class Export
26
{
27
    /**
28
     * @var Repository
29
     */
30
    private $repository;
31
32
    /**
33
     * @var ExporterInterface
34
     */
35
    private $exporter;
36
37
    /**
38
     * @var LoadersContainer
39
     */
40
    private $loadersContainer;
41
42
    /**
43
     * @var array
44
     */
45
    private $managedLocales = [];
46
47
    /**
48
     * @var Translation[]
49
     */
50
    private $refresh = [];
51
52
    /**
53
     * @param LoadersContainer  $loadersContainer
54
     * @param Repository        $repository
55 5
     * @param ExporterInterface $exporter
56
     */
57
    public function __construct(
58
        LoadersContainer $loadersContainer,
59
        Repository $repository,
60 5
        ExporterInterface $exporter
61 5
    ) {
62 5
        $this->repository = $repository;
63 5
        $this->exporter = $exporter;
64
        $this->loadersContainer = $loadersContainer;
65
    }
66
67
    /**
68
     * Exports translations from ES to files.
69
     *
70 5
     * @param array $domains To export.
71
     */
72 5
    public function export($domains = [])
73 1
    {
74 1
        foreach ($this->readStorage($domains) as $file => $translations) {
75
            if (!file_exists($file)) {
76 1
                $this->getFilesystem()->touch($file);
77 1
            }
78
            list($domain, $locale, $extension) = explode('.', $file);
79
            if ($this->loadersContainer && $this->loadersContainer->has($extension)) {
80
                $messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain);
81
                $translations = array_merge($messageCatalogue->all($domain), $translations);
82 1
            }
83
84
            $this->exporter->export($file, $translations);
85 1
        }
86 1
87 1
        if (!empty($this->refresh)) {
88
            foreach ($this->refresh as $translation) {
89 1
                $this->repository->getManager()->persist($translation);
90
            }
91
92
            $this->repository->getManager()->commit();
93
94
            $this->refresh = [];
95
        }
96 4
    }
97
98 4
    /**
99 4
     * Sets managed locales.
100
     *
101
     * @param array $managedLocales
102
     */
103
    public function setManagedLocales($managedLocales)
104 5
    {
105
        $this->managedLocales = $managedLocales;
106 5
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getManagedLocales()
112
    {
113
        return $this->managedLocales;
114
    }
115
116 5
    /**
117
     * Get translations for export.
118 5
     *
119 5
     * @param array $domains To read from storage.
120
     *
121
     * @return array
122 1
     */
123 1
    private function readStorage($domains)
124 1
    {
125
        $data = [];
126 1
        $translations = $this->read($this->getManagedLocales(), $domains);
127 1
128 1
        /** @var Translation $translation */
129 1
        foreach ($translations as $translation) {
130 1
            $messages = $translation->getMessages();
131 1
132 1
            foreach ($messages as $key => $message) {
133 1
                if ($message->getStatus() === Message::DIRTY) {
134
                    $path = sprintf(
135 1
                        '%s' . DIRECTORY_SEPARATOR . '%s.%s.%s',
136
                        $translation->getPath(),
137 1
                        $translation->getDomain(),
138 1
                        $message->getLocale(),
139 1
                        $translation->getFormat()
140
                    );
141
                    $data[$path][$translation->getKey()] = $message->getMessage();
142
143 1
                    $message->setStatus(Message::FRESH);
144 1
                    $this->refresh[] = $translation;
145 1
                }
146
            }
147
        }
148
149 1
        return $data;
150
    }
151
152
    /**
153
     * @return Filesystem
154
     */
155
    protected function getFilesystem()
156
    {
157
        return new Filesystem();
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function read($locales = [], $domains = [])
164
    {
165
        $search = $this->repository
166
            ->createSearch()
167
            ->setScroll('2m')
168
            ->addQuery(new MatchAllQuery());
169
        if (!empty($locales)) {
170
            $search->addFilter(new TermsQuery('messages.locale', $locales));
171
        }
172
        if (!empty($domains)) {
173
            $search->addFilter(new TermsQuery('domain', $domains));
174
        }
175
        return $this->repository->findDocuments($search);
176
    }
177
}
178