Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

Translator   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Importance

Changes 0
Metric Value
wmc 24
lcom 3
cbo 6
dl 0
loc 174
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 4 1
A addResourcesFromCacher() 0 12 2
A addResourcesFromDatabaseAndCacheThem() 0 13 3
A addResources() 0 6 2
A loadCatalogue() 0 9 2
A trans() 0 17 3
B profileTranslation() 0 28 5
A getTranslationRepository() 0 4 1
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
     * @var Kunstmaan\TranslatorBundle\Service\Translator\ResourceCacher
25
     */
26
    private $resourceCacher;
27
28
    /**
29
     * @var \Symfony\Component\HttpFoundation\Request
30
     */
31
    protected $request;
32
33
    public function __construct(ContainerInterface $container, $formatter, $defaultLocale = null, array $loaderIds = array(), array $options = array(), $profilerEnable = false)
34
    {
35
        parent::__construct($container, $formatter, $defaultLocale, $loaderIds, $options);
36
37
        $this->profilerEnabled = $profilerEnable;
38
    }
39
40
    /**
41
     * Add resources from the database
42
     * So the translator knows where to look (first) for specific translations
43
     * This function will also look if these resources are loaded from the stash or from the cache
44
     */
45
    public function addDatabaseResources()
46
    {
47
        if ($this->addResourcesFromCacher() === false) {
48
            $this->addResourcesFromDatabaseAndCacheThem();
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function warmUp($cacheDir)
56
    {
57
        return;
58
    }
59
60
    /**
61
     * Add resources to the Translator from the cache
62
     */
63
    public function addResourcesFromCacher()
64
    {
65
        $resources = $this->resourceCacher->getCachedResources(false);
66
67
        if ($resources !== false) {
68
            $this->addResources($resources);
69
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * Add resources from the stash and cache them
78
     *
79
     * @param boolean $cacheResources cache resources after retrieving them from the stasher
80
     */
81
    public function addResourcesFromDatabaseAndCacheThem($cacheResources = true)
82
    {
83
        try {
84
            $resources = $this->translationRepository->getAllDomainsByLocale();
85
            $this->addResources($resources);
86
87
            if ($cacheResources === true) {
88
                $this->resourceCacher->cacheResources($resources);
89
            }
90
        } catch (\Exception $ex){
91
            // don't load if the database doesn't work
92
        }
93
    }
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
115
        if ($this->options['debug'] === true) {
116
            $this->options['cache_dir'] = null; // disable caching for debug
117
        }
118
119
        return parent::loadCatalogue($locale);
120
    }
121
122
    public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
123
    {
124
        if (!$this->request = $this->container->get('request_stack')->getCurrentRequest()) {
125
            return parent::trans($id, $parameters, $domain, $locale);
126
        }
127
128
        $showTranslationsSource = $this->request->get('transSource');
129
        if ($showTranslationsSource !== null) {
130
            $trans = sprintf('%s (%s)', $id, $domain);
131
        } else {
132
            $trans = parent::trans($id, $parameters, $domain, $locale);
133
        }
134
135
        $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...
136
137
        return $trans;
138
    }
139
140
    /**
141
     * @deprecated This method is deprecated since KunstmaanTranslatorBundle version 5.1 and will be removed in KunstmaanTranslatorBundle version 6.0
142
     */
143
    public function profileTranslation($id, $parameters, $domain, $locale, $trans)
144
    {
145
146
        if (!$this->request || $this->profilerEnabled === false) {
147
            return;
148
        }
149
150
        if ($locale === null) {
151
            $locale = $this->request->get('_locale');
152
        }
153
154
        $translation = new Translation;
155
        $translation->setKeyword($id);
156
        $translation->setDomain($domain);
157
        $translation->setLocale($locale);
158
        $translation->setText($trans);
159
160
        $translationCollection = $this->request->request->get('usedTranslations');
161
162
        if (!$translationCollection instanceof \Doctrine\Common\Collections\ArrayCollection) {
163
            $translationCollection = new ArrayCollection;
164
        }
165
166
        $translationCollection->set($domain . $id . $locale, $translation);
167
168
        $this->request->request->set('usedTranslations', $translationCollection);
169
170
    }
171
172
    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...
173
    {
174
        return $this->translationRepository;
175
    }
176
177
    public function setTranslationRepository($translationRepository)
178
    {
179
        $this->translationRepository = $translationRepository;
180
    }
181
182
    public function setResourceCacher($resourceCacher)
183
    {
184
        $this->resourceCacher = $resourceCacher;
185
    }
186
187
188
}
189