Passed
Pull Request — master (#352)
by Rustam
04:14
created

LanguageSelector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Widget;
6
7
use Yiisoft\Csrf\CsrfTokenInterface;
8
use Yiisoft\Form\Widget\Form;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Router\UrlGeneratorInterface;
11
use Yiisoft\Translator\TranslatorInterface;
12
use Yiisoft\Widget\Widget;
13
14
final class LanguageSelector extends Widget
15
{
16
    private UrlGeneratorInterface $urlGenerator;
17
    private TranslatorInterface $translator;
18
    private CsrfTokenInterface $csrfToken;
19
20 10
    public function __construct(UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, CsrfTokenInterface $csrfToken)
21
    {
22 10
        $this->urlGenerator = $urlGenerator;
23 10
        $this->translator = $translator;
24 10
        $this->csrfToken = $csrfToken;
25 10
    }
26
27 10
    protected function run(): string
28
    {
29 10
        $form = Form::widget()
30 10
            ->action($this->urlGenerator->generate('site/set-locale'))
0 ignored issues
show
Bug introduced by
The method action() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Form\Widget\Form. ( Ignorable by Annotation )

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

30
            ->/** @scrutinizer ignore-call */ action($this->urlGenerator->generate('site/set-locale'))
Loading history...
31 10
            ->method('POST')
32 10
            ->options([
33 10
                'id' => 'localeForm',
34 10
                'csrf' => $this->csrfToken->getValue(),
35
            ]);
36
37 10
        $out = $form->begin();
38
39 10
        $select = Html::select('locale')
40 10
            ->value($this->translator->getLocale())
41 10
            ->optionsData([
42 10
                'en' => $this->translator->translate('layout.language.english'),
43 10
                'ru' => $this->translator->translate('layout.language.russian'),
44
            ])
45 10
            ->class('form-select')
46 10
            ->attributes(['aria-label' => $this->translator->translate('layout.change_language')]);
47
48 10
        $out .= Html::div($select, ['class' => 'col-2 d-inline-block']);
49
50 10
        $out .= Html::submitButton($this->translator->translate('layout.change_language'))
51 10
            ->class('btn btn-primary');
52
53 10
        $out .= $form->end();
54 10
        return $out;
55
    }
56
}
57