UrlConfiguration::getLocale()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 21
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Client\Coremedia\Api\Configuration;
9
10
use SprykerEco\Client\Coremedia\Api\Exception\UrlConfigurationException;
11
use SprykerEco\Client\Coremedia\CoremediaConfig;
12
13
class UrlConfiguration implements UrlConfigurationInterface
14
{
15
    /**
16
     * @var \SprykerEco\Client\Coremedia\CoremediaConfig
17
     */
18
    protected $config;
19
20
    /**
21
     * @param \SprykerEco\Client\Coremedia\CoremediaConfig $config
22
     */
23
    public function __construct(CoremediaConfig $config)
24
    {
25
        $this->config = $config;
26
    }
27
28
    /**
29
     * @throws \SprykerEco\Client\Coremedia\Api\Exception\UrlConfigurationException
30
     *
31
     * @return string
32
     */
33
    public function getCoremediaHost(): string
34
    {
35
        $coreMediaHost = $this->config->getCoremediaHost();
36
37
        if (!$coreMediaHost) {
38
            throw new UrlConfigurationException('Please specify the Coremedia host in configuration.');
39
        }
40
41
        return $coreMediaHost;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getBasePath(): string
48
    {
49
        return $this->config->getFragmentBasePath();
50
    }
51
52
    /**
53
     * @param string $storeName
54
     *
55
     * @throws \SprykerEco\Client\Coremedia\Api\Exception\UrlConfigurationException
56
     *
57
     * @return string
58
     */
59
    public function getStore(string $storeName): string
60
    {
61
        $applicationStoreMapping = $this->config->getApplicationStoreMapping();
62
63
        if (!isset($applicationStoreMapping[$storeName])) {
64
            throw new UrlConfigurationException(
65
                sprintf('Cannot find storeId by store name "%s" in application store mapping.', $storeName)
66
            );
67
        }
68
69
        return $applicationStoreMapping[$storeName];
70
    }
71
72
    /**
73
     * @param string $store
74
     * @param string $localeName
75
     *
76
     * @throws \SprykerEco\Client\Coremedia\Api\Exception\UrlConfigurationException
77
     *
78
     * @return string
79
     */
80
    public function getLocale(string $store, string $localeName): string
81
    {
82
        $applicationStoreLocaleMapping = $this->config->getApplicationStoreLocaleMapping();
83
84
        if (!isset($applicationStoreLocaleMapping[$store])) {
85
            throw new UrlConfigurationException(
86
                sprintf('Not defined storeId "%s" in application store locale mapping.', $store)
87
            );
88
        }
89
90
        if (!isset($applicationStoreLocaleMapping[$store][$localeName])) {
91
            throw new UrlConfigurationException(
92
                sprintf(
93
                    'Cannot find locale by locale name "%s" for storeId "%s" in application store locale mapping.',
94
                    $localeName,
95
                    $store
96
                )
97
            );
98
        }
99
100
        return $applicationStoreLocaleMapping[$store][$localeName];
101
    }
102
}
103