TranslationController::getTranslationsForDomain()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * @author Oskar van Velden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\FrameworkExtraBundle\Controller;
8
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
0 ignored issues
show
Bug introduced by
The type Sensio\Bundle\FrameworkE...dle\Configuration\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
11
use Symfony\Component\DependencyInjection\ContainerAware;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Class TranslationController
17
 *
18
 * @package Zicht\Bundle\FrameworkExtraBundle\Controller
19
 */
20
class TranslationController extends ContainerAware
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...njection\ContainerAware has been deprecated: since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

20
class TranslationController extends /** @scrutinizer ignore-deprecated */ ContainerAware
Loading history...
21
{
22
    /**
23
     * Wrapper for the translator-service
24
     *
25
     * The id's and domain are optional. When withheld the controller will return all the translations for the locale and (optional) the domain
26
     * If id's are passed, they should be passed in the query-parameters, ie:
27
     *
28
     *  One id:
29
     *  /translate/nl/messages?id=eticket.can_not_be_rendered_no_barcode
30
     *
31
     *  Multiple id's:
32
     *  /translate/nl/messages?id[]=eticket.can_not_be_rendered_no_barcode&id[]=eticket.col1&id[]=eticket.copy_warning&id[]=form_label.form.email
33
     *
34
     * @param Request $request
35
     * @param string $locale
36
     * @param string $domain
37
     * @return JsonResponse
38
     *
39
     * @Route("/translate/{locale}/{domain}")
40
     * @Route("/translate/{locale}")
41
     */
42
    public function translateAction(Request $request, $locale, $domain = null)
43
    {
44
        $queryIds = $request->query->get('id');
45
46
        if ($domain) {
47
            if ($queryIds) {
48
                if (!is_array($queryIds)) {
49
                    $queryIds = [$queryIds];
50
                }
51
52
                $translations = $this->getTranslationsForDomainAndIds($locale, $domain, $queryIds);
53
            } else {
54
                $translations = $this->getTranslationsForDomain($locale, $domain);
55
            }
56
        } else {
57
            $translations = $this->getTranslationsForLocale($locale);
58
        }
59
60
        return new JsonResponse(
61
            $translations
62
        );
63
    }
64
65
    /**
66
     * Retrieve all translations for the provided ids (within the provided locale and domain)
67
     *
68
     * @param string $locale
69
     * @param string $domain
70
     * @param array $ids
71
     * @return array
72
     */
73
    private function getTranslationsForDomainAndIds($locale, $domain, $ids)
74
    {
75
        /** @var Translator $translator */
76
        $translator = $this->container->get('translator');
77
78
        $translations = [];
79
        foreach ($ids as $id) {
80
            $translations[$id] = $translator->trans($id, [], $domain, $locale);
81
        }
82
83
        return $translations;
84
    }
85
86
    /**
87
     * Retrieve all translations for the provided locale and domain
88
     *
89
     * @param string $locale
90
     * @param string $domain
91
     * @return array
92
     * @throws \Exception
93
     */
94
    private function getTranslationsForDomain($locale, $domain)
95
    {
96
        $allMessages = $this->getTranslationsForLocale($locale);
97
98
        if (!array_key_exists($domain, $allMessages)) {
99
            throw new \Exception('Domain ' . $domain . ' not found in the translations for locale ' . $locale);
100
        }
101
102
        return $allMessages[$domain];
103
    }
104
105
    /**
106
     * Retrieve all translations for the provided locale
107
     *
108
     * @param string $locale
109
     * @return array
110
     */
111
    private function getTranslationsForLocale($locale)
112
    {
113
        /** @var Translator $translator */
114
        $translator = $this->container->get('translator');
115
116
        return $translator->getMessages($locale);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Transl...anslator::getMessages() has been deprecated: since version 2.8, to be removed in 3.0. Use TranslatorBagInterface::getCatalogue() method instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        return /** @scrutinizer ignore-deprecated */ $translator->getMessages($locale);

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

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

Loading history...
117
    }
118
}
119