buildByLocale()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace Sleepness\UberTranslationAdminBundle\Frontend;
4
5
use Sleepness\UberTranslationBundle\Storage\UberMemcached;
6
7
/**
8
 * Prepare messages for output from Memcached
9
 *
10
 * @author Viktor Novikov <[email protected]>
11
 */
12
class MemcachedMessagesFrontendCatalogue implements MessagesFrontendCatalogueInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $preparedTranslations = array();
18
19
    /**
20
     * @var UberMemcached
21
     */
22
    private $memcached;
23
24
    /**
25
     * @var array
26
     */
27
    private $supportedLocales;
28
29
    /**
30
     * @param UberMemcached $memcached
31
     * @param $supportedLocales
32
     */
33
    public function __construct(UberMemcached $memcached, $supportedLocales)
34
    {
35
        $this->memcached = $memcached;
36
        $this->supportedLocales = $supportedLocales;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function prepareTranslations($domain, $keyYml, $message, $locale)
43
    {
44
        $this->preparedTranslations[] = array(
45
            'domain'       => $domain,
46
            'keyYml'       => $keyYml,
47
            'messageProps' => array(
48
                'messageText' => $message,
49
                'locale'      => $locale,
50
            ),
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function buildByLocale($locale)
58
    {
59
        if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) {
60
            return $this->preparedTranslations;
61
        }
62
        $translations = $this->memcached->getItem($locale);
63
        if ($translations) {
64
            foreach ($translations as $memcacheDomain => $messages) {
65
                foreach ($messages as $ymlKey => $value) {
66
                    $this->prepareTranslations($memcacheDomain, $ymlKey, $value, $locale);
67
                }
68
            }
69
        }
70
71
        return $this->preparedTranslations;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 View Code Duplication
    public function buildByDomain($domain)
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...
78
    {
79
        $locales = $this->supportedLocales;
80
        foreach ($locales as $key => $locale) {
81
            if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) {
82
                continue;
83
            }
84
            $translations = $this->memcached->getItem($locale);
85
            if ($translations) {
86
                foreach ($translations as $memcacheDomain => $messages) {
87
                    if ($domain == $memcacheDomain) {
88
                        foreach ($messages as $ymlKey => $value) {
89
                            $this->prepareTranslations($domain, $ymlKey, $value, $locale);
90
                        }
91
                    }
92
                }
93
            }
94
        }
95
96
        return $this->preparedTranslations;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 View Code Duplication
    public function buildByKey($keyYml)
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...
103
    {
104
        $locales = $this->supportedLocales;
105
        foreach ($locales as $key => $locale) {
106
            if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) {
107
                continue;
108
            }
109
            $translations = $this->memcached->getItem($locale);
110
            if ($translations) {
111
                foreach ($translations as $memcacheDomain => $messages) {
112
                    foreach ($messages as $ymlKey => $value) {
113
                        if ($ymlKey == $keyYml) {
114
                            $this->prepareTranslations($memcacheDomain, $keyYml, $value, $locale);
115
                        }
116
                    }
117
                }
118
            }
119
        }
120
121
        return $this->preparedTranslations;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 View Code Duplication
    public function buildByText($text)
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...
128
    {
129
        $locales = $this->supportedLocales;
130
        foreach ($locales as $key => $locale) {
131
            if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) {
132
                continue;
133
            }
134
            $translations = $this->memcached->getItem($locale);
135
            if ($translations) {
136
                foreach ($translations as $memcacheDomain => $messages) {
137
                    foreach ($messages as $ymlKey => $value) {
138
                        if (stripos($value, $text) !== false) {
139
                            $this->prepareTranslations($memcacheDomain, $ymlKey, $value, $locale);
140
                        }
141
                    }
142
                }
143
            }
144
        }
145
146
        return $this->preparedTranslations;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 View Code Duplication
    public function getAll()
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...
153
    {
154
        $locales = $this->supportedLocales;
155
        foreach ($locales as $key => $locale) {
156
            if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) {
157
                continue;
158
            }
159
            $translations = $this->memcached->getItem($locale);
160
            if ($translations) {
161
                foreach ($translations as $memcacheDomain => $messages) {
162
                    foreach ($messages as $ymlKey => $value) {
163
                        $this->prepareTranslations($memcacheDomain, $ymlKey, $value, $locale);
164
                    }
165
                }
166
            }
167
        }
168
169
        return $this->preparedTranslations;
170
    }
171
172
    /**
173
     * Replace translation by given properties
174
     *
175
     * @param $_key        - defined translation key
176
     * @param $_locale     - defined translation locale
177
     * @param $_domain     - defined translation domain
178
     * @param $formLocale  - changed translation locale
179
     * @param $formDomain  - changed translation domain
180
     * @param $formMessage - changed translation message
181
     */
182
    public function replace($_key, $_locale, $_domain, $formLocale, $formDomain, $formMessage)
183
    {
184
        $translations = $this->memcached->getItem($_locale);
185
        unset($translations[$_domain][$_key]);
186
        if ($formLocale != $_locale) {
187
            $this->memcached->addItem($_locale, $translations);
188
            $translations = $this->memcached->getItem($formLocale);
189
        }
190
        $translations[$formDomain][$_key] = $formMessage;
191
        $this->memcached->addItem($formLocale, $translations);
192
    }
193
}
194