Completed
Branch v1.x-dev (6fcd50)
by Benjamin
04:47
created

LocaleHelper::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Obblm\Core\Helper;
4
5
use Symfony\Component\Routing\RouterInterface;
6
7
class LocaleHelper
8
{
9
    private $router = null;
10
    private $available_locales = null;
11
12
    public function __construct(RouterInterface $router, $defaultLocale = "en")
13
    {
14
        $this->router = $router;
15
        $this->available_locales = [$defaultLocale];
16
17
        $locales = $router->getRouteCollection()->get('obblm_locale_switch')->getRequirement('_locale');
18
19
        if($locales) {
20
            $this->available_locales = explode('|', $locales);
21
        }
22
    }
23
24
    /**
25
     * @return false|string[]
26
     */
27
    public function getAvailableLocales()
28
    {
29
        return $this->available_locales;
30
    }
31
32
    /**
33
     * @return false|string[]
34
     */
35
    public function getLocalizedRoutes()
36
    {
37
        $routes = [];
38
        foreach ($this->available_locales as $locale)
39
        {
40
            $routes[] = [
41
                'route' => $this->router->generate('obblm_locale_switch', ['_locale' => $locale]),
42
                '_locale' => $locale,
43
            ];
44
        }
45
        return $routes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $routes returns an array which contains values of type array<string,mixed|string> which are incompatible with the documented value type string.
Loading history...
46
    }
47
}
48