Completed
Pull Request — master (#333)
by Simon
04:01
created

TranslationService::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Components\Translations;
9
10
class TranslationService implements TranslationServiceInterface
11
{
12
    /** @var \PDO|\Enlight_Components_Db_Adapter_Pdo_Mysql */
13
    protected $db;
14
    protected $countryCode;
15
16
    protected $translations = [];
17
18
    public function __construct($db, $language)
19
    {
20
        $this->db = $db;
21
        $this->countryCode = $language;
22
    }
23
24
    public function get($topic, $value)
25
    {
26
        switch ($topic) {
27
            case 'countries':
28
                return $this->getTranslatedCountryNames($value);
29
            default:
30
                return $value;
31
        }
32
    }
33
34
    /**
35
     * Translate the iso3 country name to either an english or a german string
36
     *
37
     * @param $countries
38
     * @return mixed
39
     */
40
    private function getTranslatedCountryNames($countries)
41
    {
42
        if (in_array($this->countryCode, ['DEU', 'AUT'])) {
43
            $select = 'countryname';
44
        } else {
45
            $select = 'countryen';
46
        }
47
48
        $translatedCountries = $this->db->fetchAssoc(
49
            "SELECT iso3, {$select} as `name` FROM s_core_countries WHERE iso3 IN ({$this->db->quote($countries)})"
50
        );
51
52
        foreach ($countries as &$country) {
53
            $translation = $translatedCountries[$country]['name'];
54
            if (!empty($translation)) {
55
                $country = $translation;
56
            }
57
        }
58
59
        return $countries;
60
    }
61
}
62