LocaleMenuListener::getLanguageName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\EventListener;
13
14
use FSi\Bundle\AdminBundle\Event\MenuEvent;
15
use FSi\Bundle\AdminBundle\Menu\Item\Item;
16
use FSi\Bundle\AdminBundle\Menu\Item\RoutableItem;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Component\Intl\Intl;
19
use Symfony\Component\Translation\TranslatorInterface;
20
21
class LocaleMenuListener
22
{
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28
    /**
29
     * @var RequestStack
30
     */
31
    private $requestStack;
32
33
    /**
34
     * @var string[]
35
     */
36
    private $locales;
37
38
    public function __construct(TranslatorInterface $translator, RequestStack $requestStack, array $locales)
39
    {
40
        $this->translator = $translator;
41
        $this->requestStack = $requestStack;
42
        $this->locales = $locales;
43
    }
44
45
    public function createLocaleMenu(MenuEvent $event): void
46
    {
47
        if (count($this->locales) < 2) {
48
            return;
49
        }
50
51
        $language = new Item('admin-locale');
52
        $language->setLabel(
53
            $this->translator->trans(
54
                'admin.language.current',
55
                ['%locale%' => $this->getLanguageName()],
56
                'FSiAdminBundle'
57
            )
58
        );
59
        $language->setOptions(['attr' => ['id' => 'language']]);
60
61
        foreach ($this->locales as $locale) {
62
            $localeItem = new RoutableItem(
63
                sprintf('admin-locale.%s', $locale),
64
                'fsi_admin_locale',
65
                [
66
                    '_locale' => $locale,
67
                    'redirect_uri' => $this->requestStack->getMasterRequest()->getUri()
68
                ]
69
            );
70
71
            $localeItem->setLabel($this->getLanguageName($locale));
72
            if ($locale === $this->getCurrentLocale()) {
73
                $localeItem->setOptions(['attr' => ['class' => 'active']]);
74
            }
75
            $language->addChild($localeItem);
76
        }
77
78
        $event->getMenu()->addChild($language);
79
    }
80
81
    private function getLanguageName(?string $locale = null): ?string
82
    {
83
        if (null === $locale) {
84
            $locale = $this->getCurrentLocale();
85
        }
86
87
        return Intl::getLanguageBundle()
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Intl\Intl::getLanguageBundle() has been deprecated: since Symfony 4.3, to be removed in 5.0. Use {@see Languages} or {@see Scripts} instead. ( Ignorable by Annotation )

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

87
        return /** @scrutinizer ignore-deprecated */ Intl::getLanguageBundle()

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...
88
            ->getLanguageName(
89
                $locale,
90
                null,
91
                $this->getCurrentLocale()
92
            );
93
    }
94
95
    private function getCurrentLocale(): string
96
    {
97
        return $this->requestStack->getMasterRequest()->getLocale();
98
    }
99
}
100