IdentifierMapping::findHostnamesByIdentifier()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 6
c 4
b 0
f 0
dl 0
loc 13
rs 10
cc 4
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\Site;
6
7
final class IdentifierMapping implements IdentifierMappingInterface
8
{
9
    /**
10
     * @var IdentifierConfig[]
11
     */
12
    private $mapping;
13
14
    /**
15
     * Mapping should be in the form of:
16
     * <code>
17
     * [
18
     *     'identifier' => [ 'hostnames' => [], 'locales' => [] ]
19
     * ]
20
     * </code>.
21
     *
22
     * @param array $mapping mapping from identifier to matching request part
23
     */
24
    public function __construct(array $mapping)
25
    {
26
        foreach ($mapping as $identifier => $config) {
27
            $this->mapping[$identifier] = new IdentifierConfig($config['hostnames'], $config['locales']);
28
        }
29
    }
30
31
    public function findIdentifierByHostname(string $hostname): ?string
32
    {
33
        foreach ($this->mapping as $identifier => $config) {
34
            if (\in_array($hostname, $config->getHostnames(), true)) {
35
                return $identifier;
36
            }
37
        }
38
39
        return null;
40
    }
41
42
    public function findHostnamesByIdentifier(string $identifier, ?string $locale = null): array
43
    {
44
        if (!isset($this->mapping[$identifier])) {
45
            return [];
46
        }
47
48
        $config = $this->mapping[$identifier];
49
50
        if (!\is_string($locale)) {
51
            return $config->getHostnames();
52
        }
53
54
        return \in_array($locale, $config->getLocales(), true) ? $config->getHostnames() : [];
55
    }
56
}
57