Completed
Push — master ( 227e03...1b465c )
by Artem
10:47 queued 05:43
created

LanguageSwitcherBlockService::configureSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Application\Bundle\DefaultBundle\Service;
4
5
use Sonata\BlockBundle\Block\BlockContextInterface;
6
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
7
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
/**
14
 * Class LanguageSwitcherBlockService.
15
 */
16
class LanguageSwitcherBlockService extends AbstractBlockService
17
{
18
    /** @var Request */
19
    private $request;
20
21
    /** @var array */
22
    private $locales;
23
24
    /**
25
     * ProgramEventBlockService constructor.
26
     *
27
     * @param string          $name
28
     * @param EngineInterface $templating
29
     * @param RequestStack    $requestStack
30
     * @param array           $locales
31
     */
32
    public function __construct($name, EngineInterface $templating, RequestStack $requestStack, array $locales)
33
    {
34
        parent::__construct($name, $templating);
35
36
        $this->request = $requestStack->getCurrentRequest();
37
        $this->locales = $locales;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function execute(BlockContextInterface $blockContext, Response $response = null)
44
    {
45
        $position = $blockContext->getSetting('position');
46
        $localesArr = [];
47
        foreach ($this->locales as $locale) {
48
            $localesArr[$locale] = $this->localizeRoute($this->request, $locale);
49
        }
50
51
        return $this->renderResponse($blockContext->getTemplate(), [
52
            'block' => $blockContext->getBlock(),
53
            'locales' => $localesArr,
54
            'position' => $position,
55
        ], $response);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function configureSettings(OptionsResolver $resolver)
62
    {
63
        $resolver->setDefaults([
64
            'template' => 'ApplicationDefaultBundle:Redesign:language_switcher.html.twig',
65
            'position' => 'header',
66
        ]);
67
    }
68
69
    /**
70
     * Get inner sub string in position number.
71
     *
72
     * @param string $string
73
     * @param string $delim
74
     * @param int    $keyNumber
75
     *
76
     * @return string
77
     */
78
    private function getInnerSubstring($string, $delim, $keyNumber = 1)
79
    {
80
        $string = explode($delim, $string, 3);
81
82
        return isset($string[$keyNumber]) ? $string[$keyNumber] : '';
83
    }
84
85
    /**
86
     * Localize current route.
87
     *
88
     * @param Request $request
89
     * @param string  $locale
90
     *
91
     * @return string
92
     */
93
    private function localizeRoute($request, $locale)
94
    {
95
        $path = $request->getPathInfo();
96
        $currentLocal = $this->getInnerSubstring($path, '/');
97
        if (in_array($currentLocal, $this->locales)) {
98
            $path = preg_replace('/^\/'.$currentLocal.'\//', '/', $path);
99
        }
100
        $params = $request->query->all();
101
102
        return $request->getBaseUrl().'/'.$locale.$path.($params ? '?'.http_build_query($params) : '');
103
    }
104
}
105