Translator   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSkipServiceBuild() 0 4 1
A addTranslationCsvFile() 0 7 2
C buildTranslationService() 0 26 7
A translate() 0 5 1
B translatePlaceholders() 0 25 6
A translatePart() 0 6 1
1
<?php
2
3
namespace Magium\Util\Translator;
4
5
use Zend\I18n\Translator\Loader\PhpMemoryArray;
6
7
class Translator extends \Zend\I18n\Translator\Translator
8
{
9
    const SERVICE_NAME = 'magium';
10
11
    protected $csvFiles = [];
12
13
    protected $loader;
14
15
    protected $skipServiceBuild = false;
16
17
    /**
18
     * @param boolean $skipServiceBuild
19
     */
20
    public function setSkipServiceBuild($skipServiceBuild)
21
    {
22
        $this->skipServiceBuild = $skipServiceBuild;
23
    }
24
25
    public function addTranslationCsvFile($file, $locale)
26
    {
27
        if (!isset($this->csvFiles[$locale])) {
28
            $this->csvFiles[$locale] = [];
29
        }
30
        $this->csvFiles[$locale][] = $file;
31
    }
32
33
    public function buildTranslationService()
34
    {
35
        if ($this->skipServiceBuild || $this->loader instanceof PhpMemoryArray) {
36
            return;
37
        }
38
        $translation = [
39
            'default'   => [
40
                'en_US' => []
41
            ]
42
        ];
43
        foreach ($this->csvFiles as $locale => $fileList) {
44
            $translation['default'][$locale] = [];
45
            foreach ($fileList as $file) {
46
                if (file_exists($file)) {
47
                    $fh = fopen($file, 'r');
48
                    while ($parts = fgetcsv($fh)) {
49
                        $translation['default'][$locale][$parts[0]] = $parts[1];
50
                    }
51
                    fclose($fh);
52
                }
53
            }
54
        }
55
        $this->loader = new PhpMemoryArray($translation);
56
        $this->getPluginManager()->setService(self::SERVICE_NAME, $this->loader);
57
        $this->addRemoteTranslations(self::SERVICE_NAME);
58
    }
59
60
    public function translate($message, $textDomain = 'default', $locale = null)
61
    {
62
        $this->buildTranslationService();
63
        return parent::translate($message, $textDomain, $locale);
64
    }
65
66
67
    public function translatePlaceholders($message, $textDomain = 'default', $locale = null)
68
    {
69
        $this->buildTranslationService();
70
        if (is_array($message)) {
71
            foreach ($message as $key => $value) {
72
                $value = $this->translatePlaceholders($value);
73
                $message[$key] = $value;
74
            }
75
        } else {
76
            $results = [];
77
            preg_match_all('/\{\{([^\}]+)\}\}/', $message, $results);
78
            array_shift($results);
79
80
            foreach ($results as $result) {
81
                if (is_array($result)) {
82
                    while (($part = array_shift($result)) !== null) {
83
                        $message = $this->translatePart($message, $part, $textDomain, $locale);
84
                    }
85
                } else {
86
                    $message = $this->translatePart($message, $result, $textDomain, $locale);
87
                }
88
            }
89
        }
90
        return $message;
91
    }
92
93
94
    protected function translatePart($translate, $result, $textDomain, $locale)
95
    {
96
        $newResult = parent::translate($result, $textDomain, $locale);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (translate() instead of translatePart()). Are you sure this is correct? If so, you might want to change this to $this->translate().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
97
        $translate = str_replace('{{' . $result . '}}', $newResult, $translate);
98
        return $translate;
99
    }
100
}