Completed
Push — 1.4-localised-locales ( a66539 )
by Kamil
12:49
created

LocaleHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A convertCodeToName() 0 4 1
A getName() 0 4 1
A getLocaleCode() 0 16 4
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