Completed
Push — master ( ab6ee6...8da333 )
by Kamil
32:30 queued 09:03
created

LocaleContext::getLocaleByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
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\Behat\Context\Transform;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Component\Locale\Converter\LocaleConverterInterface;
18
use Sylius\Component\Locale\Model\LocaleInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
use Webmozart\Assert\Assert;
21
22
final class LocaleContext implements Context
23
{
24
    /** @var LocaleConverterInterface */
25
    private $localeNameConverter;
26
27
    /** @var RepositoryInterface */
28
    private $localeRepository;
29
30
    public function __construct(LocaleConverterInterface $localeNameConverter, RepositoryInterface $localeRepository)
31
    {
32
        $this->localeNameConverter = $localeNameConverter;
33
        $this->localeRepository = $localeRepository;
34
    }
35
36
    /**
37
     * @Transform :language
38
     * @Transform :localeCode
39
     * @Transform /^"([^"]+)" locale$/
40
     * @Transform /^in the "([^"]+)" locale$/
41
     */
42
    public function castToLocaleCode(string $localeName): string
43
    {
44
        return $this->localeNameConverter->convertNameToCode($localeName);
45
    }
46
47
    /**
48
     * @Transform :localeName
49
     */
50
    public function castToLocaleName(string $localeCode): string
51
    {
52
        return $this->localeNameConverter->convertCodeToName($localeCode);
53
    }
54
55
    /**
56
     * @Transform :locale
57
     */
58
    public function getLocaleByName(string $name): LocaleInterface
59
    {
60
        $locale = $this->localeRepository->findOneBy(['code' => $this->localeNameConverter->convertNameToCode($name)]);
61
62
        Assert::isInstanceOf(
63
            $locale,
64
            LocaleInterface::class,
65
            sprintf('Cannot find "%s" locale.', $name)
66
        );
67
68
        return $locale;
69
    }
70
}
71