Completed
Push — master ( 1c02a4...0f40a4 )
by Rafał
06:14
created

TenantResolver::extractHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
        $domain = $this->extractDomain($host);
46
        $subdomain = $this->extractSubdomain($host);
47
48
        if (null !== $subdomain) {
49
            $tenant = $this->tenantRepository->findOneBySubdomainAndDomain($subdomain, $domain);
50
        } else {
51
            $tenant = $this->tenantRepository->findOneByDomain($domain);
52
        }
53
54
        if (null === $tenant) {
55
            throw new TenantNotFoundException($host);
56
        }
57
58
        return $tenant;
59
    }
60
61
    /**
62
     * @param $host
63
     *
64
     * @return string
65
     */
66
    protected function extractDomain($host)
67
    {
68
        if (null === $host || TenantResolverInterface::LOCALHOST === $host) {
69
            return TenantResolverInterface::LOCALHOST;
70
        }
71
72
        $result = $this->extractHost($host);
73
74
        // handle case for ***.localhost
75 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...
76
            null !== $result->getHostname() &&
77
            null === $result->getSubdomain()
78
        ) {
79
            return $result->getSuffix();
80
        }
81
82
        $domainString = $result->getHostname();
83
        if (null !== $result->getSuffix()) {
84
            $domainString = $domainString.'.'.$result->getSuffix();
85
        }
86
87
        return $domainString;
88
    }
89
90
    /**
91
     * Extracts subdomain from the host.
92
     *
93
     * @param string $host Hostname
94
     *
95
     * @return string
96
     */
97
    protected function extractSubdomain($host)
98
    {
99
        $result = $this->extractHost($host);
100
101
        // handle case for ***.localhost
102 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...
103
            null !== $result->getHostname() &&
104
            null === $result->getSubdomain()
105
        ) {
106
            return $result->getHostname();
107
        }
108
109
        $subdomain = $result->getSubdomain();
110
        if (null !== $subdomain) {
111
            return $subdomain;
112
        }
113
114
        return;
115
    }
116
117
    private function extractHost($host)
118
    {
119
        $extract = new \LayerShifter\TLDExtract\Extract();
120
121
        return $extract->parse($host);
122
    }
123
}
124