Completed
Push — master ( a418da...00a3a8 )
by Rafał
01:45
created

TenantResolver::extractDomain()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 23

Duplication

Lines 6
Ratio 26.09 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 6
loc 23
rs 8.6186
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Component.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\MultiTenancy\Resolver;
16
17
use SWP\Component\MultiTenancy\Exception\TenantNotFoundException;
18
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
19
20
/**
21
 * TenantResolver resolves the tenant based on subdomain.
22
 */
23
class TenantResolver implements TenantResolverInterface
24
{
25
    /**
26
     * @var TenantRepositoryInterface
27
     */
28
    private $tenantRepository;
29
30
    /**
31
     * TenantResolver constructor.
32
     *
33
     * @param TenantRepositoryInterface $tenantRepository
34
     */
35
    public function __construct(TenantRepositoryInterface $tenantRepository)
36
    {
37
        $this->tenantRepository = $tenantRepository;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function resolve($host = null)
44
    {
45
        // remove www prefix from host
46
        $host = str_replace('www.', '', $host);
47
48
        $domain = $this->extractDomain($host);
49
        $subdomain = $this->extractSubdomain($host);
50
51
        if (null !== $subdomain) {
52
            $tenant = $this->tenantRepository->findOneBySubdomainAndDomain($subdomain, $domain);
53
        } else {
54
            $tenant = $this->tenantRepository->findOneByDomain($domain);
55
        }
56
57
        if (null === $tenant) {
58
            throw new TenantNotFoundException($host);
59
        }
60
61
        return $tenant;
62
    }
63
64
    /**
65
     * @param string $host
66
     *
67
     * @return string
68
     */
69
    protected function extractDomain($host)
70
    {
71
        if (null === $host || TenantResolverInterface::LOCALHOST === $host) {
72
            return TenantResolverInterface::LOCALHOST;
73
        }
74
75
        $result = $this->extractHost($host);
76
77
        // handle case for ***.localhost
78 View Code Duplication
        if (TenantResolverInterface::LOCALHOST === $result->getSuffix() &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            null !== $result->getHostname() &&
80
            null === $result->getSubdomain()
81
        ) {
82
            return $result->getSuffix();
83
        }
84
85
        $domainString = $result->getHostname();
86
        if (null !== $result->getSuffix()) {
87
            $domainString = $domainString.'.'.$result->getSuffix();
88
        }
89
90
        return $domainString;
91
    }
92
93
    /**
94
     * Extracts subdomain from the host.
95
     *
96
     * @param string $host Hostname
97
     *
98
     * @return string
99
     */
100
    protected function extractSubdomain($host)
101
    {
102
        $result = $this->extractHost($host);
103
104
        // handle case for ***.localhost
105 View Code Duplication
        if (TenantResolverInterface::LOCALHOST === $result->getSuffix() &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            null !== $result->getHostname() &&
107
            null === $result->getSubdomain()
108
        ) {
109
            return $result->getHostname();
110
        }
111
112
        $subdomain = $result->getSubdomain();
113
        if (null !== $subdomain) {
114
            return $subdomain;
115
        }
116
117
        return;
118
    }
119
120
    private function extractHost($host)
121
    {
122
        $extract = new \LayerShifter\TLDExtract\Extract();
123
124
        return $extract->parse($host);
125
    }
126
}
127