Translator   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 34.38%

Importance

Changes 0
Metric Value
wmc 24
lcom 3
cbo 6
dl 0
loc 169
ccs 22
cts 64
cp 0.3438
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addDatabaseResources() 0 6 2
A warmUp() 0 3 1
A addResourcesFromCacher() 0 12 2
A loadCatalogue() 0 8 2
A trans() 0 17 3
A getTranslationRepository() 0 4 1
A addResourcesFromDatabaseAndCacheThem() 0 13 3
A addResources() 0 6 2
A profileTranslation() 0 26 5
A setTranslationRepository() 0 4 1
A setResourceCacher() 0 4 1
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Service\Translator;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Kunstmaan\TranslatorBundle\Entity\Translation;
7
use Psr\Container\ContainerInterface;
8
use Symfony\Bundle\FrameworkBundle\Translation\Translator as SymfonyTranslator;
9
10
/**
11
 * Translator
12
 *
13
 * NEXT_MAJOR remove the $profilerEnable constructor parameter en remove the profileTranslation method.
14
 */
15
class Translator extends SymfonyTranslator
16
{
17
    /** @var bool */
18
    private $profilerEnabled;
19
20
    private $translationRepository;
21
22
    /**
23
     * Resource Cacher
24
     *
25
     * @var Kunstmaan\TranslatorBundle\Service\Translator\ResourceCacher
26
     */
27
    private $resourceCacher;
28
29
    /**
30
     * @var \Symfony\Component\HttpFoundation\Request
31
     */
32
    protected $request;
33
34 3
    public function __construct(ContainerInterface $container, $formatter, $defaultLocale = null, array $loaderIds = array(), array $options = array(), $profilerEnable = false)
35
    {
36 3
        parent::__construct($container, $formatter, $defaultLocale, $loaderIds, $options);
37
38 3
        $this->profilerEnabled = $profilerEnable;
39 3
    }
40
41
    /**
42
     * Add resources from the database
43
     * So the translator knows where to look (first) for specific translations
44
     * This function will also look if these resources are loaded from the stash or from the cache
45
     */
46 3
    public function addDatabaseResources()
47
    {
48 3
        if ($this->addResourcesFromCacher() === false) {
49 3
            $this->addResourcesFromDatabaseAndCacheThem();
50
        }
51 3
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function warmUp($cacheDir)
57
    {
58
    }
59
60
    /**
61
     * Add resources to the Translator from the cache
62
     */
63 3
    public function addResourcesFromCacher()
64
    {
65 3
        $resources = $this->resourceCacher->getCachedResources(false);
66
67 3
        if ($resources !== false) {
68
            $this->addResources($resources);
69
70
            return true;
71
        }
72
73 3
        return false;
74
    }
75
76
    /**
77
     * Add resources from the stash and cache them
78
     *
79
     * @param bool $cacheResources cache resources after retrieving them from the stasher
80
     */
81 3
    public function addResourcesFromDatabaseAndCacheThem($cacheResources = true)
82
    {
83
        try {
84 3
            $resources = $this->translationRepository->getAllDomainsByLocale();
85
            $this->addResources($resources);
86
87
            if ($cacheResources === true) {
88
                $this->resourceCacher->cacheResources($resources);
89
            }
90 3
        } catch (\Exception $ex) {
91
            // don't load if the database doesn't work
92
        }
93 3
    }
94
95
    /**
96
     * Add resources to the Translator
97
     * Resources is an array[0] => array('name' => 'messages', 'locale' => 'en')
98
     * Where name is the domain of the domain
99
     *
100
     * @param array $resources
101
     */
102
    public function addResources($resources)
103
    {
104
        foreach ($resources as $resource) {
105
            $this->addResource('database', 'DB', $resource['locale'], $resource['name']);
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    protected function loadCatalogue($locale)
113
    {
114
        if ($this->options['debug'] === true) {
115
            $this->options['cache_dir'] = null; // disable caching for debug
116
        }
117
118
        return parent::loadCatalogue($locale);
119
    }
120
121
    public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
122
    {
123
        if (!$this->request = $this->container->get('request_stack')->getCurrentRequest()) {
124
            return parent::trans($id, $parameters, $domain, $locale);
125
        }
126
127
        $showTranslationsSource = $this->request->get('transSource');
128
        if ($showTranslationsSource !== null) {
129
            $trans = sprintf('%s (%s)', $id, $domain);
130
        } else {
131
            $trans = parent::trans($id, $parameters, $domain, $locale);
132
        }
133
134
        $this->profileTranslation($id, $parameters, $domain, $locale, $trans);
0 ignored issues
show
Deprecated Code introduced by
The method Kunstmaan\TranslatorBund...r::profileTranslation() has been deprecated with message: This method is deprecated since KunstmaanTranslatorBundle version 5.1 and will be removed in KunstmaanTranslatorBundle version 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
135
136
        return $trans;
137
    }
138
139
    /**
140
     * @deprecated This method is deprecated since KunstmaanTranslatorBundle version 5.1 and will be removed in KunstmaanTranslatorBundle version 6.0
141
     */
142
    public function profileTranslation($id, $parameters, $domain, $locale, $trans)
143
    {
144
        if (!$this->request || $this->profilerEnabled === false) {
145
            return;
146
        }
147
148
        if ($locale === null) {
149
            $locale = $this->request->get('_locale');
150
        }
151
152
        $translation = new Translation();
153
        $translation->setKeyword($id);
154
        $translation->setDomain($domain);
155
        $translation->setLocale($locale);
156
        $translation->setText($trans);
157
158
        $translationCollection = $this->request->request->get('usedTranslations');
159
160
        if (!$translationCollection instanceof \Doctrine\Common\Collections\ArrayCollection) {
161
            $translationCollection = new ArrayCollection();
162
        }
163
164
        $translationCollection->set($domain . $id . $locale, $translation);
165
166
        $this->request->request->set('usedTranslations', $translationCollection);
167
    }
168
169
    public function getTranslationRepository()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
170
    {
171
        return $this->translationRepository;
172
    }
173
174 3
    public function setTranslationRepository($translationRepository)
175
    {
176 3
        $this->translationRepository = $translationRepository;
177 3
    }
178
179 3
    public function setResourceCacher($resourceCacher)
180
    {
181 3
        $this->resourceCacher = $resourceCacher;
182 3
    }
183
}
184