Completed
Push — master ( f3cf07...ead026 )
by Paweł
237:27 queued 221:30
created

ChannelBasedLocaleProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAvailableLocalesCodes() 0 20 2
A getDefaultLocaleCode() 0 11 2
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\Provider;
13
14
use Sylius\Component\Channel\Context\ChannelContextInterface;
15
use Sylius\Component\Channel\Context\ChannelNotFoundException;
16
use Sylius\Component\Core\Model\ChannelInterface;
17
use Sylius\Component\Locale\Context\LocaleNotFoundException;
18
use Sylius\Component\Locale\Model\LocaleInterface;
19
use Sylius\Component\Locale\Provider\LocaleProviderInterface;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class ChannelBasedLocaleProvider implements LocaleProviderInterface
25
{
26
    /**
27
     * @var ChannelContextInterface
28
     */
29
    private $channelContext;
30
31
    /**
32
     * @var string
33
     */
34
    private $defaultLocaleCode;
35
36
    /**
37
     * @param ChannelContextInterface $channelContext
38
     * @param string $defaultLocaleCode
39
     */
40
    public function __construct(ChannelContextInterface $channelContext, $defaultLocaleCode)
41
    {
42
        $this->channelContext = $channelContext;
43
        $this->defaultLocaleCode = $defaultLocaleCode;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAvailableLocalesCodes()
50
    {
51
        try {
52
            /** @var ChannelInterface $channel */
53
            $channel = $this->channelContext->getChannel();
54
55
            return $channel
56
                ->getLocales()
57
                ->filter(function (LocaleInterface $locale) {
58
                    return $locale->isEnabled();
59
                })
60
                ->map(function (LocaleInterface $locale) {
61
                    return $locale->getCode();
62
                })
63
                ->toArray()
64
            ;
65
        } catch (ChannelNotFoundException $exception) {
66
            return [$this->defaultLocaleCode];
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getDefaultLocaleCode()
74
    {
75
        try {
76
            /** @var ChannelInterface $channel */
77
            $channel = $this->channelContext->getChannel();
78
79
            return $channel->getDefaultLocale()->getCode();
80
        } catch (ChannelNotFoundException $exception) {
81
            return $this->defaultLocaleCode;
82
        }
83
    }
84
}
85