Completed
Push — locale-in-url ( 151bbf...6507a9 )
by Kamil
20:45
created

StorageBasedLocaleContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getLocaleCode() 0 16 3
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
namespace Sylius\Component\Core\Locale\Context;
13
14
use Sylius\Component\Channel\Context\ChannelContextInterface;
15
use Sylius\Component\Channel\Context\ChannelNotFoundException;
16
use Sylius\Component\Core\Locale\LocaleStorageInterface;
17
use Sylius\Component\Locale\Context\LocaleContextInterface;
18
use Sylius\Component\Locale\Context\LocaleNotFoundException;
19
use Sylius\Component\Locale\Provider\LocaleProviderInterface;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class StorageBasedLocaleContext implements LocaleContextInterface
25
{
26
    /**
27
     * @var ChannelContextInterface
28
     */
29
    private $channelContext;
30
31
    /**
32
     * @var LocaleStorageInterface
33
     */
34
    private $localeStorage;
35
36
    /**
37
     * @var LocaleProviderInterface
38
     */
39
    private $localeProvider;
40
41
    /**
42
     * @param ChannelContextInterface $channelContext
43
     * @param LocaleStorageInterface $localeStorage
44
     * @param LocaleProviderInterface $localeProvider
45
     */
46
    public function __construct(
47
        ChannelContextInterface $channelContext,
48
        LocaleStorageInterface $localeStorage,
49
        LocaleProviderInterface $localeProvider
50
    ) {
51
        $this->channelContext = $channelContext;
52
        $this->localeStorage = $localeStorage;
53
        $this->localeProvider = $localeProvider;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getLocaleCode()
60
    {
61
        $availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $availableLocalesCodes exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
62
63
        try {
64
            $localeCode = $this->localeStorage->get($this->channelContext->getChannel());
65
        } catch (ChannelNotFoundException $exception) {
66
            throw new LocaleNotFoundException(null, $exception);
67
        }
68
69
        if (!in_array($localeCode, $availableLocalesCodes, true)) {
70
            throw LocaleNotFoundException::notAvailable($localeCode, $availableLocalesCodes);
71
        }
72
73
        return $localeCode;
74
    }
75
}
76