Completed
Push — odev ( a25a56...21e788 )
by De Cramer
02:29
created

Translations   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 60
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTranslation() 0 4 1
A getTranslations() 0 15 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use Symfony\Component\Translation\Translator;
6
7
8
/**
9
 * Class Translations
10
 *
11
 * @package eXpansion\Framework\Core\Services;
12
 * @author oliver de Cramer <[email protected]>
13
 */
14
class Translations
15
{
16
    /** @var Translator */
17
    protected $translator;
18
19
    /** @var string[] List of supported locales. */
20
    protected $supportedLocales;
21
22
    /**
23
     * Translations constructor.
24
     *
25
     * @param $translator
26
     * @param array $supportedLocales
27
     */
28 8
    public function __construct($translator, array $supportedLocales)
29
    {
30 8
        $this->translator = $translator;
31 8
        $this->supportedLocales = $supportedLocales;
32 8
    }
33
34
    /**
35
     * Get translated message.
36
     *
37
     * @param string $id
38
     * @param array $parameters
39
     * @param $locale
40
     *
41
     * @return mixed
42
     */
43 4
    public function getTranslation($id, $parameters = [], $locale = null)
44
    {
45 4
        return $this->translator->trans($id, $parameters, null, $locale);
46
    }
47
48
    /**
49
     * Get list of translations.
50
     *
51
     * @TODO optimize by preparing the messages before.
52
     *
53
     * @param $id
54
     * @param $parameters
55
     *
56
     * @return array
57
     */
58 2
    public function getTranslations($id, $parameters)
59
    {
60 2
        $messages = [];
61
62 2
        foreach ($this->supportedLocales as $locale)
63
        {
64 2
            $message = $this->getTranslation($id, $parameters, $locale);
65 2
            $messages[] = array(
66 2
                "Lang" => lcfirst($locale),
67 2
                "Text" => $message,
68
            );
69
        }
70
71 2
        return $messages;
72
    }
73
}