Completed
Push — 1.4-localised-locales ( 28107e )
by Kamil
81:37 queued 69:17
created

LocaleHelper::getLocaleCode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\LocaleBundle\Templating\Helper;
15
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
use Sylius\Component\Locale\Context\LocaleNotFoundException;
18
use Sylius\Component\Locale\Converter\LocaleConverterInterface;
19
use Symfony\Component\Templating\Helper\Helper;
20
21
final class LocaleHelper extends Helper implements LocaleHelperInterface
22
{
23
    /**@var LocaleConverterInterface */
24
    private $localeConverter;
25
26
    /** @var LocaleContextInterface|null */
27
    private $localeContext;
28
29
    public function __construct(LocaleConverterInterface $localeConverter, ?LocaleContextInterface $localeContext = null)
30
    {
31
        if (null === $localeContext) {
32
            @trigger_error('Not passing LocaleContextInterface explicitly as the second argument is deprecated since 1.4 and will be prohibited in 2.0', \E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
        }
34
35
        $this->localeConverter = $localeConverter;
36
        $this->localeContext = $localeContext;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function convertCodeToName(string $code, ?string $localeCode = null): ?string
43
    {
44
        return $this->localeConverter->convertCodeToName($code, $this->getLocaleCode($localeCode));
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getName(): string
51
    {
52
        return 'sylius_locale';
53
    }
54
55
    private function getLocaleCode(?string $localeCode): ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
56
    {
57
        if (null !== $localeCode) {
58
            return $localeCode;
59
        }
60
61
        if (null === $this->localeContext) {
62
            return null;
63
        }
64
65
        try {
66
            return $this->localeContext->getLocaleCode();
67
        } catch (LocaleNotFoundException $exception) {
68
            return null;
69
        }
70
    }
71
}
72