HostnameIdentifiedSiteResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 15 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\Site;
6
7
use Symfony\Component\Routing\RequestContext;
8
9
/**
10
 * @author Joris van de Sande <[email protected]>
11
 */
12
final class HostnameIdentifiedSiteResolver implements SiteResolverInterface
13
{
14
    private $siteRepository;
15
    private $identifierMapping;
16
17
    public function __construct(SiteRepositoryInterface $siteRepository, IdentifierMappingInterface $identifierMapping)
18
    {
19
        $this->siteRepository = $siteRepository;
20
        $this->identifierMapping = $identifierMapping;
21
    }
22
23
    /**
24
     * @throws SiteNotFoundException
25
     */
26
    public function resolve(RequestContext $requestContext): SiteInterface
27
    {
28
        $identifier = $this->identifierMapping->findIdentifierByHostname($requestContext->getHost());
29
30
        if (null === $identifier) {
31
            throw SiteNotFoundException::withRequestContext($requestContext);
32
        }
33
34
        foreach ($this->siteRepository->findAll() as $site) {
35
            if ($site instanceof IdentifiedSiteInterface && $site->matches($identifier)) {
36
                return $site;
37
            }
38
        }
39
40
        throw SiteNotFoundException::withRequestContext($requestContext);
41
    }
42
}
43